I have a raw dataset of Images I got from Kaggle, It has been classified already, but I want to randomly split the information in a 80:20 ratio between train and test, problem is since all information is classified, I'd love to keep that folder classification in my new train set while all is mixed in the test folder. I cant do it myself since I want it to be randomly split, how do I do this?


1 Answer 1


From this dataset, you should prepare new variables namely, X and y; where X = feature matrix and y = class variable. Now, use the following code to split the data into train and test dataset of desired proportion -

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

The test_size helps you prepare a train-test split of 80:20; and
the random_state is the seed value for randomisation.
Each different seed shall yield a different randomised split.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.