Teach by example

Add labeled points and watch a k-nearest-neighbors model shade the whole plane with its guesses.

NoviceExplained in What is AI, really?

Teach by example

Add labeled examples. The shading shows what the computer would guess for a new point anywhere on the plane.

Tap the plane to
3
Example data
Test point is guessedOrange, by 3 of 3 nearest examples
Your examples it gets right100%

Challenges

0 of 3 complete

  1. Place the test point where the vote is as close as it can be: won by a single neighbor (for example 3 to 2 with k = 5). (not yet)
  2. Load Ring, then find the smallest k at which the model starts getting its own examples wrong. (not yet)
  3. Clear the board and teach a shape of your own: at least 6 blue and 6 orange examples, every one guessed right with k = 3. (not yet)

Try this

  • Set k to 1 and add one blue point in the middle of the orange points. Then raise k and watch it get outvoted.
  • Load Ring or Mixed up and compare k = 1 with k = 15.
  • Choose Test a point and drag it along the border between colors.
The same idea in a few lines of codePython with NumPy
import numpy as np

# Six labeled examples: positions across and up, 0 = blue, 1 = orange.
points = np.array([[0.2, 0.7], [0.3, 0.6], [0.25, 0.8],
                   [0.7, 0.3], [0.8, 0.4], [0.6, 0.2]])
labels = np.array([0, 0, 0, 1, 1, 1])

def guess(query, k):
    distances = np.linalg.norm(points - query, axis=1)
    nearest = np.argsort(distances, kind="stable")[:k]
    votes = np.bincount(labels[nearest], minlength=2)
    return ("blue" if votes[0] > votes[1] else "orange"), votes

for k in (1, 3, 5):
    color, votes = guess(np.array([0.5, 0.5]), k)
    print(f"k={k}: {votes[0]} blue, {votes[1]} orange -> {color}")
k=1: 1 blue, 0 orange -> blue
k=3: 1 blue, 2 orange -> orange
k=5: 2 blue, 3 orange -> orange

Try "embedding", "softmax", "overfitting", or "backpropagation".