在 sklearn 中进行交叉验证时出现 TypeError

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

这是我的代码:

from sklearn import cross_validation, linear_modelimport numpy as npX_digits = xY_digits = list(np.array(y).reshape(-1,))loo = cross_validation.LeaveOneOut(len(Y_digits))# 确保它能工作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 尝试进行某种分类线性回归的过程充满了困难,我现在欢迎任何建议。

EDIT 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.

回答:

交叉验证迭代器返回的索引用于索引 numpy 数组,但你的数据是普通的 Python 列表。Python 列表不支持 numpy 数组所支持的复杂索引。你看到这个错误是因为 Python 试图将 traintest 解释为可以用于索引列表的东西,但无法做到。你需要对 X_digitsY_digits 使用 numpy 数组而不是列表。(或者,你可以使用列表解析或类似方法提取给定的索引,但由于 scikit 最终会转换为 numpy,你还不如从一开始就使用 numpy。)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注