# Splitting data in scikit-learn

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.

• Just split it twice.
– Emre
Aug 20 '16 at 19:32
• Wouldn't that be somewhat ugly? I mean there must be a way in scikit-learn for that. Aug 20 '16 at 19:33

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)

list(y)

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

y_train
X_test

y_test

train_test_split(y, shuffle=False)