As I understand, numpy.linalg.lstsq
and sklearn.linear_model.LinearRegression
both look for solutions x
of the linear system Ax = y
, that minimise the resdidual sum ||Ax - y||
.
But they don't give the same result:
from sklearn import linear_model
import numpy as np
A = np.array([[1, 0], [0, 1]])
b = np.array([1, 0])
x , _, _, _ = np.linalg.lstsq(A,b)
x
Out[1]: array([ 1., 0.])
clf = linear_model.LinearRegression()
clf.fit(A, b)
coef = clf.coef_
coef
Out[2]: array([ 0.5, -0.5])
What am I overlooking?
linear_model.LinearRegression(fit_intercept=False)
to get the same result asnp.linalg.lstsq
. – Quintillion