Model Evaluation using Accuracy
After fit runs, the model is ready to be evaluated.
Guessing a Label from Similar Examples
Imagine you have a collection of labeled examples, and you are handed a new data point whose label is unknown. How would you guess that label? One simple idea is to look at the examples that are most similar to the new point and let them decide. The k-nearest neighbors classification algorithm works exactly this way. It makes a prediction for a new data point by considering its closest neighbor or neighbors in the training set.
This article follows one complete path through that idea. First you will see how the algorithm uses neighbors to make a prediction. Then you will see how the KNeighborsClassifier class carries out that work through its fit and score methods. Finally you will see how the accuracy of the resulting model is measured on the test set, and what a value such as 97% actually tells you.
Neighbors as the Basis for a Guess
A neighbor is a training example that is close to the new point. The number of neighbors the algorithm considers is the k in the algorithm's name, k-nearest neighbors.
The algorithm does not need to find a rule that separates the classes in advance. There is no step where it first studies the whole training set and writes down a boundary between the labels. Instead, when a new point arrives, the algorithm looks at the training examples nearest to that point and uses their labels to decide the prediction. The work happens at the moment a prediction is needed, by consulting the neighbors.
Tracing One Prediction
Predicting the label of one new point
A new data point arrives with an unknown label. The model uses k = 3, so it considers the three training examples closest to the new point. Suppose those three neighbors have the labels A, A, and B. What does the algorithm use to decide the prediction?
- 1Locate the neighbors
The algorithm looks at the training set and finds the k = 3 examples closest to the new point. These are the neighbors.
- 2Read the neighbor labels
Each neighbor is a labeled training example, so each one carries a label. In this case the labels are A, A, and B.
- 3Let the neighbors decide
The prediction is decided by the labels of the neighbors. Two of the three neighbors point toward A, so the new point is predicted to have label A.
The new point is predicted to have label A, decided by the labels of its three nearest training examples.
KNeighborsClassifier Holds the Work
In Python, the k-nearest neighbors algorithm is implemented in the KNeighborsClassifier class. This class contains two things: the algorithm that builds the model, and the algorithm that makes a prediction using that model. The class therefore plays two roles, one for building and one for predicting.
You do not write the neighbor search yourself. Instead you create an object from the KNeighborsClassifier class and call its methods. The object is the thing that holds the work: once it exists, you use its fit method to build the model and its score method to evaluate that model.
Building the Model and Measuring Accuracy
Following Data Through fit and score
The workflow has a clear order. Training data goes into fit, which produces a model. The test set then goes into score, which reports accuracy. The same model is never asked to be scored on the data it was built from in this workflow; the test set is kept separate so that the reported accuracy describes how the model handles data it has not seen during fitting.
Reading the Matches Behind a Percentage
A percentage such as 97% is easiest to understand as a count of matches. Each test example gets a predicted label from the model, and that predicted label is compared with the true label of the same example. Where the two agree, the prediction counts as correct. Where they disagree, it counts as a mistake. The score is the share of test examples that were predicted correctly.
What a 97% Score Refers To
The source states that the score method computes accuracy on the test set. So when a result reads 97%, that number describes the test set predictions, not the training examples the model was fitted on. It tells you how often the model's predictions matched the true labels for test examples that were kept apart from the training data.
Interpreting 97% on a test set
A model is built with fit on training data and then scored on a test set of 100 examples. The score reports 97%. What does this mean?
- 1Identify the set being scored
The score method is applied to the test set, so the 97% describes the 100 test examples.
- 2Count the matches
97% of 100 examples is 97 examples. The model's predicted label matched the true label for 97 of the test examples.
- 3Recognize the 3 mismatches
The remaining 3 test examples were predicted with a label that did not match the true label.
The model predicted 97 of the 100 unseen test examples correctly. The 97% is a statement about the test set, not about the training set.
What do you think happens?
A model scores 97% using the score method. Was that 97% computed on the training examples used by fit?
Reveal answer
No, on the test set
The score method computes accuracy when applied to the test set data and test set labels. The training data X_train and y_train were used by fit to build the model, so the reported accuracy describes the separate test set.
Mistakes Beginners Make with Accuracy
Common mistake Treating an accuracy number as if it applies to any data the model has ever seen
What it looks likeReading a 97% result as proof that the model will be 97% accurate on every future point, including the training examples themselves.
Why it failsThe score method computes accuracy on the test set. The number describes the test set predictions, so it should be read as the result on that set.
Do this insteadInterpret the reported accuracy as performance on the test set, and keep the test set separate from the data used by fit.
Common mistake Expecting the algorithm to learn a separating rule before any prediction is made
What it looks likeAssuming that kNN first builds a boundary between the classes and then applies it to the new point.
Why it failsThe algorithm does not need to find a rule that separates the classes in advance. It looks at the training examples nearest to the new point when a prediction is needed.
Do this insteadThink of the prediction as coming from the neighbors closest to the new point.
Common mistake Trying to write the neighbor search yourself
Why it failsThe KNeighborsClassifier class already contains the algorithm that builds the model and the algorithm that makes predictions.
Do this insteadCreate an object from KNeighborsClassifier and call its methods, such as fit to build the model and score to evaluate it.
Common mistake Confusing the training data with the test set when reading the result of score
Practicing Accuracy Interpretation
A KNeighborsClassifier object is fitted on X_train and y_train. Its score method is then applied to a test set, and it reports 90%. Explain, in your own words, which examples the 90% describes, and what the 10% that is not included in the accuracy represents.
Hints
- Ask which set score is applied to, according to the source.
- Think about the relationship between the fraction of test predictions that match and the fraction that do not.
- The accuracy is the fraction of test predictions that match the true labels, so the remaining fraction is made up of mismatches.
Key Takeaways
- The k-nearest neighbors algorithm predicts a new data point by considering its closest neighbor or neighbors in the training set, so the nearest examples decide the prediction.
- The KNeighborsClassifier class contains both the algorithm that builds the model and the algorithm that makes predictions; you create an object from it and call its methods.
- The fit method builds the model from X_train and
y_train, and the score method computes accuracy when applied to the test set data and test set labels. - Accuracy is the fraction of test predictions that match the true labels, so a value such as 97% describes the test set, not the training data.