A Nearest Neighbor on a Number Line

Nearest-neighbor prediction chooses training examples with smallest distance to a query under the prescribed feature representation.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a query value
  2. Choose a tie policy
  3. Compare distances on both sides of a query
  4. Check boundary and duplicate examples

Trace a query value

With examples at 2 labeled A and 7 labeled B, a query at 4 is two units from A and three from B. The nearest is A. See absolute value.

Choose a tie policy

At 4.5 both distances are equal. Use a deterministic rule or multiple neighbors under the model's contract. Stable ordering illustrates a related tie guarantee.[1]

Compare distances on both sides of a query

Training points are at one, five and eight. At query six, the distances are five, one and two, so point five supplies the prediction under one-neighbor lookup. At 6.5, points five and eight tie. See k-neighbor votes and ties.

Query 6: |6−1|=5, |6−5|=1, |6−8|=2
Query 6.5: distance to 5 and 8 both 1.5
Choose an explicit tie rule

Check boundary and duplicate examples

Beyond the stored range, the nearest point is normally an endpoint. Two training examples at the same position can give contradictory labels. Test both and document how identity or order breaks ties.

References

  1. Gareth James et al., An Introduction to Statistical Learning ↩