Create train and test partitions
Preparing supervised-learning data for model fitting and evaluation.
Scikit-Learn · Function
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.
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.
sklearn.model_selection.train_test_split(*arrays, test_size=None, train_size=None, random_state=None, shuffle=True, stratify=None)| Name | Type | Description |
|---|---|---|
arrays | sequence of indexables | One or more indexable arrays or matrices with the same length in the first dimension. |
test_size | float or int | Test-set size as a fraction or absolute sample count; default=None. |
train_size | float or int | Training-set size as a fraction or absolute sample count; default=None. |
random_state | int, RandomState instance or None | Seed or random-state object controlling shuffling; default=None. |
shuffle | bool | Whether to shuffle before splitting; default=True. |
stratify | array-like of shape (n_samples,) | Class-label array used for stratified splitting; default=None. |
A list containing the train-test split of each input, with length 2 * len(arrays).
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.
Preparing supervised-learning data for model fitting and evaluation.
Keeping class distributions comparable between subsets.
Repeating experiments with identical data allocation.
Splitting multiple aligned inputs together.
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.
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.
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.
ShuffleSplitsklearn.model_selection