Classification · Part 2
Use nearby observations to classify or predict a number, then choose the number of neighbors through validation.
Polynomial, multiple and logistic regression each start with a chosen formula.
For logistic regression, maximum likelihood estimates the coefficients from labelled data.
Choose a model form with a fixed number of coefficients.
Fit the coefficients on training data. For a new input, evaluate the fitted formula.
Keep the labelled training examples.
For a new input, find its nearest examples and let their labels vote. As the dataset grows, KNN can represent more detailed decision boundaries.
Keep the training inputs and their labels.
Find the K nearest inputs. Their labels vote.
| Chapter | Question we will answer |
|---|---|
| 5.7 · K-Nearest Neighbors | What do nearby examples suggest for a new input? |
| 5.7.1 · Bayesian derivation | Why does a neighbor fraction estimate a class probability? |
| 5.7.2 · K-NN regressor | What if the target is a number? |
| 5.7.3 · Validation | How do we choose K? |
| 5.7.4 · Characteristics | When is the method useful, and what does it cost? |
Each point has two predictors and a class label. The goal is to divide the plane into two regions, one for each class.
Rank training inputs by their distance from xᵢ. Keep the closest K.
Each neighbor contributes one entry, including repeated labels.
For each candidate class c, add 1 for each matching label yⱼ.
Class 0 gets 2 votes. Class 1 gets 3, so ŷᵢ = 1.
With K = 1, the closest stored point supplies the label. The background shows the predicted class at every location.
Square the feature differences, add them, and take the square root.
Move the new input or change K. Read each selected neighbor’s distance and label, then follow the totals to the prediction. All feature values use the same synthetic scale.
With one vote per neighbor, the vote share from the previous example estimates the probability of a class for the new input.
Likelihood, prior, and evidence.
We begin with the input density and estimate it from nearby data.
Bayes’ theorem needs the input density at the query, which we can approximate using the probability mass around it.

The integral adds up the density across 𝓡 to give the shaded area.
The input is uniform from 0 to 10. The whole rectangle has area 1 and width 10, so its height, the density, is 1 ÷ 10 = 0.10 per input unit.
The shaded area (0.20) is the 20% probability of landing between 4 and 6. Since the density is flat, dividing by the interval length V = 2 gives the exact density.
We want to estimate the chance that a new input belongs to a class. Bayes’ theorem gives us a way to do this, using quantities we can estimate from the training data.
First, show why the fraction of training inputs inside a fixed region estimates the probability of landing there.
Evidence is the local density across all inputs.
Likelihood is the local density within the chosen class.
Prior is the class’s overall frequency.
Put those estimates into Bayes’ theorem. The shared region size cancels, along with the dataset counts, leaving a fraction of the neighbors.
Choose K, move the query and choose a class. Use the same circle to estimate both densities, then substitute the numbers into Bayes’ theorem.
The same nearest neighbors can predict a number by averaging their target values.
Use their numerical targets.
Move the new input x and choose K. Select neighbors by horizontal input distance only, then add their numerical targets y and divide by K.
In the regression demo, setting K = n makes every query average the same training targets.
Regression predicts the global mean. Classification predicts the global majority class.
At the other extreme, K = 1 makes the prediction depend entirely on the closest stored observation.
A different sample can change the nearest target. This highly local rule typically has higher variance.
The last two slides show how K changes the prediction, so we need a fair way to compare candidate values.
Try several values of K and keep the one with the best test score.
Choosing K selects a prediction rule. Compare candidates on validation data and reserve test data for the final assessment.
Store the training examples.
Compare candidates and choose K.
Evaluate the chosen model.
Fraction of correct labels. Higher is better.
Typical size of the error in the target’s units. Lower is better.
Compare K = 1, 5 and 15. Click a validation case to see how its neighbors voted, then lock your choice to reveal the test score.
A parametric model can work well when its chosen form fits the relationship.
KNN uses local examples and can adapt to varied decision boundaries or regression shapes.
| For our two-feature example | Logistic regression | KNN |
|---|---|---|
| What is stored for prediction? | 3 fitted coefficients: intercept + 2 slopes | 50 input pairs and their 50 labels |
| What happens for one new input? | Evaluate the fitted probability formula | Compare input distances and combine nearby labels |
| Idea | What to explain |
|---|---|
| KNN classification | Nearby labels vote, optionally with distance weights. |
| Bayesian derivation | Local density estimates lead to the neighbor fraction . |
| KNN regression | Average the nearest observations’ numerical targets. |
| Validation | Choose K before using the final test observations. |
Lecture 12 · Classification · Part 2