Binary Classification and k-Nearest Neighbours

Binary classification predicts a label in {−1, +1} and is judged by the 0-1 loss. Minimising it directly over linear classifiers is NP-hard, and regression on ±1 labels is a poor substitute. k-nearest neighbours avoids training altogether: it labels a point by majority vote among its k closest training points, with k trading noise sensitivity against smoothness.

Machine Learning Techniques

Regression predicts a number. Many of the most important prediction problems instead ask a yes-or-no question: is this email spam, will this customer leave, does this scan show a tumour, is this transaction fraudulent? This chapter sets up binary classification and meets its simplest method, one that does not train at all.

The problem

The training data is {(x1,y1),…,(xn,yn)}\{(x_1, y_1), \dots, (x_n, y_n)\} with xi∈Rdx_i \in \mathbb{R}^d and labels yi∈{−1,+1}y_i \in \{-1, +1\} (sometimes written {0,1}\{0, 1\}). We want a classifier h:Rd→{−1,+1}h: \mathbb{R}^d \to \{-1, +1\}.

The natural measure of quality is how often the classifier is wrong, the 0-1 loss:

L0-1(h)=1n∑i=1n1[h(xi)≠yi],L_{0\text{-}1}(h) = \frac{1}{n}\sum_{i=1}^{n} \mathbf{1}\big[h(x_i) \ne y_i\big],

the fraction of training mistakes (the training error). As always, what we really care about is the error rate on new data.

Why not just reuse regression?

Two obvious approaches both disappoint.

Minimise the 0-1 loss directly over linear classifiers h(x)=sign⁡(w⊤x)h(x) = \operatorname{sign}(w^\top x). The loss is a step function of ww: it is flat almost everywhere and jumps when a point crosses the boundary, so gradients are useless. Worse, finding the linear classifier with the fewest mistakes on data that no line separates perfectly is NP-hard in general. We will need smarter substitutes for the 0-1 loss (Modules 9 to 11 are largely about them).

Fit least squares to the labels ±1, then take the sign. It is easy and sometimes acceptable, but it optimises the wrong thing. Squared error punishes predictions that are too correct: a point with label +1+1 and score +5+5 costs (5−1)2=16(5 - 1)^2 = 16, as much as a serious mistake. Add a cluster of easy, far-away points of one class and the regression line tilts towards them to reduce their "error", moving the boundary and creating real mistakes elsewhere.

Two classes along a line; a least-squares fit to the plus and minus one labels crosses zero between them. Adding a far-away group of plus-one points tilts the fit so that its zero crossing moves and misclassifies some points
Least squares on ±1 labels is fragile: far-away points that are already correctly classified pull the fitted line and move the decision boundary.

k-nearest neighbours

k-nearest neighbours (kNN) sidesteps all of this. To classify a new point xx:

  1. Find the kk training points closest to xx (usually by Euclidean distance).
  2. Predict the majority label among them.

There is no training step: the classifier is the stored training set. This is called a non-parametric or instance-based method.

The effect of k

  • k = 1 predicts the label of the single nearest training point. Its decision regions are a Voronoi diagram of the training points, the same geometry as k-means, now with one cell per training point. It fits every quirk of the training data, including mislabelled points: a single noisy point carves out an island of the wrong class around itself. Its training error is zero (every point is its own nearest neighbour), which tells you nothing.
  • Large k averages over many neighbours, smoothing the boundary and ignoring isolated noise. Too large and it blurs genuine structure away; at k=nk = n every point gets the overall majority class.

So kk trades variance (small kk, sensitive to individual points) against bias (large kk, too smooth). It is chosen by cross-validation, usually as an odd number to avoid ties between two classes.

Try it yourself
Machine Learning Lab: k-nearest neighbours →
Slide k from 1 to 45 on the two-moons data and watch the regions change from islands to a smooth curve to a near-straight line. Click the plot to add a point and see how far its influence reaches.

Practical issues

  • Scale. Distance mixes all features, so a feature measured in large units (income in rupees) swamps one in small units (age in years). Standardise the features first.
  • Prediction cost. Every prediction needs the distance to every training point: O(nd)O(nd) per query, with the whole training set kept in memory. Tree and hashing indexes (k-d trees, approximate nearest-neighbour search, the same vector indexes used in RAG systems) make it practical at scale.
  • High dimensions. In many dimensions, distances between random points become nearly equal, so "nearest" carries little information (the curse of dimensionality). kNN works best when the data lies near a low-dimensional structure, or after dimensionality reduction such as PCA.
  • No model to inspect. kNN gives predictions but no compact summary of what it learned. In the language of this course, there is no compression at all: the "model" is the data.

That last point motivates the rest of the module. Decision trees, next, keep kNN's flexibility but compress the data into a short list of questions.

EasykNNGeneralisation

Why does 1-nearest-neighbour always have zero training error, and why is that meaningless?

EasykNNPreprocessing

A kNN classifier uses features age (years) and annual income (rupees) unscaled. What goes wrong and how do you fix it?

MediumkNNBias-variance

Explain how k controls the bias-variance trade-off in kNN.