Module 4 — k-Nearest-Neighbour Decision Rules

Source: Lec 9 — k-Nearest Neighbor Decision Rule.

Setup. Training pairs , , , , . Test point . Distance (default Euclidean).


C23. The kNN Decision Rule

Intuition

“Tell me who your neighbours are and I will tell you who you are.” Find the training points closest to the new point and let them vote.

Algorithm (lecture)

  1. Compute for all .
  2. Sort the distances in non-decreasing order.
  3. Take the first (the nearest neighbours).
  4. Let be the number of those points in class . Assign to class if . Ties are broken arbitrarily.

Geometrically, the neighbourhood is the closed disk of radius .

Mathematical facts

  • Choice of : no universal rule; estimate by cross-validation (C13). Small ⇒ low bias, high variance (jagged boundary); large ⇒ smoother, may swamp small classes.
  • 1-NN asymptotics (Cover & Hart 1967): as , the 1-NN error is at most twice the Bayes error . More precisely, for two classes .
  • Cost: no training, per query.

Derivation of the 1-NN limit (two classes)

Let . As the nearest neighbour of converges to so its label is an independent draw with . 1-NN errs when the test label and the neighbour’s label differ: Let ; then . Taking expectations (with ): , because Jensen gives . ∎

Key insights

  • Distance function and feature scaling dominate the behaviour.
  • Majority voting is unreliable when the top two classes are nearly tied (the lecture’s motivation for the variants C24–C25).

Worked examples

Example 1 — Very Easy

  • Problem: Training: . Classify with (Euclidean).
  • Asked: Class.
  • Concept: kNN.
  • Approach: Distances, sort, vote.
  • Solution: ; ; ; ; . Sorted: . Votes: A=2, B=1.
  • Final answer: A.
  • Key lesson: Only the closest matter.

Example 2 — Easy

  • Problem: The sorted neighbour labels of are (nearest first): . Classify for .
  • Asked: Four decisions.
  • Concept: Dependence on .
  • Approach: Count in prefixes.
  • Solution: : : positions 1,3 ⇒ 2; : 2,4,5 ⇒ 3 ⇒ . : : 1,3,6 ⇒ 3; 3 ⇒ tie. : : 1,3,6,7 ⇒ 4; 3 ⇒ . : : 1,3,6,7,9 ⇒ 5; 4 ⇒ .
  • Final answer: , tie, , .
  • Key lesson: The prediction flips with when the vote is nearly even.

Example 3 — Moderate

  • Problem: Two classes with Bayes error . Bound the asymptotic 1-NN error. Repeat for .
  • Asked: Two bounds.
  • Concept: Cover–Hart.
  • Approach: vs .
  • Solution: : (crude: ). : (crude: ).
  • Final answer: and .
  • Key lesson: The bound is tight when is small, loose when large (it cannot exceed ).

Example 4 — Hard

  • Problem: Features (age in years, income in ₹). Training: , . Test . Feature standard deviations: age , income . Classify by 1-NN raw and after standardising (divide differences by the std).
  • Asked: Two decisions.
  • Concept: Feature scaling.
  • Approach: Compute both distances twice.
  • Solution: Raw: ; ⇒ A. Scaled: , ; , ⇒ B.
  • Final answer: Raw → A; scaled → B.
  • Key lesson: Without normalisation the large-unit feature is the only feature.

Example 5 — Very Hard

  • Problem: Half of the input space has , the other half . Compute (a) the Bayes error, (b) the asymptotic 1-NN error, (c) , and confirm (b)(c).
  • Asked: Three numbers and verification.
  • Concept: Cover–Hart derivation.
  • Approach: Local errors then average.
  • Solution: (a) Local Bayes: and . . (b) Local 1-NN: and . . (c) ; . Chain: ✓.
  • Final answer: , , bound .
  • Key lesson: 1-NN pays extra only where the posterior is neither pure nor 50/50.

Common mistakes

  • Not scaling features.
  • Using even in 2-class problems (ties).
  • Believing 1-NN error is always twice Bayes (it is an upper bound).

Exam traps

  • Ties are broken arbitrarily.
  • “Optimal ” is found by cross-validation; there is no formula.
  • The bound requires .

Connections

Bayes error (C15), cross-validation (C13), weighted/Tweaked kNN (C24, C25), k-means assignment (C36).

Mastery checklist

  • Run kNN by hand.
  • Explain the effect of and of scaling.
  • Derive for 1-NN.

C24. Distance-Weighted kNN

Intuition

A neighbour 1 cm away should speak louder than one 10 km away. Give each of the neighbours a weight that decreases with distance.

Formal definition (Dudani 1976, as in the lecture)

Let be the nearest neighbours sorted by distance , so . Assign to the class whose neighbours’ weights sum to the greatest value.

Properties

  • (nearest) and (farthest), linear in between.
  • It reduces the sensitivity to the arbitrary cut-off but must still be chosen.

Worked examples

Example 1 — Very Easy

  • Problem: Distances for . Weights?
  • Asked: .
  • Concept: Linear weighting.
  • Approach: .
  • Solution: ; ; .
  • Final answer: .
  • Key lesson: The farthest neighbour always gets 0.

Example 2 — Easy

  • Problem: Neighbour labels (nearest first) with the distances above. Plain vote vs weighted vote.
  • Asked: Two classes.
  • Concept: Weighted voting.
  • Approach: Sum weights per class.
  • Solution: Plain: A=1, B=2 ⇒ B. Weighted: A ; B ⇒ A.
  • Final answer: Plain → B; weighted → A.
  • Key lesson: One very close point can outweigh two distant ones.

Example 3 — Moderate

  • Problem: (all equal) with labels . Weights and decision.
  • Asked: Weights, class.
  • Concept: Degenerate case .
  • Approach: Use the "" branch.
  • Solution: All ⇒ A , B ⇒ B (ordinary kNN).
  • Final answer: B.
  • Key lesson: The formula degrades gracefully to majority vote.

Example 4 — Hard

  • Problem: with distances and labels . Compute weights and compare plain majority vs weighted.
  • Asked: Weights, both decisions.
  • Concept: Weighted kNN.
  • Approach: , , denominator .
  • Solution: . Class A: . Class B: . Plain majority: A , B ⇒ A. Weighted ⇒ B.
  • Final answer: Plain → A; weighted → B.
  • Key lesson: Close neighbours (B,B) outweigh far ones (A,A).

Example 5 — Very Hard

  • Problem: Show (i) the weighted rule with neighbours ignores the th, so it uses only the nearer neighbours; (ii) construct distances where weighted kNN is identical to 1-NN regardless of the other labels.
  • Asked: Proof and construction.
  • Concept: Weight structure.
  • Approach: Algebra.
  • Solution: (i) always, so never contributes. The remaining weights are , ; they depend on — so still controls the scale of the weighting. (ii) Let with labels . Weights: ; ; . Class A ; B ⇒ A, the 1-NN label, even though a majority-of-3 says B.
  • Final answer: Weighted kNN can behave like 1-NN when the nearest neighbour is much closer than the rest.
  • Key lesson: The weighting depends on relative distances; outliers in distance can make the rule effectively 1-NN.

Common mistakes

  • Forgetting .
  • Using weights and calling them the lecture formula.

Exam traps

  • Weights use in the numerator, in the denominator.
  • Still need a valid .

Connections

C23, C25.

Mastery checklist

  • Compute linear distance weights.
  • Explain when weighted and plain kNN disagree.

C25. Tweaked kNN (TkNN): Bounded Majority Voting

Intuition

Instead of fixing , keep widening the neighbourhood until the winner leads the runner-up by a margin . If no margin is ever reached, refuse to classify. A single, more interpretable parameter replaces .

Algorithm (lecture)

  1. Compute and sort distances .
  2. (a positive integer).
  3. Take the first neighbours; let be the class counts. ; = second largest.
  4. If ⇒ output the majority class. Else and repeat. If ⇒ is unclassified.

Properties

  • ⇒ the rule equals 1-NN (at the margin is already ).
  • Larger ⇒ more cautious, more abstentions.
  • Choose by cross-validation. Does not consider the quality of the voting neighbours (open problem noted in the lecture).

Worked examples

Example 1 — Very Easy

  • Problem: Show TkNN with equals 1-NN.
  • Asked: Proof.
  • Concept: Margin definition.
  • Approach: Start at .
  • Solution: At : top class has count 1, every other has 0 ⇒ margin ⇒ stop, output the class of the nearest neighbour.
  • Final answer: Yes.
  • Key lesson: is the most permissive setting.

Example 2 — Easy

  • Problem: The sequence (nearest first) (the lecture’s 9.1). Run TkNN with and .
  • Asked: Output.
  • Concept: Algorithm.
  • Approach: Track margin.
  • Solution: : : counts margin ⇒ . : : margin ; : margin ; : margin ; : margin ; : margin ; : margin ; : margin ; : margin . Never reaches ; ⇒ unclassified.
  • Final answer: ; unclassified.
  • Key lesson: The lecture’s remark “for cannot classify” is verified; an alternating sequence never builds a margin.

Example 3 — Moderate

  • Problem: Sequence with . Output and the effective .
  • Asked: Class and .
  • Concept: Adaptive .
  • Approach: Margin tracking.
  • Solution: : margin 0. : margin 1. : margin ⇒ stop.
  • Final answer: with .
  • Key lesson: Effective depends on the query.

Example 4 — Hard

  • Problem: Three classes ; sequence ; . Find the output and .
  • Asked: Class and .
  • Concept: with three classes.
  • Approach: Counts at each .
  • Solution: : , , margin 0. : margin 0. : margin 1. : margin 0. : margin 1. : margin ⇒ stop.
  • Final answer: at .
  • Key lesson: The margin is against the second best class, not the average of the others.

Example 5 — Very Hard

  • Problem: Show that TkNN can only ever stop when and that the margin at stopping is exactly (not larger). Then argue why overshooting is impossible.
  • Asked: Two proofs.
  • Concept: Margin dynamics.
  • Approach: One-step margin changes.
  • Solution: The margin is at most (all neighbours in one class), so stopping requires — matching the initial . When increases by 1 exactly one class count increases by 1, so and each change by at most and changes by at most in absolute value. Hence cannot jump over : the first time it equals . (At the start we have .)
  • Final answer: The stopping margin is exactly .
  • Key lesson: The test "" is equivalent to "", because moves in unit steps.

Common mistakes

  • Starting with instead of (harmless, but not the lecture’s algorithm).
  • Forgetting to return “unclassified”.

Exam traps

  • ⇒ 1-NN.
  • No is needed, but is.

Connections

C23, C24, cross-validation (C13).

Mastery checklist

  • Run TkNN on a label sequence.
  • Explain why is 1-NN.
  • State when it abstains.