Signature
train_test_split(*arrays, test_size=None, random_state=None, stratify=None)What it does
train_test_split shuffles rows and returns two subsets for each input array. A fixed random_state makes the split reproducible. Pass stratify to preserve class proportions in classification problems.
Parameters
arrayssequence of arrays- Features and targets to split together.
test_sizefloat or int, optionaloptional- Fraction or count reserved for testing.
random_stateint, optionaloptional- Seed for a reproducible shuffle.
stratifyarray_like, optionaloptional- Labels whose proportions should be preserved.
Returns
list of arrays
Example
pythontrain_test_split example
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, stratify=y
)Watch out for
- Split before fitting any preprocessing so the test set stays unseen.
- Pass the same random_state when you need to compare models on identical rows.