CS 460G Midterm Hub / Lectures / Week 3 Lecture 2
Week 3 · Lecture 2

Non-parametric Methods — k-Nearest Neighbours

Parametric vs non-parametric, the distance metric, and the four-step algorithm.

short answercoding Q

One clean idea: do not build a global model at all, just remember the data and look things up at prediction time.

Parametric vs non-parametric — the comparison table

This is a standard short-answer question and the slide gives you the answer in a directly comparable form.

ParametricNon-parametric
Assumptionfixed assumption on the form of the modelonly "similar inputs have similar outputs"
Parametersfinite numberinfinite (complexity grows with $N$)
Effect of dataall training instances affect the global estimateonly nearby instances affect a local model
Exampleslinear regression, logistic regressionk-NN, decision trees
Advantagesimple; reduces learning to estimating a few $w$flexible, adapts to complex relationships
Disadvantagethe assumption may not hold → large errormemory and computation grow with the data
Parametric models
Parametric models · slide 12
Non-parametric models
Non-parametric models · slide 19

Instance-based learning

k-NN is an instance-based (memory-based) learner, also called a lazy learner: it simply stores the training instances and does all its work at query time. It works for both classification and regression.

Instance-based / lazy learning
Instance-based / lazy learning · slide 27

The distance metric

Training examples live in Euclidean space, $x\in\mathbb{R}^d$. Similarity is measured by a Minkowski distance of order $p$:

$$d(x,z) = \left(\sum_{j=1}^{d}|x_j - z_j|^p\right)^{1/p}$$

  • $p=1$ → Manhattan distance ($L_1$)
  • $p=2$ → Euclidean distance ($L_2$)
Minkowski distance; p=1 Manhattan, p=2 Euclidean
Minkowski distance; p=1 Manhattan, p=2 Euclidean · slide 33

Scale matters and k-NN never tells you. Euclidean distance adds up squared differences across features, so a feature measured in thousands (income) drowns a feature measured in tenths (a ratio). Standardise first — that is exactly what the PCA notebook does before PCA, and it is worth a mark in any k-NN answer.

The algorithm — four steps, exactly as examined

k-NN classification

  1. Store the training instances $\{(x_i,y_i),\dots\}$
  2. Find the $k$ neighbours from the training set closest to the test instance $x^*$, by the distance metric
  3. Count how many of those $k$ neighbours fall in each class
  4. Assign $x^*$ to the class with the maximum count — majority voting among the $k$ neighbours
The four-step k-NN algorithm
The four-step k-NN algorithm · slide 43

The same four steps with step 4 changed gives k-NN regression: take the average of the $k$ neighbour targets, $y^* = \operatorname{avg}(y_1,\dots,y_k)$.

k-NN for regression — average the neighbour targets
k-NN for regression — average the neighbour targets · slide 45

Choosing k

The deck demonstrates the effect visually: $k=3$ versus $k=5$ can change the label of the query point, and the decision boundary becomes smoother as $k$ grows.

  • $k$ too small — boundary follows noise; high variance, overfitting. $k=1$ has zero training error and is maximally jagged.
  • $k$ too large — boundary washes out; high bias, underfitting. At $k=N$ everything is one class.
  • $k$ is a hyperparameter: set before training, tuned on validation data — not learned from the training set.

"How does k-NN work? Explain with an example." Give the four steps, then a concrete instance: with $k=3$ and the Iris petal features, a query at (5.0, 2.0) has nearest neighbours of class virginica, virginica and versicolor; the majority is virginica so the query is labelled virginica. Close with the three properties that make the answer complete: it is non-parametric / lazy (no training, all cost at inference), it is sensitive to feature scaling (so standardise), and $k$ is a hyperparameter chosen by validation, with small $k$ → overfitting and large $k$ → underfitting.