Python:Sklearn.linear_model.LinearRegression 工作很奇怪 [英] Python: Sklearn.linear_model.LinearRegression working weird

查看:32
本文介绍了Python:Sklearn.linear_model.LinearRegression 工作很奇怪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行多变量线性回归.但我发现 sklearn.linear_model 工作起来很奇怪.这是我的代码:

I am trying to do multiple variables linear regression. But I find that the sklearn.linear_model working very weird. Here's my code:

import numpy as np
from sklearn import linear_model

b = np.array([3,5,7]).transpose() ## the right answer I am expecting
x = np.array([[1,6,9],   ## 1*3 + 6*5 + 7*9 = 96
              [2,7,7],   ## 2*3 + 7*5 + 7*7 = 90
              [3,4,5]])  ## 3*3 + 4*5 + 5*7 = 64
y = np.array([96,90,64]).transpose()

clf = linear_model.LinearRegression()
clf.fit([[1,6,9],
         [2,7,7],
         [3,4,5]], [96,90,64])
print clf.coef_ ## <== it gives me [-2.2  5  4.4] NOT [3, 5, 7]
print np.dot(x, clf.coef_) ## <== it gives me [ 67.4  61.4  35.4]

推荐答案

为了找回初始系数,在构建线性回归时需要使用关键字fit_intercept=False.

In order to find your initial coefficients back you need to use the keyword fit_intercept=False when construction the linear regression.

import numpy as np
from sklearn import linear_model

b = np.array([3,5,7])
x = np.array([[1,6,9],  
              [2,7,7],   
              [3,4,5]])  
y = np.array([96,90,64])

clf = linear_model.LinearRegression(fit_intercept=False)
clf.fit(x, y)
print clf.coef_
print np.dot(x, clf.coef_)

使用 fit_intercept=False 可以防止 LinearRegression 对象与 x - x.mean(axis=0) 一起工作,否则它会do(并使用恒定偏移量 y = xb + c 捕获平均值) - 或等效地通过将 1 列添加到 x.

Using fit_intercept=False prevents the LinearRegression object from working with x - x.mean(axis=0), which it would otherwise do (and capture the mean using a constant offset y = xb + c) - or equivalently by adding a column of 1 to x.

顺便说一句,对一维数组调用 transpose 没有任何效果(它颠倒了轴的顺序,而您只有一个).

As a side remark, calling transpose on a 1D array doesn't have any effect (it reverses the order of your axes, and you only have one).

这篇关于Python:Sklearn.linear_model.LinearRegression 工作很奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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