CoursesAIMathCodePhysicsAbout and FAQ

TwiddleLab / Artificial Intelligence / Learning From Data

Ask Your Neighbors: kNN

Lesson 2 of 11 · 7 min · Free, no account needed

What you will learn

  • k-nearest neighbours labels a new example by a vote of the k closest examples in the dataset.
  • With k = 1, one odd example can decide everything. A middle k gives a calmer vote; a huge k lets far-away examples vote too.
  • kNN keeps the whole dataset. Change the examples and you change its answers.

Start the lesson

How the lesson goes

  1. The new critter has ears of 4.8 cm and a tail of 7.4 cm. Puff or Zip?

    Make a gut call: tap the known critter it looks most like. Then Check.

  2. Ask the 3 closest critters.

    Drag the violet critter around the map. Lines go to its 3 nearest neighbours, and they vote.

  3. One neighbour might be a strange one. Ask more.

    k is how many of the closest critters get a vote. Slide it.

  4. What you just did is a real machine learning method.

    Tap to reveal its name.

    It's called k-nearest neighbours, or kNN.

    To label a new example, find the k closest examples. Close means the features are similar: the distance between the points is small.

    They vote. The kind with the most votes wins. With two kinds, an odd k avoids ties.

    There's no rule inside. The dataset itself is the model.

  5. Out-guess the vote.

    Six mystery critters, one at a time. Tap what its 3 nearest neighbours will say before the lines appear.

  6. Teach it with your own critters.

    Right now k = 3 says Puff for the violet critter. Pick a brush and tap the map to add critters until it says Zip. Then Check.

  7. One odd Zip is fooling kNN.

    With k = 1, two of the four test critters get the wrong answer. Try other values of k until all four are right. Then Check.

  8. Code: kNN is a short recipe.

    A program does exactly what you did, in this order. Put the stations on the belt. Sorting is the same idea as in Sorting, One Swap at a Time.

  9. Your map, with your critters in it. What does k = 5 say?

    Count the colours at the ends of the 5 lines and tap Puff or Zip. Then Check.

  10. Three flicks.

    Each critter shows its 3 neighbours for 3 seconds. Drag it to Puffs or Zips, tap a pile, or press Left or Right.

Previous lesson: Data Is a Table · Next lesson: Twenty Questions: Decision Trees · Part of the course: Learning From Data

Lessons are interactive and need JavaScript. Turn it on to play them.

Loading TwiddleLab