I know how to split the dataset into train and test sets using train_test_split but is there any way that I can split the dataset into three different sets, i.e., "Train set", "Test set" and "Validation Set". An example should be enough.

  • 3
    $\begingroup$ Just split it twice. $\endgroup$
    – Emre
    Aug 20, 2016 at 19:32
  • 1
    $\begingroup$ Wouldn't that be somewhat ugly? I mean there must be a way in scikit-learn for that. $\endgroup$
    – enterML
    Aug 20, 2016 at 19:33

2 Answers 2


train_test_split is just a utility function around ShuffleSplit, which on its turn just randomly assigns each sample to either train or test, taking the desired probability into account. You can do that however you'd like, and there's no real reason to use that specific function.

Its not too hard to come up with some code that does that for three values or N values, if you rather avoid calling train_test_split twice.


Here you go.

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)



train_test_split(y, shuffle=False)

Check this link.


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.