liveBook · Manning

Train_test_split

As explained in Grokking Statistics

train_test_split is a scikit-learn function that randomly partitions arrays or datasets into training and testing subsets.

A model can memorize the examples it receives, yet fail on tomorrow’s data. train_test_split randomly divides arrays into a training dataset for fitting and a test dataset for evaluating unseen cases. section 8.5 section 8.6

A practical split follows this pattern:

  • Reserve part of the data for testing; a common ratio uses two-thirds for training and one-third for testing. section 8.6
  • Pass the training portion to the model’s fit method, keeping the test portion out of the training process. section 8.6
  • Use the fitted model to predict the test labels, then compare predictions with actual labels to calculate accuracy. section 8.6
  • Treat the result as an estimate of generalization, not automatic proof; cross-validation is preferred when fitting is not computationally expensive. section 8.11

Predicting toddler meltdowns

For a binary meltdown example, train_test_split(x, y, test_size=1/3) randomly creates X_train, X_test, Y_train, and Y_test. LogisticRegression fits the training arrays, predicts classes on X_test, and compares those predictions with Y_test. The same pattern works with linear regression, logistic regression, or neural networks. section 8.6

import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

x = np.array([0.1, 0.5, 0.7, 1.2, 1.2, 1.5, 1.6, 1.9, 2.5, 2.5,
             2.7, 3.2, 3.3, 3.7, 3.8, 4.1, 4.5, 4.5, 4.5, 5.0,
             5.1, 5.6, 5.7, 6.2, 6.3]) \
   .reshape(-1, 1)  #1

y = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
             0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0,
             1.0, 1.0, 1.0, 1.0, 1.0])  #2

X_train, X_test, Y_train, Y_test = \
   train_test_split(x, y, test_size=1/3)  #3

model = LogisticRegression(penalty=None)  #4
model.fit(X_train, Y_train)  #4

predictions = model.predict(X_test)  #5

print(predictions)  #6

accuracy = np.sum(predictions == Y_test) / len(Y_test)  #7
print(f"Accuracy: {accuracy:.4f}")  #7

Inspect the complete NumPy and scikit-learn listing: it creates the arrays, performs the split, fits logistic regression, predicts test classes, and prints accuracy. section 8.6

How Grokking Statistics covers it

How deep it goes
Defines the split, gives a common proportion, and works through prediction and accuracy without exploring statistical theory.
Examples
Demonstrates toddler-meltdown classification with NumPy arrays, logistic regression, held-out predictions, and repeated split accuracy.
What it assumes
Assumes labeled arrays and a fitted predictive model, while explaining training, testing, unseen data, and generalization from practical examples.
What you can do after
Apply it via scikit-learn to split labeled arrays, train a classifier, predict held-out classes, and calculate accuracy.
How it connects
Connects train_test_split with cross-validation, overfitting, logistic regression, classification, prediction, and model generalization.

Also explained in

Compare how 2 books explain train_test_split →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Statistics. The section links point to the parts of the book it draws on.