Try the neighbor vote
Choose a query and a value of k to see the voting neighbors. Open a distance to check the calculation.
Tile U · (4, 3)
Use k = 1
Type B · 0 vote(s) for Type A and 1 for Type B.
| Point | Distance | Class | Vote |
|---|---|---|---|
| B1 | 1.00√[(4 − 4)² + (3 − 4)²] | Type B | Yes |
| A1 | 2.00√[(4 − 2)² + (3 − 3)²] | Type A | — |
| A2 | 2.24√[(4 − 5)² + (3 − 5)²] | Type A | — |
| B2 | 2.83√[(4 − 6)² + (3 − 1)²] | Type B | — |
| A3 | 3.61√[(4 − 1)² + (3 − 1)²] | Type A | — |
| B3 | 4.24√[(4 − 1)² + (3 − 6)²] | Type B | — |
Use k = 3
Type A · 2 vote(s) for Type A and 1 for Type B.
| Point | Distance | Class | Vote |
|---|---|---|---|
| B1 | 1.00√[(4 − 4)² + (3 − 4)²] | Type B | Yes |
| A1 | 2.00√[(4 − 2)² + (3 − 3)²] | Type A | Yes |
| A2 | 2.24√[(4 − 5)² + (3 − 5)²] | Type A | Yes |
| B2 | 2.83√[(4 − 6)² + (3 − 1)²] | Type B | — |
| A3 | 3.61√[(4 − 1)² + (3 − 1)²] | Type A | — |
| B3 | 4.24√[(4 − 1)² + (3 − 6)²] | Type B | — |
Use k = 5
Type A · 3 vote(s) for Type A and 2 for Type B.
| Point | Distance | Class | Vote |
|---|---|---|---|
| B1 | 1.00√[(4 − 4)² + (3 − 4)²] | Type B | Yes |
| A1 | 2.00√[(4 − 2)² + (3 − 3)²] | Type A | Yes |
| A2 | 2.24√[(4 − 5)² + (3 − 5)²] | Type A | Yes |
| B2 | 2.83√[(4 − 6)² + (3 − 1)²] | Type B | Yes |
| A3 | 3.61√[(4 − 1)² + (3 − 1)²] | Type A | Yes |
| B3 | 4.24√[(4 − 1)² + (3 − 6)²] | Type B | — |
Delivery P · 4.2 km in rain
Use k = 1
Late · 0 vote(s) for On time and 1 for Late.
| Point | Distance | Class | Vote |
|---|---|---|---|
| F | 0.20√[(4.2 − 4)² + (1 − 1)²] | Late | Yes |
| H | 0.80√[(4.2 − 5)² + (1 − 1)²] | Late | — |
| E | 1.02√[(4.2 − 4)² + (1 − 0)²] | Late | — |
| D | 1.20√[(4.2 − 3)² + (1 − 1)²] | Late | — |
| G | 1.28√[(4.2 − 5)² + (1 − 0)²] | On time | — |
| C | 1.56√[(4.2 − 3)² + (1 − 0)²] | On time | — |
| B | 2.20√[(4.2 − 2)² + (1 − 1)²] | On time | — |
| A | 2.42√[(4.2 − 2)² + (1 − 0)²] | On time | — |
Use k = 3
Late · 0 vote(s) for On time and 3 for Late.
| Point | Distance | Class | Vote |
|---|---|---|---|
| F | 0.20√[(4.2 − 4)² + (1 − 1)²] | Late | Yes |
| H | 0.80√[(4.2 − 5)² + (1 − 1)²] | Late | Yes |
| E | 1.02√[(4.2 − 4)² + (1 − 0)²] | Late | Yes |
| D | 1.20√[(4.2 − 3)² + (1 − 1)²] | Late | — |
| G | 1.28√[(4.2 − 5)² + (1 − 0)²] | On time | — |
| C | 1.56√[(4.2 − 3)² + (1 − 0)²] | On time | — |
| B | 2.20√[(4.2 − 2)² + (1 − 1)²] | On time | — |
| A | 2.42√[(4.2 − 2)² + (1 − 0)²] | On time | — |
Use k = 5
Late · 1 vote(s) for On time and 4 for Late.
| Point | Distance | Class | Vote |
|---|---|---|---|
| F | 0.20√[(4.2 − 4)² + (1 − 1)²] | Late | Yes |
| H | 0.80√[(4.2 − 5)² + (1 − 1)²] | Late | Yes |
| E | 1.02√[(4.2 − 4)² + (1 − 0)²] | Late | Yes |
| D | 1.20√[(4.2 − 3)² + (1 − 1)²] | Late | Yes |
| G | 1.28√[(4.2 − 5)² + (1 − 0)²] | On time | Yes |
| C | 1.56√[(4.2 − 3)² + (1 − 0)²] | On time | — |
| B | 2.20√[(4.2 − 2)² + (1 − 1)²] | On time | — |
| A | 2.42√[(4.2 − 2)² + (1 − 0)²] | On time | — |
Use k = 7
Late · 3 vote(s) for On time and 4 for Late.
| Point | Distance | Class | Vote |
|---|---|---|---|
| F | 0.20√[(4.2 − 4)² + (1 − 1)²] | Late | Yes |
| H | 0.80√[(4.2 − 5)² + (1 − 1)²] | Late | Yes |
| E | 1.02√[(4.2 − 4)² + (1 − 0)²] | Late | Yes |
| D | 1.20√[(4.2 − 3)² + (1 − 1)²] | Late | Yes |
| G | 1.28√[(4.2 − 5)² + (1 − 0)²] | On time | Yes |
| C | 1.56√[(4.2 − 3)² + (1 − 0)²] | On time | Yes |
| B | 2.20√[(4.2 − 2)² + (1 − 1)²] | On time | Yes |
| A | 2.42√[(4.2 − 2)² + (1 − 0)²] | On time | — |
Delivery Q · 3.2 km in dry weather
Use k = 1
On time · 1 vote(s) for On time and 0 for Late.
| Point | Distance | Class | Vote |
|---|---|---|---|
| C | 0.20√[(3.2 − 3)² + (0 − 0)²] | On time | Yes |
| E | 0.80√[(3.2 − 4)² + (0 − 0)²] | Late | — |
| D | 1.02√[(3.2 − 3)² + (0 − 1)²] | Late | — |
| A | 1.20√[(3.2 − 2)² + (0 − 0)²] | On time | — |
| F | 1.28√[(3.2 − 4)² + (0 − 1)²] | Late | — |
| B | 1.56√[(3.2 − 2)² + (0 − 1)²] | On time | — |
| G | 1.80√[(3.2 − 5)² + (0 − 0)²] | On time | — |
| H | 2.06√[(3.2 − 5)² + (0 − 1)²] | Late | — |
Use k = 3
Late · 1 vote(s) for On time and 2 for Late.
| Point | Distance | Class | Vote |
|---|---|---|---|
| C | 0.20√[(3.2 − 3)² + (0 − 0)²] | On time | Yes |
| E | 0.80√[(3.2 − 4)² + (0 − 0)²] | Late | Yes |
| D | 1.02√[(3.2 − 3)² + (0 − 1)²] | Late | Yes |
| A | 1.20√[(3.2 − 2)² + (0 − 0)²] | On time | — |
| F | 1.28√[(3.2 − 4)² + (0 − 1)²] | Late | — |
| B | 1.56√[(3.2 − 2)² + (0 − 1)²] | On time | — |
| G | 1.80√[(3.2 − 5)² + (0 − 0)²] | On time | — |
| H | 2.06√[(3.2 − 5)² + (0 − 1)²] | Late | — |
Use k = 5
Late · 2 vote(s) for On time and 3 for Late.
| Point | Distance | Class | Vote |
|---|---|---|---|
| C | 0.20√[(3.2 − 3)² + (0 − 0)²] | On time | Yes |
| E | 0.80√[(3.2 − 4)² + (0 − 0)²] | Late | Yes |
| D | 1.02√[(3.2 − 3)² + (0 − 1)²] | Late | Yes |
| A | 1.20√[(3.2 − 2)² + (0 − 0)²] | On time | Yes |
| F | 1.28√[(3.2 − 4)² + (0 − 1)²] | Late | Yes |
| B | 1.56√[(3.2 − 2)² + (0 − 1)²] | On time | — |
| G | 1.80√[(3.2 − 5)² + (0 − 0)²] | On time | — |
| H | 2.06√[(3.2 − 5)² + (0 − 1)²] | Late | — |
Use k = 7
On time · 4 vote(s) for On time and 3 for Late.
| Point | Distance | Class | Vote |
|---|---|---|---|
| C | 0.20√[(3.2 − 3)² + (0 − 0)²] | On time | Yes |
| E | 0.80√[(3.2 − 4)² + (0 − 0)²] | Late | Yes |
| D | 1.02√[(3.2 − 3)² + (0 − 1)²] | Late | Yes |
| A | 1.20√[(3.2 − 2)² + (0 − 0)²] | On time | Yes |
| F | 1.28√[(3.2 − 4)² + (0 − 1)²] | Late | Yes |
| B | 1.56√[(3.2 − 2)² + (0 − 1)²] | On time | Yes |
| G | 1.80√[(3.2 − 5)² + (0 − 0)²] | On time | Yes |
| H | 2.06√[(3.2 − 5)² + (0 − 1)²] | Late | — |
Which tiles are closest?
Who gets a vote?
Every training point has a known class. Sort the points by their distance from the unknown point. Each of the closest k gets one vote.
| Rank | Point | Distance | Class | Votes? |
|---|
Calculate a distance
Subtract the two coordinates separately, square the gaps, add them, then take the square root.
Try this in class
Check the worked example
What does training do in kNN?
Training stores the feature values and known class of each example. Prediction compares a new example with these stored points. A small k makes the result sensitive to one nearby point. A larger k includes more distant examples. Choose k and the feature scaling using validation data, and reserve unseen test labels for evaluation.