sklearn.model_selection.
train_test_split
( *arrays , **options ):将数组或矩阵随机划分为训练集和测试集两部分
参数 : *arrays : sequence of indexables with same length / shape[0]
允许输入lists, numpy arrays, scipy-sparse matrices 或者 pandas dataframes.
test_size : 小数、整数或者为空(默认为空)
如果是小数, 需要介于0.0和1.0之间,代表测试集占输入数据集的百分比。如果是整数,代表测试集样本数量。如果为空,这个值自动设置为训练集补集的大小;如果训练集大小也设置为空,则默认为0.25.
train_size : 小数、整数或者为空(默认为空)
如果是小数,需要介于0.0和1.0之间,代表训练集占输入数据集的百分比。如果是整数,代表训练集样本数量。如果为空,这个值自动设置为测试集大小的补集.
random_state : 整数或者 RandomState
伪随机数种子用于随机抽样。
stratify : array-like or None (default is None)
If not None, data is split in a stratified fashion, using this as the class labels.
返回值:
splitting : 列表,长度为两倍数组大小
列表包含划分好的训练集和测试集.
0.16 版本中新特性 : 如果输入的数据稀疏,输出将是一个 scipy.sparse.csr_matrix
. 另外,输出类型与输入类型一致.
示例
>>>
>>> import numpy as np
>>> from sklearn.model_selection 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]