Stratified Split

noun

say it: “STRAT-ih-fyd SPLIT” /ˈstrætəˌfaɪd ˈsplɪt/

A way of dividing data into parts so that each category keeps the same share in every part.

What is a stratified split?

A stratified split keeps each class's share the same in the training and test parts, such as 20% spam in both. With a rare class, a plain random split can leave the test set with too few examples of it, or none. In scikit-learn, pass stratify=y to train_test_split().

from sklearn.model_selection import train_test_split
emails = [[n] for n in range(20)]
spam = [1, 1, 1, 1] + [0] * 16    # 20% spam
_, _, y_train, y_test = train_test_split(emails, spam, test_size=0.25, stratify=spam, random_state=0)
print(sum(y_train), sum(y_test))   # 3 1: 20% spam in each part

In a sentence

  • “The stratified split put exactly one spam email in the test set of five.”
  • “With only 2% fraud, a stratified split made sure the test set had some fraud to find.”

Level: Journeyman · More AI Basics terms at this level

Updated