train_test_split是sklearn的库函数,用于将数据集随机划分为训练子集和测试子集,并返回划分好的样本和标签,在模型训练中很好用。格式:
X_train,X_test, y_train, y_test =cross_validation.train_test_split(train_data,train_target,test_size=0.2, random_state=0)
函数说明的链接为:http://scikit-learn.org/stable/modules/generated/sklearn.cross_validation.train_test_split.html
函数主要参数解释:
train_data:被划分的样本集;
train_target:被划分的样本标签;
test_size:一般是是浮点数,在0-1之间,表示样本占比,比如test_size=0.2;如果是整数的话就是样本的数量;
random_state:是随机数的种子,这个用处比较小。随机数种子:产生该组随机数的编号,在需要重复试验的时候,可以保证得到一组一样的随机数。比如:每次都填同一个正整数,其他参数一样的情况下得到的随机数组是一样的,但填0或不填,每次都会不一样。
下面直接给出sklearn上的例子:
>>> import numpy as np
>>> from sklearn.cross_validation import train_test_split
>>> X, y = np.arange(10).reshape((5, 2)), range(5)
>>> X
array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
>>> list(y)
[0, 1, 2, 3, 4]
>>> X_train, X_test, y_train, y_test = train_test_split(
... X, y, test_size=0.33, random_state=42)
...
>>> X_train
array([[4, 5],
[0, 1],
[6, 7]])
>>> y_train
[2, 0, 3]
>>> X_test
array([[2, 3],
[8, 9]])
>>> y_test
[1, 4]
参考资料
[1] http://scikit-learn.org/stable/modules/generated/sklearn.cross_validation.train_test_split.html