cross_validation.train_test_split

在机器学习中,该函数可按照用户设定的比例,随机将样本集合划分为训练集 和测试集,并返回划分好的训练集和测试集数据。

语法

X_train,X_test, y_train, y_test =cross_validation.train_test_split(X,y,test_size, random_state)
  • 1

参数说明

Code Text
X 待划分的样本特征集合
y 待划分的样本标签
test_size 若在0~1之间,为测试集样本数目与原始样本数目之比;若为整数,则是测试集样本的数目。
random_state 随机数种子
X_train 划分出的训练集数据(返回值)
X_test 划分出的测试集数据(返回值)
y_train 划分出的训练集标签(返回值)
y_test 划分出的测试集标签(返回值)

代码示例 
输入:

import numpy as np
from sklearn.model_selection import train_test_split

#创建一个数据集X和相应的标签y,X中样本数目为100
X, y = np.arange(200).reshape((100, 2)), range(100)

#用train_test_split函数划分出训练集和测试集,测试集占比0.33
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.33, random_state=42)

#打印出原始样本集、训练集和测试集的数目
print("The length of original data X is:", X.shape[0])
print("The length of train Data is:", X_train.shape[0])
print("The length of test Data is:", X_test.shape[0])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

输出:

The length of original data X is: 100
The length of train Data is: 67
The length of test Data is: 33

猜你喜欢

转载自blog.csdn.net/qq_30868235/article/details/80369686