linearRegression() 返回列表中的列表(sklearn)

Zac*_*ach 5 python regression list

我正在 Python ( sklearn ) 中进行多元线性回归,但由于某种原因,系数没有正确返回为列表。相反,返回一个列表 IN A LIST:

from sklearn import linear_model
clf = linear_model.LinearRegression()
# clf.fit ([[0, 0, 0], [1, 1, 1], [2, 2, 2]], [0, 1, 2])
clf.fit([[394, 3878, 13, 4, 0, 0],[384, 10175, 14, 4, 0, 0]],[3,9])
print 'coef array',clf.coef_
print 'length', len(clf.coef_)
print 'getting value 0:', clf.coef_[0]
print 'getting value 1:', clf.coef_[1]
Run Code Online (Sandbox Code Playgroud)

这将返回列表 [[]] 而不是列表 [] 的列表中的值。知道为什么会这样吗?输出:

coef array [[  1.03428648e-03   9.54477167e-04   1.45135995e-07   0.00000000e+00
0.00000000e+00   0.00000000e+00]]
length 1
getting value 0: [  1.03428648e-03   9.54477167e-04   1.45135995e-07   0.0000000
0e+00 0.00000000e+00   0.00000000e+00]
getting value 1:
Traceback (most recent call last):
  File "regress.py", line 8, in <module>
    print 'getting value 1:', clf.coef_[1]
IndexError: index out of bounds
Run Code Online (Sandbox Code Playgroud)

但这有效:

from sklearn import linear_model
clf = linear_model.LinearRegression()
clf.fit ([[0, 0, 0], [1, 1, 1], [2, 2, 2]], [0, 1, 2])
# clf.fit([[394, 3878, 13, 4, 0, 0],[384, 10175, 14, 4, 0, 0]],[3,9])
print 'coef array',clf.coef_
print 'length', len(clf.coef_)
print 'getting value 0:', clf.coef_[0]
print 'getting value 1:', clf.coef_[1]
Run Code Online (Sandbox Code Playgroud)

输出:

coef array [ 0.33333333  0.33333333  0.33333333]
length 3
getting value 0: 0.333333333333
getting value 1: 0.333333333333
Run Code Online (Sandbox Code Playgroud)

Zac*_*ach 3

通过更新 SciKit-Learn 文件夹中的两个文件可以解决此问题。

代码在这里: https: //github.com/scikit-learn/scikit-learn/commit/d0b20f0a21ba42b85375b1fbc7202dc3962ae54f