Python 线性回归返回的结果与合成参数不同
正在尝试此代码:Python 线性回归返回的结果与合成参数不同,python,scikit-learn,linear-regression,Python,Scikit Learn,Linear Regression,正在尝试此代码: from sklearn import linear_model import numpy as np x1 = np.arange(0,10,0.1) x2 = x1*10 y = 2*x1 + 3*x2 X = np.vstack((x1, x2)).transpose() reg_model = linear_model.LinearRegression() reg_model.fit(X,y) print reg_model.coef_ # should be
from sklearn import linear_model
import numpy as np
x1 = np.arange(0,10,0.1)
x2 = x1*10
y = 2*x1 + 3*x2
X = np.vstack((x1, x2)).transpose()
reg_model = linear_model.LinearRegression()
reg_model.fit(X,y)
print reg_model.coef_
# should be [2,3]
print reg_model.predict([5,6])
# should be 2*5 + 3*6 = 28
print reg_model.intercept_
# perfectly at the expected value of 0
print reg_model.score(X,y)
# seems to be rather confident to be right
结果是
- [0.31683168 3.16831683]
- 20.5940594059
- 0.0
- 1.0
因此不是我所期望的-它们与用于合成数据的参数不同。为什么会这样?您的问题在于解决方案的唯一性,因为两个维度都是相同的(对一个维度应用线性变换在该模型眼中不会产生唯一的数据),因此您可以得到无限多适合您的数据的可能解决方案。将非线性变换应用到第二维度,您将看到所需的输出
from sklearn import linear_model
import numpy as np
x1 = np.arange(0,10,0.1)
x2 = x1**2
X = np.vstack((x1, x2)).transpose()
y = 2*x1 + 3*x2
reg_model = linear_model.LinearRegression()
reg_model.fit(X,y)
print reg_model.coef_
# should be [2,3]
print reg_model.predict([[5,6]])
# should be 2*5 + 3*6 = 28
print reg_model.intercept_
# perfectly at the expected value of 0
print reg_model.score(X,y)
输出为
[2.3.]
[28.]
-2.84217094304e-14
1.0