Python 在sklearn中尝试交叉验证时出现TypeError
我真的需要一些帮助,但我是编程新手,所以请原谅我的无知。我试图使用scikit的普通最小二乘回归作为估计量对数据集进行交叉验证 这是我的密码:Python 在sklearn中尝试交叉验证时出现TypeError,python,numpy,machine-learning,scikit-learn,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))
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编码
考虑到这一点,有没有关于如何解决这个问题的建议
将回归估计器与这些数据进行拟合似乎是可行的,尽管我得到了很多非常大/奇怪的系数。老实说,在进入SKL的整个过程中,学习尝试某种类型的分类线性回归是完全令人担忧的,我欢迎在这一点上的任何建议
编辑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
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.]
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.
编辑4尝试将列表转换为数组,但遇到错误:
---------------------------------------------------------------------------
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
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.]
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.
X_位=np.数组(X)
Y_位=np.数组(Y)
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
14
15
--->16[重新拟合(X_位数[列车]、Y_位数[列车])。分数(X_位数[测试]、Y_位数[测试])列车,在loo中测试]
C:\Program Files\Anaconda\lib\site packages\sklearn\base.py(self,X,y)
320
321来自。指标导入r2_分数
-->322返回r2_分数(y,自我预测(X))
323
324
C:\Program Files\Anaconda\lib\site packages\sklearn\metrics\metrics.py在r2\u分数中(y\u true,y\u pred)
2184
2185如果len(y_true)==1:
->2186 raise VALUE ERROR(“r2_分数只能在一个以上的情况下计算”
2187“样本”)
2188分子=(y_真-y_pred)**2.sum(dtype=np.float64)
ValueError:r2_分数只能在多个样本的情况下计算。
交叉验证迭代器返回索引,用于索引numpy数组,但您的数据是纯Python列表。Python列表不支持numpy数组所做的那种奇特的索引。您之所以看到这个错误,是因为Python试图将train
和test
解释为它可以用来索引到列表中的东西,但它无法这样做。对于X\u位
和Y\u位
,您需要使用numpy数组而不是列表。(或者,您可以使用列表理解或类似方法提取给定的索引,但由于scikit无论如何都将转换为numpy,因此您最好首先使用numpy。)交叉验证迭代器返回索引以用于索引numpy数组,但您的数据是纯Python列表。Python列表不支持numpy数组所做的那种奇特的索引。您之所以看到这个错误,是因为Python试图将train
和test
解释为它可以用来索引到列表中的东西,但它无法这样做。对于X\u位
和Y\u位
,您需要使用numpy数组而不是列表。(或者,您可以使用列表理解或类似方法提取给定的索引,但由于scikit无论如何都将转换为numpy,您最好首先使用numpy。)您可以显示整个回溯(错误消息)吗?当然可以!现在将更新主帖子。你能显示整个回溯(错误消息)吗?当然!将更新主帖子。嗨,谢谢你的建议-我会根据你的评论更新我的帖子。将列表转换为数组后,我收到另一个错误:ValueError:r2_分数只能在给定多个样本的情况下计算。您只保留了一个样本。您不能仅对一个样本计算r^2
。使用不同的交叉验证折叠对象,例如KFold
。啊,是的,当然-我刚刚意识到我应该在这里使用MSE参数(在模型选择阶段)!您还可以使用np.array(my_Python_list)将普通Python列表转换为Numpy数组嗨,谢谢您的建议-我将根据您的评论更新我的帖子。将列表转换为数组后,我收到另一个错误:ValueError:r2_分数只能在给定多个样本的情况下计算。您只保留了一个样本。您不能仅对一个样本计算r^2
。使用不同的交叉验证折叠对象,例如KFold
。啊,是的,当然-我刚刚意识到我应该在这里使用MSE参数(在模型选择阶段)!您还可以使用np.array(my_Python_list)将普通Python列表转换为Numpy数组