Data, Training and Testing
Read a little, play a little. No scary maths, and no rush.
Split or it didn't happen
A model that scores 99% on the data it trained on may score 60% on new data — it memorized instead of learning. The defense is splitting data into three roles: train (learn from this, ~70%), validation (tune settings against this, ~15%), and test (touch once, at the very end, ~15%). Shuffle first, keep class ratios similar (stratify), and if your data has time in it, split by time — random shuffling a time series lets the model peek at the future.
Underfit, overfit, just right
- Underfit (both train and test bad): the model is too simple — bigger model, better features.
- Overfit (train great, test bad): the model memorized quirks — more data, dropout, early stopping, simpler model.
- Just right: small gap, both good. That gap is the generalization report.
Plot both curves as training progresses; the moment they diverge, memorize the shape — you'll recognize it for the rest of your career.
Leakage horror stories
The silent killer: a feature containing the answer. Predicting product returns with a "refund_issued?" column gets 99% accuracy and teaches nothing — at prediction time, that column doesn't exist yet. Always ask: "Would I know this value when actually predicting?" If not, it's leakage wearing a costume. Duplicates straddling train/test and preprocessing fit on full data (instead of train-only) are the same crime.
A worked example: predicting churn
A subscription app has 10,000 customers, and 8% of them cancel each year. You want a model that flags who is likely to leave.
- Stratify the split. With only 8% positives, a random split could leave the test set with too few customers who cancel to measure anything. Stratifying keeps roughly 8% in every split.
- Beware the accuracy trap. A model that predicts "stays" for everyone is 92% accurate and completely useless. For rare outcomes, look at how many real cancellations it catches (recall) and how many of its warnings were right (precision).
- Hunt for leakage. A column called
cancellation_reasonis only filled in after someone cancels. It would make the model look brilliant and fail on day one. - Respect time. If each row is a monthly snapshot, train on earlier months and test on later ones, because that is how the model will be used.
When data is small: cross-validation
With only a few hundred rows, a single 15% validation slice is too small to trust; one lucky split can flatter a bad model. K-fold cross-validation fixes this. Split the training data into, say, five folds, train five times with a different fold held out each time, and average the five scores. Every row gets used for validation once, so the estimate is steadier.
Cross-validation replaces the validation set, not the test set. Keep a final test set aside and score it once at the very end, exactly as before.
Remember this
- Train learns, validation tunes, test judges — once, at the end.
- The train/test gap diagnoses everything; leakage fakes everything.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
End of AI Foundations (No Math Fear)
Take the section checkpoint
A short mixed quiz on all 4 chapters, to check you are ready to move on.
Related chapters
Spotted a mistake or want a topic covered? Report an issue