【问题标题】:Scikit learn linear regression with several outputsScikit 学习具有多个输出的线性回归
【发布时间】:2013-03-09 10:44:13
【问题描述】:

我正在尝试使用 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 方法的 api,其中说 x 应该是 n_sample x n_features,y 应该是 n_sample x n_targets。 Link to fit method

我做错了什么?

【问题讨论】:

    标签: python scikit-learn linear-regression


    【解决方案1】:

    您正在使用 scikit-learn 0.10 和 0.13.1 的文档。升级您的安装,然后重试——它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2021-05-25
      • 2016-10-01
      • 2019-04-19
      • 2020-07-04
      • 2021-01-27
      • 2016-03-31
      • 2015-10-08
      • 2019-07-18
      • 2018-01-26
      相关资源
      最近更新 更多