用train_test_split进行训练集和测试集的随机切分

X_train,X_test, y_train, y_test =sklearn.model_selection.train_test_split(sample_data,sample_target,test_size=0.25, train_size=None,random_state=0,stratify=y_train)
train_data:所要划分的样本特征集
train_target:所要划分的样本类别数据
test_size:测试样本占比,如果是整数的话就是样本的数量。默认是0.25
train_size:训练样本的占比,如果指定了test_size按照test_size比例来分配。
random_state:是随机数的种子。
stratify:为了保持split前类的分布,保证训练集和测试集内类的分布比例相同。

eg:

import numpy as np
from sklearn.model_selection import train_test_split
X, y = np.arange(10).reshape((5, 2)), range(5)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.33, random_state=42)

你可能感兴趣的:(数据处理)