Non-parametric Methods — k-Nearest Neighbours
Parametric vs non-parametric, the distance metric, and the four-step algorithm.
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.
| Parametric | Non-parametric | |
|---|---|---|
| Assumption | fixed assumption on the form of the model | only "similar inputs have similar outputs" |
| Parameters | finite number | infinite (complexity grows with $N$) |
| Effect of data | all training instances affect the global estimate | only nearby instances affect a local model |
| Examples | linear regression, logistic regression | k-NN, decision trees |
| Advantage | simple; reduces learning to estimating a few $w$ | flexible, adapts to complex relationships |
| Disadvantage | the assumption may not hold → large error | memory and computation grow with the data |


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.

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$)

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
- Store the training instances $\{(x_i,y_i),\dots\}$
- Find the $k$ neighbours from the training set closest to the test instance $x^*$, by the distance metric
- Count how many of those $k$ neighbours fall in each class
- Assign $x^*$ to the class with the maximum count — majority voting among the $k$ neighbours

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)$.

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.











































