The difference between a model’s performance on the data it was trained on versus its performance on new, unseen data. It measures how well the model’s learned patterns apply to the real world.
The gap between how well a student does on the homework (training data) versus the actual final exam (real-world data). If they memorized the homework answers, their generalization error is huge.
Generalization error (or out-of-sample error) is the ultimate metric of a machine learning model’s success. It is composed of three parts: Bias (error from overly simplistic assumptions), Variance (error from sensitivity to small fluctuations in the training set), and Irreducible Error (noise in the data). The goal of ML is to minimize the sum of bias and variance.
A stock trading bot that makes 100% profit on historical data from 2010-2020 (training), but loses all its money when deployed in 2024 (unseen data). The historical profit was an illusion; the generalization error was massive.
# Conceptual: Calculating the generalization gap
from sklearn.metrics import accuracy_score
# Model predictions
train_preds = model.predict(X_train)
test_preds = model.predict(X_test)
train_acc = accuracy_score(y_train, train_preds)
test_acc = accuracy_score(y_test, test_preds)
# The generalization gap (error)
gen_gap = train_acc - test_acc
print(f"Training Accuracy: {train_acc:.3f}")
print(f"Test Accuracy (Real World): {test_acc:.3f}")
print(f"Generalization Gap: {gen_gap:.3f}")
# A large gap indicates overfitting.