Nearest neighbours.

k-nearest neighbours classifies a new point by looking at the k known points closest to it and taking a vote. There is no model to fit. The only real choice is k.

From Chapter 8: Nearest Neighbours, which explains these figures step by step.

Figure

A vote among the closest points

Filled kind at (5.64, 1.45)Filled kind at (3.58, 3.48)Filled kind at (4.8, 4.44)Filled kind at (9.57, 3.32)Filled kind at (7, 1.18)Filled kind at (3.57, 3.62)Filled kind at (6.68, 5.38)Filled kind at (7.43, 0.87)Filled kind at (7.7, 2.6)Filled kind at (9.69, 2.64)Filled kind at (9.03, 5.39)Filled kind at (8.5, 4.31)Filled kind at (7.48, 0.2)Filled kind at (8.21, 3.47)Filled kind at (5.41, 0.62)Filled kind at (5.29, 1.79)Filled kind at (7.14, 0.35)Filled kind at (4.23, 2.79)Filled kind at (7.49, 2.73)Filled kind at (3.76, 3.11)Filled kind at (9.07, 4.87)Filled kind at (6.71, 1.58)Filled kind at (7.74, 1.69)Filled kind at (9.77, 2.05)Filled kind at (9.63, 5.26)Filled kind at (4.38, 0.2)Filled kind at (6.07, 0.2)Filled kind at (8.49, 1.75)Filled kind at (6.1, 4.35)Filled kind at (4.41, 4.1)Hollow kind at (8.9, 3.6)Hollow kind at (5.69, 7.83)Hollow kind at (5.77, 8.54)Hollow kind at (2.76, 8.31)Hollow kind at (6.9, 7.92)Hollow kind at (6.04, 9.8)Hollow kind at (1.78, 7.51)Hollow kind at (0.48, 5.18)Hollow kind at (1.98, 7.35)Hollow kind at (7.01, 9.8)Hollow kind at (1.97, 7.53)Hollow kind at (4.23, 8.94)Hollow kind at (3.6, 8.5)Hollow kind at (4.38, 6.87)Hollow kind at (6.75, 6.95)Hollow kind at (1.83, 7.3)Hollow kind at (7.1, 5.52)Hollow kind at (3.31, 8.33)Hollow kind at (3.17, 7.27)Hollow kind at (0.98, 7.37)Hollow kind at (6.76, 8.7)Hollow kind at (4.4, 7.69)Hollow kind at (2.21, 5.63)Hollow kind at (4.48, 6.96)Hollow kind at (1.35, 6.71)Hollow kind at (8.28, 4.22)Hollow kind at (3.12, 5.01)Hollow kind at (4.79, 8.24)Hollow kind at (2.09, 5.3)Hollow kind at (1, 8.83)
  • Known: filled kind
  • Known: hollow kind
  • New point and its neighbours
Neighbours asked (k)
5
Votes
4 filled · 1 hollow
The new point is called
filled
5
Drag the orange point.
Sixty known points of two kinds. The orange point is new, and the lines join it to the neighbours that get a vote. Try it: drag the orange point through the gap between the two groups and watch the vote change. Then set k to 1, and to 15.
Figure

What k-NN would say everywhere

5.64, 1.453.58, 3.484.8, 4.449.57, 3.327, 1.183.57, 3.626.68, 5.387.43, 0.877.7, 2.69.69, 2.649.03, 5.398.5, 4.317.48, 0.28.21, 3.475.41, 0.625.29, 1.797.14, 0.354.23, 2.797.49, 2.733.76, 3.119.07, 4.876.71, 1.587.74, 1.699.77, 2.059.63, 5.264.38, 0.26.07, 0.28.49, 1.756.1, 4.354.41, 4.18.9, 3.65.69, 7.835.77, 8.542.76, 8.316.9, 7.926.04, 9.81.78, 7.510.48, 5.181.98, 7.357.01, 9.81.97, 7.534.23, 8.943.6, 8.54.38, 6.876.75, 6.951.83, 7.37.1, 5.523.31, 8.333.17, 7.270.98, 7.376.76, 8.74.4, 7.692.21, 5.634.48, 6.961.35, 6.718.28, 4.223.12, 5.014.79, 8.242.09, 5.31, 8.83
  • Shaded: would be called filled
  • Unshaded: would be called hollow
Neighbours asked (k)
1
Known points
60
Right on 300 new points
86.0%
1

Click or tap anywhere on the plot to add a point of the selected kind.

Shaded areas would be called filled, unshaded areas hollow. Try it: start with k = 1 and look for small islands around single points. Raise k and watch them dissolve. Then click to add a few points of the wrong kind inside a region.

Learn the ideas behind it

More playgrounds

Search

    The Book of Machine Learning
    Search
    Reading settings
    Text size
    Colours

    Saved in this browser only.

    Keyboard shortcuts
    /
    Search the book
    ← →
    Previous and next chapter
    g
    Glossary
    c
    Table of contents
    e
    Jump to the exercises
    t
    Reading settings
    ?
    Show this list
    Esc
    Close a window or pop-in