Evaluation Metrics for Supervised Models
A model evaluated only on the data it was trained on tells you about fit, not about generalization to new data.
Why a Training Score Misleads
Once a supervised model has been fitted, it is tempting to ask it how well it did on the same data it learned from, and to report that number as the result. The source material for this topic makes a precise point about why that number is not enough: a model evaluated only on the data it was trained on tells you about fit, not about generalization to new data. Fit describes how closely the model matches the rows it has already seen. Generalization describes how well it predicts rows it has never seen. Those are different questions, and a supervised model is usually built to answer the second one.
What do you think happens?
A model scores very well on the rows it was trained on. Does that guarantee it will predict well on new rows it has never seen?
Reveal answer
No, a high training score only shows how closely the model fits the data it has already seen
The source is explicit that a model evaluated only on its training data tells you about fit rather than generalization. A model that fits its training set very closely may still make poor predictions on data it has not observed, so the training score is not the quantity that matters.
Tracing a Train-Test Evaluation
The standard remedy is to hold part of the data back before any fitting happens. A train-test evaluation has three distinct moves, and each has a specific job. First, the dataset is divided into a training set and a test set by train_test_split. Second, the fit method builds a model using only the training set. Third, the score method measures how the fitted model performs on the test set. The number you see at the end depends on all three moves, but only the third one touches the test data. Read the pipeline below from top to bottom and notice which steps touch the training rows and which touch the test rows.
- 1Line 1 to 3 import the three tools: a data generator, the splitting function, and the classifier.
- 2Line 5 creates a synthetic dataset of 200 rows with 5 input features and a known label for each row.
- 3Line 7 to 9 divide the rows into a training set (X_train,
y_train) and a held-back test set (X_test,y_test). Therandom_statevalue makes the split repeatable. - 4Line 11 creates an untrained logistic regression classifier.
- 5Line 12 fits the model using only the training rows. The test rows have not been touched yet.
- 6Line 14 scores the fitted model on the test rows and prints one number. This is the only line where the test data reaches the model.
make_classificationcreates a synthetic dataset, so the rows are generated rather than collected from the worldtrain_test_splitholds back 25 percent of the rows as the test set and keeps the rest for training- LogisticRegression is the classifier that will be trained on the training rows only
- fit receives only X_train and
y_train, so the model is built from the training set alone - score receives X_test and
y_test, so the test rows are used only at this final step
Following one evaluation from split to score
A dataset of labeled rows must be used to estimate how a classifier will perform on rows it has not seen. Trace which rows each step is allowed to use.
- 1Divide the data
train_test_splitseparates the rows into a training part and a test part. Neither part is used for anything yet except as a container. - 2Fit the model
fit receives only the training part. The model's internal settings are chosen to match those rows. The test part is not passed in.
- 3Score the model
score receives the test part together with its true labels. The model predicts for those held-back rows and the fraction of correct predictions is returned.
- 4Read the result
The single number reflects performance on rows that played no role in building the model, which is why it is the estimate of generalization we want.
The test score is a held-out estimate of how the model will perform on new data, and it is valid only because the test rows never shaped the fitted model.
Overfitting Shows as a Gap
The reason for splitting is that the training score is not the quantity we care about. The useful comparison is between the score on training rows and the score on test rows. A model that has memorized its training data will look excellent on those rows and noticeably weaker on the held-back rows, which produces a large gap between the two numbers. A model that generalizes shows training and test scores that are much closer together. The gap is therefore a diagnostic signal: it tells you whether the model's apparent quality survives contact with data it did not see.
To check the gap in the pipeline above, you can score the same fitted model on its own training rows as well as on the test rows. Both numbers come from the same model; only the rows differ. If the training number is high and the test number is much lower, the model fits its training data more closely than it generalizes. The source treats this as the central reason the held-back test set exists.
A learner fits a model, calls score on the same rows that were used for fit, and reports the result as the model's performance. Nothing in that number reveals whether the model would predict new rows well. The corrected habit is to keep one set of rows aside, score on those, and treat any training-set score only as a sign of fit.
Cross-Validation Rotates the Test Fold
A single train-test split gives one estimate of generalization performance, and that estimate depends on which rows happened to land in the test set. A different random split could give a different number. Cross-validation is introduced as a more robust way to assess generalization performance. Instead of relying on one split, the data is divided into several parts, and each part takes a turn as the held-out evaluation data while the others are used for training. Every row therefore gets to serve as test data exactly once across the rounds.
Reading the rounds across the diagram shows the rotation. Each round holds out a different fold, trains on the remaining folds, and produces its own score. Because each fold appears as the test set in exactly one round, no single lucky or unlucky split dominates the estimate. The source introduces cross-validation at this level of motivation, so the key idea to carry forward is the rotation itself: the held-out role moves from part to part until every part has been tested.
- 1Line 1 to 3 import the data generator, the cross-validation helper, and the classifier.
- 2Line 5 creates the same synthetic dataset used in the train-test example.
- 3Line 7 creates an untrained classifier. Cross-validation will create fresh fitted copies internally for each round.
- 4Line 8 runs four rounds. In each round one fold is held out for scoring and the other three are used for fitting.
- 5Line 9 prints one score per round.
- 6Line 10 prints the average of those scores as a summary estimate.
cross_val_scoreruns the rotation automatically, fitting and scoring once per fold- cv=4 asks for four folds, matching the four rounds in the diagram
- The result is one score per round, so the spread of the list shows how sensitive the estimate is to the split
Grid Search Over Parameter Choices
Every supervised model has parameter settings that are chosen before fitting, and the choice affects how well the model generalizes. Grid search is presented as an effective method for adjusting these parameters in supervised models to reach the best generalization performance. The idea connects directly to everything above. The parameter choice should be judged by how it performs on held-out data, not by how it performs on the data the model was fitted to. Evaluation tells you whether a setting works, and parameter selection uses that evaluation to pick a setting.
In practice, grid search tries a list of candidate values for each setting, forms every combination of those values, and evaluates each combination with cross-validation. The combination with the best cross-validated result is selected. The procedure is simple to state, but it only works as intended when every candidate is judged on rows that were not used to fit that candidate, which is exactly what cross-validation provides.
- 1Line 5 defines the grid: three candidate values for the setting C.
- 2Line 6 builds the search object, pairing the classifier with the grid and a four-fold cross-validation scheme.
- 3Line 7 runs the search. Each of the three candidates is cross-validated and the results are compared.
- 4Line 9 prints the candidate that achieved the best cross-validated score.
- 5Line 10 prints that best cross-validated score.
param_gridlists the candidate values for one setting, C, which controls regularization strength in logistic regression- cv=4 means every candidate is evaluated with four-fold cross-validation
- best_params_ reports the winning candidate and best_score_ reports its cross-validated score
Default Metrics Hide Error Types
The score method provides default measures. For classification it reports the fraction of correctly classified samples, which is accuracy. For regression the default measure is R squared. Treat the default score as a starting point rather than the complete verdict on a model. The source explicitly notes that the chapter goes beyond these default measures to discuss other ways of evaluating classification and regression performance.
A single number can hide differences that matter for a particular problem. Two classifiers with the same accuracy may behave very differently, so the choice of evaluation measure should match what you need the model to do. Accuracy counts all correct predictions equally. It does not tell you which kind of mistake the model makes, or whether those mistakes are acceptable for the task at hand. That is why the default score should be read together with an understanding of the problem, rather than accepted as the whole story.
Common mistake Reporting the training score as the model's performance
Why it failsA training score describes fit to rows the model has already seen, not generalization to new data.
Do this insteadHold rows back before fitting and report the score on those rows.
Common mistake Passing the test rows into fit
What it looks likeCalling fit on the full dataset before splitting, or fitting again after looking at test results
Why it failsThe test rows then shape the model, so the final score is no longer a fair check of generalization.
Do this insteadPass only the training rows to fit and touch the test rows only in score.
Common mistake Treating one split's score as the final answer
Why it failsA single split depends on which rows landed in the test set, so the estimate can shift with the split.
Do this insteadUse cross-validation to rotate the held-out fold and look at the spread of scores.
Common mistake Assuming equal accuracy means two models are equally suitable
Common mistake Choosing a grid search winner and then reporting its score as an untouched final result
Trace the Evaluation Yourself
A colleague fits a logistic regression on all 200 rows, then reports the score on those same 200 rows as the model's performance. Using the pipeline from this article, describe which step is wrong and rewrite the evaluation so that the reported score comes only from rows the model never used during fitting.
Hints
- Identify which rows fit receives in your colleague's version and which rows should be held back.
- Use
train_test_splitbefore fit, and call fit with the training rows only. - Make sure the final score call receives rows that fit never saw.
Suppose two cross-validation runs on the same data give very different average scores. Explain, using the idea of rotating the held-out fold, why one split alone would not reveal this sensitivity, and what you would look at in the list of per-round scores.
Hints
- Think about what a single split would show compared with four separate rounds.
- Look at the spread of the per-round scores, not only their average.
Key Takeaways
- A score computed on the data a model was trained on measures fit, not generalization to new, unseen data.
- A train-test evaluation splits the data, fits only on the training part, and scores only on the held-back test part, so the test rows never shape the model.
- Cross-validation rotates the held-out fold across several rounds, giving a more robust estimate than one split, and grid search uses such estimates to choose parameter settings.
- Default measures such as accuracy for classification and R squared for regression are a starting point; a single number can hide differences that matter for the problem.