EXERCISE
1Train the model on 80% of data, test on 20%, and compare R² scores — if both are similar, your model generalises well.
Save
Think of training R² as your practice exam score and testing R² as your real exam score. If practice is 95% but the real exam is 40%, you memorised instead of learning.
The complete train/test workflow:
import numpy
from sklearn.metrics import r2_score
numpy.random.seed(2)
x = numpy.random.normal(3, 1, 100)
y = numpy.random.normal(150, 40, 100) / x
# Split
train_x = x[:80]
train_y = y[:80]
test_x = x[80:]
test_y = y[80:]
# Train a polynomial model (degree 4)
mymodel = numpy.poly1d(numpy.polyfit(train_x, train_y, 4))
# R² on TRAINING data
train_r2 = r2_score(train_y, mymodel(train_x))
print(f"Train R²: {round(train_r2, 4)}")
# Output: Train R²: 0.7988
# R² on TESTING data
test_r2 = r2_score(test_y, mymodel(test_x))
print(f"Test R²: {round(test_r2, 4)}")
# Output: Test R²: 0.8086
Interpreting the results:
| Train R² | Test R² | Verdict |
|---|---|---|
| 0.80 | 0.81 | ✅ Great — model generalises well |
| 0.99 | 0.30 | ❌ — memorised |
| 0.40 | 0.38 | ⚠️ — model is too simple |
> 💡 Key Insight: The ideal scenario is when train R² and test R² are CLOSE to each other AND both are high. If train is much higher than test, your model is overfitting. If both are low, your model is underfitting. This gap between train and test performance is the essence of the — the most important concept in all of ML.