且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python:Sklearn.linear_model.LinearRegression 工作很奇怪

更新时间:2022-06-12 08:55:45

为了找回初始系数,在构建线性回归时需要使用关键字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).