Scikit学习线性回归多个输出

2 投票
1 回答
1269 浏览
提问于 2025-04-17 19:44

我正在尝试使用scikit-learn来进行线性回归,并且有多个输出。

这是我的代码(用随机数据作为例子):

from sklearn import datasets, linear_model
import numpy as np

X = np.random.rand(300,10)
y = np.random.rand(300,9)
reg_model = linear_model.LinearRegression()
reg_model.fit(X,y)

我遇到了以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/sorensonderby/Documents/workspaces/workspace/Chemoinformatics_proect/notebooks/<ipython-input-116-e235c7159573> in <module>()
      5 y = np.random.rand(300,9)
      6 reg_model = linear_model.LinearRegression()
----> 7 reg_model.fit(X,y)
      8 

/Library/Python/2.7/site-packages/scikit_learn-0.10-py2.7-macosx-10.7-intel.egg/sklearn/linear_model/base.pyc in fit(self, X, y)
    178                     linalg.lstsq(X, y)
    179 
--> 180         self._set_intercept(X_mean, y_mean, X_std)
    181         return self
    182 

/Library/Python/2.7/site-packages/scikit_learn-0.10-py2.7-macosx-10.7-intel.egg/sklearn/linear_model/base.pyc in _set_intercept(self, X_mean, y_mean, X_std)
    106         """
    107         if self.fit_intercept:
--> 108             self.coef_ = self.coef_ / X_std
    109             self.intercept_ = y_mean - np.dot(X_mean, self.coef_.T)
    110         else:

ValueError: operands could not be broadcast together with shapes (10,9) (10)

我查看了fit方法的说明,上面说x的形状应该是样本数乘以特征数,而y的形状应该是样本数乘以目标数。 这是fit方法的链接

我哪里做错了呢?

1 个回答

1

你现在用的是scikit-learn 0.10版本,但你查看的是0.13.1版本的说明文档。建议你把软件升级到最新版本,然后再试一次,这样应该就能正常工作了。

撰写回答