Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Function

train_test_split

Splits aligned arrays or matrices into random training and test subsets. It supports proportional or absolute subset sizes, reproducible shuffling, and optional stratification by class labels.

How it works

The function jointly splits one or more equally sized arrays into corresponding training and test portions. It can shuffle samples, select split sizes by proportion or count, preserve label proportions with stratification, and return the resulting subsets in train-then-test order.

Syntax

sklearn.model_selection.train_test_split(*arrays, test_size=None, train_size=None, random_state=None, shuffle=True, stratify=None)

Parameters

NameTypeDescription
arrayssequence of indexablesOne or more indexable arrays or matrices with the same length in the first dimension.
test_sizefloat or intTest-set size as a fraction or absolute sample count; default=None.
train_sizefloat or intTraining-set size as a fraction or absolute sample count; default=None.
random_stateint, RandomState instance or NoneSeed or random-state object controlling shuffling; default=None.
shuffleboolWhether to shuffle before splitting; default=True.
stratifyarray-like of shape (n_samples,)Class-label array used for stratified splitting; default=None.

Returns

A list containing the train-test split of each input, with length 2 * len(arrays).

Key points

  • Use the function to create training and testing partitions from arrays or matrices.
  • It combines validation, split generation, and applying the same partition to supplied inputs.
  • An integer test_size requests an exact number of test samples.
  • When test_size is omitted, its value is inferred from train_size.
  • random_state governs the randomization performed before partitioning.
  • Providing stratify makes the split use the supplied class labels.

Common mistakes

  • Do not combine stratification with disabled shuffling.
  • A fractional test_size must lie strictly within the documented proportion range.
  • Use an integer random_state when repeatable splits are required.

Example: Reproducible stratified split

from sklearn.model_selection import train_test_split

X = [[1], [2], [3], [4], [5], [6]]
y = [0, 0, 0, 1, 1, 1]
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.33, random_state=42, stratify=y
)
print(len(X_train), len(X_test))

Splits features and labels into aligned training and test subsets while preserving class proportions and making the result reproducible.

When to use it

Create train and test partitions

Preparing supervised-learning data for model fitting and evaluation.

Perform a stratified classification split

Keeping class distributions comparable between subsets.

Build reproducible experiments

Repeating experiments with identical data allocation.

Keep features and targets synchronized

Splitting multiple aligned inputs together.

Interview questions

QWhat does train_test_split return?

It accepts one or more arrays or matrices and returns their corresponding train and test subsets. Inputs must have the same first dimension so samples remain aligned.

Follow-up: How would you split features and labels while preserving class proportions? Pass the labels through stratify, for example stratify=y, and keep shuffling enabled.

QHow are train_size and test_size interpreted?

test_size controls the test subset: a float specifies a proportion, while an integer specifies an absolute sample count. If it is omitted, the function uses the complement of train_size; if both sizes are omitted, the documented default test proportion is 0.25.

Follow-up: What happens when both train_size and test_size are supplied? The two requested subsets determine the allocation, subject to valid sizes and the available samples.

QHow do random_state, shuffle, and stratify interact?

random_state controls the shuffling before splitting. Supplying an integer makes repeated calls produce reproducible output, while shuffle=False prevents shuffling and requires stratify=None.

Follow-up: Why might stratification be useful? It creates a split based on the supplied class-label array, helping retain class representation across subsets.

  • ShuffleSplit
  • sklearn.model_selection

Source: train_test_split — scikit-learn 1.9.1 documentation. Checked 2026-10-15.