Fit ordinary least-squares linear regression with an intercept by solving the normal equations. Prepend a column of ones to X, then solve (X~⊤X~)β=X~⊤y.
Implement solve(X, y).
Returns. Return a NumPy vector with the intercept first, followed by one coefficient per feature. The Moore-Penrose pseudo-inverse is used, so perfectly collinear features do not crash.
Example 1
Input:
solve([[1.0], [2.0], [3.0]], [2.0, 4.0, 6.0])
Output:
[0.0, 2.0]
Example 2
Input:
solve([[1.0, 0.0], [0.0, 1.0], [1.0, 1.0], [2.0, 1.0]], [1.0, 2.0, 3.0, 4.0])
Output:
[0.0, 1.0, 2.0]
No results yet
Run your code to check it against the examples, or submit to run every test.