Vocabulary for this course

The terms the chapters assume, in plain words. You can open this from any chapter.

accuracy
The share of predictions that are correct, counting every class the same.
additive
Built by summing separate contributions, so each part’s effect can be read on its own.
baseline
A reference score from a rule so plain it needs no learning, such as always naming the most common answer.
features
The input columns for one example — the numbers a model is allowed to read before it predicts.
fitting
Adjusting a model’s numbers so its predictions get closer to the answers in the training data.
generalisation
How well a model performs on examples it never saw while being fitted.
hypothesis class
The family of shapes a model is allowed to use, such as a straight line or a tree.
label
The correct answer for one example — what you want the model to predict.
loss
A single number measuring how wrong a prediction is; smaller means better.
metric
A single number used to judge predictions, such as the share answered correctly.
noise
Random variation in data that carries no real signal, which a model can mistake for a pattern.
optimiser
The procedure that changes a model’s numbers to make its loss smaller.
overfitting
When a model memorises accidental details of its training examples and does worse on new ones.
parameters
The numbers inside a model that fitting adjusts, as opposed to the inputs it reads.
regularisation
Any penalty or limit that discourages a model from chasing noise in its training examples. The aim is better performance on new data.
supervised learning
A setting where each example carries the answer you want the model to predict.
test set
Examples kept out of fitting, used to estimate how the model will do on data it has not seen.
training set
The examples a model is fitted on; a score there is not an estimate of performance on new data.
weights
The numbers a model multiplies each input by before adding the results together.

Classical machine learning

Nearest neighbours

The simplest learner stores the data and answers by looking at the closest examples. Distance, k and scaling decide everything it does.

Part 3 of 10

Beginner14 min readBack to Courses

No parameters, only examples

A nearest-neighbour classifier does not fit a line or a tree. It stores the training rows. At prediction time it finds the k closest rows to the query and takes a vote among their labels. Training is almost free. The cost moves to inference, the step that answers a query. There every prediction compares the query with the stored data.

The decision boundary is implied by the points, not written down as an equation. The boundary is the set of points where the prediction changes. With one neighbour, it is the set of places where the closest point changes label. That boundary can be jagged. It can wrap around a single noisy example. The model has no global view, only local evidence.

The method is called lazy because it puts off the work until prediction. That helps when a dataset changes often, since adding a row needs no refit. It hurts when the dataset is large or the latency budget is tight. The method is also a useful baseline. If a nearest-neighbour model with a sensible distance does well, a more complex model has to justify itself.

Distance is a choice

Euclidean distance is the default. It is the square root of the sum of squared coordinate differences. Manhattan distance adds the absolute differences instead. It behaves differently when movement happens along the axes. Cosine distance compares directions. It is common for text vectors, where length often reflects document size rather than meaning.

The units matter more than the formula. A feature measured in thousands dominates one measured between zero and one. The Euclidean distance then measures the large feature alone. Standardising each column to zero mean and unit variance puts them on a similar footing. Fit the scaler on the training data only, for the reason the previous chapter gave.

Cosine distance is not a drop-in replacement for Euclidean distance. It ignores magnitude. That is right when the direction of a vector is the signal, and wrong when the size is. In text, a long document and a short one can point the same way. In sensor data, the magnitude may be the measurement. The metric should follow the meaning of the columns, not the convenience of the library.

The classifier in scikit-learn

from sklearn.neighbors import KNeighborsClassifier
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

model = make_pipeline(
    StandardScaler(),
    KNeighborsClassifier(n_neighbors=5, metric="euclidean"),
)
model.fit(X_train, y_train)
print(model.predict(X_test))

Vote among the nearest

Run the distance table, then add k=4 and explain why the vote is tied.

Output
Click "Run" to execute your code

Choosing k

A small k follows the data closely. One neighbour can be swung by a single mislabelled point. The boundary then has high variance, meaning it changes a lot with a new sample. A large k smooths the boundary and lowers that variance. Eventually it starts averaging across regions that are genuinely different. The bias-variance trade is visible right in the choice of k.

For a two-class problem, an odd k avoids a tied vote. There is no universal value. Tune it on validation data, and report the curve rather than a single winning number.

Distance weighting is a middle ground. Instead of giving every neighbour one vote, weight each by the inverse of its distance. A close point then counts more than a far one. That softens the effect of a large k without ignoring local evidence. It also adds a new choice: how to handle an exact match, where the weight would be infinite. The usual answer is a small constant added to the denominator.

Scale before you compare. If one column ranges over thousands and another over fractions, the distance is a measurement of the first column with a small correction from the second.

When it fails

In high dimensions, distances lose contrast. The nearest and farthest points become almost equally far, so “nearest” stops meaning much. Irrelevant features add noise to every distance. A single uninformative column can drown a useful one. Feature selection is not optional when the row is wide.

The method also stores the training data. That can be a privacy problem and a memory problem. Inference cost grows with the number of stored rows. A missing value has no natural distance unless you define one. These are reasons to try a linear model or a tree first. They are not reasons the method is useless.

The feature that dominates

Compare the raw and standardised nearest neighbour, then remove the standardisation and explain which column is deciding the answer.

Output
Click "Run" to execute your code

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

Where does a nearest-neighbour model do most of its work?