sklearn.model_selection.train_test_split
将数组或矩阵切分成随机训练和测试子集。
参数列表:
-
1 *arrays : sequence of indexables with same length / shape[0]
行数(样本数)/shape[0]取值相同的
-
2 test_size : float,int or None,optional(default=None)
如果为空,那么该值是设置为0.25
-
3 train_size : float, int, or None, (default=None)
如果为空,那么该值为1-test_size
-
4 random_state : int, RandomState instance or None, optional (default=None)
该参数可以取值为int值,如果是int值,那么此时int值作为随机数生成种子;如果是RandomState实例,那么这个就是随机数生成器,用于生成随机数。如果为None,那么默认使用np.random的Random State 实例
-
5 shuffle : boolean, optional (default=True)
在进行数据拆分前是否suffle。默认是进行suffle,如果设置了False,那么stratify一定为None。
-
6 stratify : array-like or None (default=None)
如果不是None,则以分层方法时分割数据,并将其作为类标签。
返回值:
splitting : list, length=2 * len(arrays)
List containing train-test split of inputs
示例
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]