python - 在 sklearn 中尝试交叉验证时出现类型错误

标签 python numpy machine-learning scikit-learn

我真的需要一些帮助,但我是编程新手,所以请原谅我的无知。我正在尝试使用 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

编辑 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.]

编辑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.

最佳答案

交叉验证迭代器返回用于索引 numpy 数组的索引,但您的数据是普通的 Python 列表。 Python 列表不支持 numpy 数组那样的奇特索引。您看到此错误是因为 Python 试图将 traintest 解释为可用于索引列表的内容,但无法执行此操作。您需要使用 numpy 数组而不是 X_digitsY_digits 列表。 (或者,您可以使用列表理解等提取给定的索引,但由于 scikit 无论如何都会转换为 numpy,所以您最好首先使用 numpy。)

关于python - 在 sklearn 中尝试交叉验证时出现类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24215886/

相关文章:

python - 将队列附加到 tensorflow 中的 numpy 数组以获取数据而不是文件?

python - PCA 和朴素贝叶斯分类器

python - 优化 App Engine 上的 RSS 解析以避免高 CPU 警告

python - 在Python中使用MXNet预训练图像分类模型

python - 将数组从 MATLAB 转换为 numpy

python - 数组乘以标量的意外结果

python - 在我的图中扩展 pylab.poly1d

python - 无法使用 anaconda 更新到 numpy 1.13?

machine-learning - 使用数据集进行 TensorFlow 分类

python - SciKit Learn 中的多类逻辑回归