您的位置:首页 > 大数据 > 人工智能

Sklearn的train_test_split用法

2018-01-28 20:17 627 查看
用途

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

语法

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


参数说明

CodeText
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])


输出:

The length of original data X is: 100
The length of train Data is: 67
The length of test Data is: 33
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  机器学习