All problems
2D Linear Regression via Normal Equation
Problem
Fit y = w1*x1 + w2*x2 (no intercept) by solving the 2x2 normal equation (X^T X) w = X^T y, where X is a list of [x1, x2] rows.
Return [w1, w2] using Cramer's rule. If the determinant of X^T X is 0 (within 1e-12), return [0.0, 0.0]. Empty X also returns [0.0, 0.0].
Hint
Build s11, s12, s22 from X and t1, t2 from X^T y, then solve the 2x2 system with Cramer's rule.
Test cases
case 1
in: [[[1,0],[0,1],[1,1]],[1,2,3]]
expected: [1,2]
case 2
in: [[[1,2],[2,1],[3,3]],[5,4,9]]
expected: [1,2]
case 3
in: [[[2,0],[0,2],[1,1]],[4,2,3]]
expected: [2,1]
case 4
in: [[[1,1],[2,2],[3,3]],[1,2,3]]
expected: [0,0]
Starter codePython
def fit_2d_normal_equation(X, y):
# Returns [w1, w2]
# Your code here
pass4 test cases · run your code against them in the editor