Dataset and Model Evaluation
A dataset is split into a training set, which builds the model, and a test set, which evaluates how well the model generalizes to unseen data.
Why Memorizing Is Not Enough
Building a machine learning model is only half of the job. A model can produce answers, but the practical question is how well it will perform on data it has never seen. A model that has memorized its training examples may look impressive on those same examples and still fail when it meets something new. This article walks through the standard approach: hold part of the data back, build the model from the rest, and then measure performance on the held-back portion.
What do you think happens?
Suppose a model gets every example it was trained on exactly right. Does that tell you it will make good predictions on new examples?
Reveal answer
No, it may have memorized those examples rather than learned a pattern that carries over
The source warns that a model that has memorized its training examples may look impressive on those same examples and still fail on something new. Performance on the training data alone cannot show how the model handles unseen data, which is why a separate held-back portion is needed.
Splitting the Dataset in Two
The dataset is divided into two parts that play different roles. The training set is used to build the model. The test set is kept aside and used only to evaluate how well the model generalizes to new, previously unseen data. Each example belongs to one part or the other, so the two roles stay separate.
The diagram shows the split as a one-way division: the dataset feeds two separate destinations. Notice that the test set does not appear on the path that builds the model. The model is constructed from training examples alone, and the test examples enter only at the evaluation step.
Why the Test Set Must Stay Separate
The separation is what allows the test set to act as an honest check. Nothing in the test set is used to construct the model. If test examples leaked into building the model, the resulting score would describe data the model had already partly learned from, and it would no longer be a fair measure of performance on unseen inputs.
How k-Nearest Neighbors Predicts
The source uses the k-nearest neighbors classification algorithm as its example model. It makes a prediction for a new data point by looking at its closest neighbor or neighbors in the training set. The prediction therefore depends on which known examples sit nearest to the new point. Because the algorithm is a classification method, its output is a class label rather than a continuous number.
Tracing one prediction, step by step (generated example)
Training points lie on a line. Each has a value and a label: 1.0 (A), 1.5 (A), 2.0 (A), 5.0 (B), 6.0 (B), 7.0 (B). A new point with value 4.2 arrives, and k is set to 3. What class does the model predict?
- 1Start with the training set only
The new point is compared only with the training points. The test set plays no part in making this prediction.
- 2Measure distances
The distances from 4.2 are: 1.0 is 3.2 away, 1.5 is 2.7 away, 2.0 is 2.2 away, 5.0 is 0.8 away, 6.0 is 1.8 away, and 7.0 is 2.8 away.
- 3Select the 3 nearest
The three closest training points are 5.0 (label B, distance 0.8), 6.0 (label B, distance 1.8), and 2.0 (label A, distance 2.2).
- 4Combine the neighbors' labels
Two of the three neighbors are labeled B and one is labeled A, so B is the most common label among the neighbors.
- 5Output the prediction
The model predicts class B for the new point at 4.2.
The predicted class for the point at 4.2 is B.
Following the Four-Step Workflow
The source describes the workflow in four moves: create the classifier object and set its parameters, build the model by calling fit with the training data and training outputs, then call score with the test set data and test set labels. The source names the parameters X_train and y_train for the training inputs and outputs. The listing below is a generated illustration of that same workflow in Python. It is not the source's own listing, and the parameter value shown is a choice made for this illustration. Assume the data have already been divided into the training and test parts.
n_neighbors=3 sets k to 3, the number of closest training points consulted for each prediction- fit uses only the training inputs and training outputs
- score uses the test set data and test set labels, which were kept out of fit
Reading an Accuracy Score
Accuracy is the fraction of test examples the model classifies correctly. The source treats a result of about 97 percent as meaning the model is correct 97 percent of the time on the test set. Written as a ratio, the definition is shown below.
Computing accuracy on a test set (generated example)
A test set contains 50 held-back examples. The model classifies 47 of them correctly. What is the accuracy on this test set?
- 1Identify the counts
There are 47 correct predictions out of 50 test examples.
- 2Apply the ratio
Accuracy is $\frac{47}{50}$.
- 3Convert to a fraction of 1 and a percent
$\frac{47}{50} = 0.94$, which is 94 percent of the test examples.
The accuracy on this test set is 94 percent, meaning the model was correct on 94 percent of these held-back examples.
The left column describes what the number measures: the proportion of correct predictions on held-out examples. The right column names what the number does not promise. A score on one test set describes that test set, and the source frames any expectation about future correctness as an estimate carried forward from it.
From Test Result to Trust in New Predictions
Once the test set result looks acceptable, the model can be applied to new data. In the source's example, the new data are new flower measurements. The source reports that the evaluation gave enough confidence to apply the model to these measurements and to expect it to be correct about 97 percent of the time. The reasoning is that the test set was a stand-in for unseen data, so its accuracy is the best available estimate of accuracy on future inputs.
This is why a model that performs well on a test set can be trusted, within limits, on new predictions. The test examples were never used to build the model, so their results are not inflated by memorization. The trust rests on the assumption that future inputs resemble the test inputs.
Mistakes Beginners Make with Evaluation
Common mistake Scoring the model on the training examples and calling that the test result
What it looks likeCalling score with the same data that was passed to fit and reporting that number as evidence for new data.
Why it failsThe model was built from those examples, so the score describes data it has already partly learned from rather than unseen data.
Do this insteadCompute accuracy on the held-back test set only, which was kept out of the fit step.
Common mistake Letting test examples influence how the model is built
Why it failsOnce test examples shape the model, the test set no longer acts as an honest check of generalization.
Do this insteadKeep the test set separate from every step that builds the model.
Common mistake Treating an accuracy score as a promise about every future prediction
What it looks likeReading a 97 percent test accuracy as meaning the model will be right on exactly 97 out of every 100 future inputs, no matter what they look like.
Why it failsThe score is an estimate computed on one test set, and the source frames it as confidence to proceed rather than a fixed property of future predictions.
Do this insteadUse the score as the best available estimate, and watch for cases where new data differ from the test data.
Common mistake Expecting a k-nearest neighbors prediction to depend on the test set
Why it failsThe new point is compared only with training examples during prediction, so the test set plays no part in producing that prediction.
Practice Tracing a Split and a Prediction
A dataset has 100 labeled examples. Suppose 80 are placed in the training set and 20 are held back as the test set. The model is built, and it classifies 18 of the 20 test examples correctly. Which examples were used to build the model, what is the accuracy on the test set, and what does that accuracy estimate about new data?
Hints
- Ask which subset the fit step is allowed to see, and which subset the score step uses.
- Accuracy is the number of correct test predictions divided by the total number of test examples.
- Remember that the source treats the test accuracy as an estimate for unseen data, not a guarantee.
Key Takeaways
- A dataset is split into a training set, which builds the model, and a test set, which is held back to evaluate how well the model generalizes to unseen data.
- The k-nearest neighbors classifier predicts the class of a new point from its closest neighbors in the training set, so the test set plays no part in that prediction.
- Accuracy is the fraction of test examples classified correctly; it is an estimate for that test set and does not guarantee correctness on every future prediction.
- A model that performs well on a test set can be trusted for new predictions, within limits, because the test set stood in for unseen data and was kept separate from model building.