train_test_split()

noun

say it: “TRAYN TEST SPLIT” /ˈtreɪn ˈtɛst ˈsplɪt/

A scikit-learn function that randomly divides a table's rows into a part to train a model on and a part kept back to test it.

What does train_test_split() do?

train_test_split() shuffles the rows and splits them into a training part and a test part, so the model is scored on rows it never saw. test_size=0.25 holds back a quarter; random_state makes the shuffle the same every run.

from sklearn.model_selection import train_test_split
temps = [[60], [65], [70], [75], [80], [85], [90], [95]]
cups = [20, 30, 40, 50, 60, 70, 80, 90]
X_train, X_test, y_train, y_test = train_test_split(temps, cups, test_size=0.25, random_state=0)
print(len(X_train), len(X_test))   # 6 2

In a sentence

  • “train_test_split() held back a quarter of the home sales for testing.”
  • “Passing random_state to train_test_split() put the same rows in the test set every run.”

Level: Apprentice · More Python terms at this level

Updated