线性回归返回的结果与合成参数不同 [英] Linear Regression Returns Different Results Than Synthetic Parameters

查看:109
本文介绍了线性回归返回的结果与合成参数不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试此代码:

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

,因此不是我所期望的-它们与用于合成数据的参数不同.为什么会这样?

and therefore not what I expected - they are not the same as the parameters used to synthesize the data. Why is this so?

推荐答案

您的问题在于解决方案的唯一性,因为这两个维度都是相同的(在一个维度上应用线性变换并不会产生唯一的数据)模型),您将获得无数个可能适合您数据的解决方案.将非线性变换应用于第二维,您将看到所需的输出.

Your problem is with the uniqueness of solutions, as both dimensions are the same (applying a linear transform to one dimension does not make unique data in the eyes of this model), you get an infinite number of possible solutions that will fit you data. Applying a non-linear transformation to your second dimension you will see the desired output.

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
  • [ 2. 3.]
  • [ 28.]
  • -2.84217094304e-14
  • 1.0

这篇关于线性回归返回的结果与合成参数不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆