简体   繁体   中英

How to split data by using train_test_split in Python Numpy into train, test and validation data set? The split should not random

I want to split data category wise into train, test and validation set. For example: if we have 3 categories positive, negative and neutral in the dataset. The positive category split into train, test, and validation. And the same with the other two categories. The splitting ratio is 80% of the data is for training and 20% for testing. From 80% of the training data, split 10% for the validation data. But the most important the split data should not random.

You can use the stratify parameter to do this:

For example: If you were to use Iris dataset to do this.

from sklearn import cross_validation, datasets 

X = iris.data[:,:2]
y = iris.target

cross_validation.train_test_split(X,y,stratify=y)

You can read more here: https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM