sklearn.model_selection.train_test_split随机划分训练集和测试集
X_train,X_test, y_train, y_test =sklearn.model_selection.train_test_split(train_data,train_target,test_size=0.4, random_state=0,stratify=y_train)
参数:
train_data:所要划分的样本特征集
train_target:所要划分的样本结果集
test_size:样本占比,如果是整数的话就是样本的数量
random_state:是随机数的种子,是该组随机数的编号,在需要重复试验的时候,保证得到一组一样的随机数。每次都填1,其他参数一样的情况下你得到的随机数组是一样的。但填0或不填,每次都会不一样。
stratify是为了保持split前类的分布。比如有100个数据,60个属于A类,40个属于B类。如果train_test_split(..., test_size=0.25, stratify = y_all), 那么split之后数据如下:
training: 75个数据,其中45个属于A类,30个属于B类。
testing: 25个数据,其中15个属于A类,10个属于B类。 </