简体   繁体   English

如何使用 train_test_split 拆分元组?

[英]How to split a tuple using train_test_split?

X = (569,30)
y = (569,)
X_train, X_test, y_train, y_test = train_test_split(np.asarray(X),np.asarray(y),test_size = 0.25, random_state=0)

I am expecting output as below:我期待 output 如下:

  • X_train has shape (426, 30) X_train 具有形状 (426, 30)
  • X_test has shape (143, 30) X_test 具有形状 (143, 30)
  • y_train has shape (426,) y_train 的形状为 (426,)
  • y_test has shape (143,) y_test 的形状为 (143,)

But i am getting the following warning但我收到以下警告

ValueError: Found input variables with inconsistent numbers of samples: [2, 1]

I know that, i can get the desired output in another way, all the problems found in the online show that lengths of X and y are not same but in my case that's not the problem.我知道,我可以通过另一种方式获得所需的 output,在线发现的所有问题都表明 X 和 y 的长度不一样,但在我的情况下这不是问题。

It seems that you're misunderstanding what train_test_split does.您似乎误解了train_test_split的作用。 It is not expecting the shapes of the input arrays, what it does is to split the input arrays into train and test sets.它不期望输入 arrays 的形状,它所做的是将输入 arrays拆分为训练集和测试集。 So you must feed it the actual arrays, for instace:所以你必须给它实际的 arrays,例如:

X = np.random.rand(569,30)
y =  np.random.randint(0,2,(569))
X_train, X_test, y_train, y_test = train_test_split(np.asarray(X),np.asarray(y),test_size = 0.25, random_state=0)

print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)

(426, 30)
(143, 30)
(426,)
(143,)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python,train_test_split 是如何工作的? - Python, how train_test_split works? 如何使用train_test_split将未标记的数据拆分为训练集和测试集? - How to split unlabeled data into train and test set using train_test_split? 如何在不使用 function train_test_split 的情况下将数据拆分为测试和训练? - How can I split the data into test and train without using function train_test_split? train_test_split:值错误 - train_test_split: ValueError 通过 train_test_split() 使用单独的测试和训练文件 - Using seperated test and train files with train_test_split() 如何在不使用train_test_split()的情况下拆分数据集? - How to split the data set without train_test_split()? 如何使用 Python Numpy 中的 train_test_split 将数据拆分为训练、测试和验证数据集? 分裂不应该是随机的 - How to split data by using train_test_split in Python Numpy into train, test and validation data set? The split should not random 带有test_size = 0的train_test_split如何影响数据? - How is train_test_split with test_size=0 affecting the data? 使用 train_test_split 后 100% 分类器准确率 - 100% classifier accuracy after using train_test_split 在数据帧列表上使用 train_test_split - Using train_test_split over a list of dataframes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM