【问题标题】:TypeError when attempting cross validation in sklearn在 sklearn 中尝试交叉验证时出现 TypeError
【发布时间】:2014-08-04 15:17:06
【问题描述】:

我真的需要一些帮助,但我是编程新手,所以请原谅我的普遍无知。我正在尝试使用来自 scikit 的普通最小二乘回归作为估计器对数据集执行交叉验证。

这是我的代码:

from sklearn import cross_validation, linear_model
import numpy as np

X_digits = x
Y_digits = list(np.array(y).reshape(-1,))

loo = cross_validation.LeaveOneOut(len(Y_digits))

# Make sure it works
for train_indices, test_indices in loo:
    print('Train: %s | test: %s' % (train_indices, test_indices))

regr = linear_model.LinearRegression()

[regr.fit(X_digits[train], Y_digits[train]).score(X_digits[test], Y_digits[test]) for train, test in loo]

当我运行它时,我得到一个错误:

**TypeError: only integer arrays with one element can be converted to an index**

这应该是指我的 x 值,它们是 0 和 1 的列表 - 每个列表代表一个已使用 OneHotEncoder 编码的分类变量。

考虑到这一点 - 对于如何解决这个问题有什么建议吗?

对这些数据拟合回归估计器似乎可行,尽管我得到了很多非常大/看起来很奇怪的系数。老实说,在 sklearn 中尝试某种分类线性回归的整个过程非常令人担忧,在这一点上我欢迎任何建议。

编辑 2 抱歉,我尝试了另一种方法并错误地将错误回调:

    ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-be578cbe0327> in <module>()
     16 regr = linear_model.LinearRegression()
     17 
---> 18 [regr.fit(X_digits[train], Y_digits[train]).score(X_digits[test], Y_digits[test]) for train, test in loo]

TypeError: only integer arrays with one element can be converted to an index

EDIT 3 添加我的自变量 (x) 数据的示例:

print x[1]
[ 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  1.  0.  0.  0.  0.]

EDIT 4 尝试将列表转换为数组,遇到错误:

X_digits = np.array(x)
Y_digits = np.array(y)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-20-ea8b84f0005f> in <module>()
     14 
     15 
---> 16 [regr.fit(X_digits[train], Y_digits[train]).score(X_digits[test], Y_digits[test]) for train, test in loo]

C:\Program Files\Anaconda\lib\site-packages\sklearn\base.py in score(self, X, y)
    320 
    321         from .metrics import r2_score
--> 322         return r2_score(y, self.predict(X))
    323 
    324 

C:\Program Files\Anaconda\lib\site-packages\sklearn\metrics\metrics.py in r2_score(y_true, y_pred)
   2184 
   2185     if len(y_true) == 1:
-> 2186         raise ValueError("r2_score can only be computed given more than one"
   2187                          " sample.")
   2188     numerator = ((y_true - y_pred) ** 2).sum(dtype=np.float64)

ValueError: r2_score can only be computed given more than one sample.

【问题讨论】:

  • 能否请您显示整个回溯(错误消息)?
  • 确定!现在将更新主帖。

标签: python numpy machine-learning scikit-learn


【解决方案1】:

交叉验证迭代器返回用于索引 numpy 数组的索引,但您的数据是纯 Python 列表。 Python 列表不支持 numpy 数组所做的花哨类型的索引。您看到此错误是因为 Python 试图将 traintest 解释为可用于索引到列表中的东西,但无法这样做。您需要为 X_digitsY_digits 使用 numpy 数组而不是列表。 (或者,您可以使用列表解析等提取给定的索引,但由于 scikit 无论如何都会转换为 numpy,因此您最好首先使用 numpy。)

【讨论】:

  • 您好,感谢您的建议 - 我将根据您的 cmets 更新我的帖子。将列表转换为数组后,我收到另一个错误:ValueError:r2_score 只能在给定多个样本的情况下计算。
  • 您只留下一个样本。您不能仅对一个样本计算 r^2。使用不同的交叉验证折叠对象,例如KFold
  • 啊,是的,当然——我刚刚意识到我应该在这里使用 MSE 参数(在模型选择阶段)!
  • 您还可以使用 np.array(my_python_list) 将纯 Python 列表转换为 Numpy 数组
猜你喜欢
  • 2017-12-26
  • 2012-12-31
  • 2018-08-16
  • 2018-04-26
  • 1970-01-01
  • 2014-11-24
  • 1970-01-01
  • 2016-05-07
  • 1970-01-01
相关资源
最近更新 更多