"TypeError:单例数组不能被视为有效集合".使用sklearn train_test_split [英] "TypeError: Singleton array cannot be considered a valid collection" using sklearn train_test_split

查看:128
本文介绍了"TypeError:单例数组不能被视为有效集合".使用sklearn train_test_split的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TypeError:单例数组array(0.2)不能被视为有效集合.

TypeError: Singleton array array(0.2) cannot be considered a valid collection.

X = df.iloc[:, [1,7]].values
y= df.iloc[:,-1].values
from sklearn.model_selection import train_test_split 
X_train, X_test, y_train, y_test = train_test_split(X, y, 0.2)

尝试train_test_split时出现此错误.我能够用X和y值训练我的模型.但是,我想拆分数据框,然后对其进行训练和测试.

I am getting this error when trying to train_test_split. I am able to train my model with X and y values. However, i would like to split my dataframe and then train and test it.

感谢您的帮助.

推荐答案

一个鲜为人知的事实是源代码了解更多信息.

A not-so-commonly known fact is that train_test_split can split any number of arrays, not just two ("train", and "test"). See the linked docs and the source code for more info.

例如

np.random.seed(0)
df1 = pd.DataFrame(np.random.choice(10, (5, 4)), columns=list('ABCD'))
y = df1.pop('C')
z = df1.pop('D')
X = df1

splits = train_test_split(X, y, z, test_size=0.2)
len(splits)
# 6

IOW,指定测试大小的唯一方法是通过指定关键字参数 test_size .假定所有位置参数都是要拆分的集合,在这种情况下,因为您这样做

IOW, the only way to specify the test size is by specifying the keyword argument test_size. All positional arguments are assumed to be collections that are to be split, and in your case, since you do

train_test_split(X, y, 0.2)

该函数尝试拆分 0.2,但由于浮点数不是集合,因此会引发错误.解决方案是(如前所述)指定关键字参数:

The function tries to split 0.2, but since a float is not a collection, the error is raised. The solution is to (as mentioned), specify the keyword argument:

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

这篇关于"TypeError:单例数组不能被视为有效集合".使用sklearn train_test_split的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆