CS203 · DATA SCIENCE FUNDAMENTALS · WEEK 11

K-nearest neighbors

Move the unknown point. Choose how many neighbors vote. See when the prediction changes.

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.

PointDistanceClassVote
B1
1.00√[(4 − 4)² + (3 − 4)²]
Type BYes
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.

PointDistanceClassVote
B1
1.00√[(4 − 4)² + (3 − 4)²]
Type BYes
A1
2.00√[(4 − 2)² + (3 − 3)²]
Type AYes
A2
2.24√[(4 − 5)² + (3 − 5)²]
Type AYes
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.

PointDistanceClassVote
B1
1.00√[(4 − 4)² + (3 − 4)²]
Type BYes
A1
2.00√[(4 − 2)² + (3 − 3)²]
Type AYes
A2
2.24√[(4 − 5)² + (3 − 5)²]
Type AYes
B2
2.83√[(4 − 6)² + (3 − 1)²]
Type BYes
A3
3.61√[(4 − 1)² + (3 − 1)²]
Type AYes
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.

PointDistanceClassVote
F
0.20√[(4.2 − 4)² + (1 − 1)²]
LateYes
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.

PointDistanceClassVote
F
0.20√[(4.2 − 4)² + (1 − 1)²]
LateYes
H
0.80√[(4.2 − 5)² + (1 − 1)²]
LateYes
E
1.02√[(4.2 − 4)² + (1 − 0)²]
LateYes
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.

PointDistanceClassVote
F
0.20√[(4.2 − 4)² + (1 − 1)²]
LateYes
H
0.80√[(4.2 − 5)² + (1 − 1)²]
LateYes
E
1.02√[(4.2 − 4)² + (1 − 0)²]
LateYes
D
1.20√[(4.2 − 3)² + (1 − 1)²]
LateYes
G
1.28√[(4.2 − 5)² + (1 − 0)²]
On timeYes
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.

PointDistanceClassVote
F
0.20√[(4.2 − 4)² + (1 − 1)²]
LateYes
H
0.80√[(4.2 − 5)² + (1 − 1)²]
LateYes
E
1.02√[(4.2 − 4)² + (1 − 0)²]
LateYes
D
1.20√[(4.2 − 3)² + (1 − 1)²]
LateYes
G
1.28√[(4.2 − 5)² + (1 − 0)²]
On timeYes
C
1.56√[(4.2 − 3)² + (1 − 0)²]
On timeYes
B
2.20√[(4.2 − 2)² + (1 − 1)²]
On timeYes
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.

PointDistanceClassVote
C
0.20√[(3.2 − 3)² + (0 − 0)²]
On timeYes
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.

PointDistanceClassVote
C
0.20√[(3.2 − 3)² + (0 − 0)²]
On timeYes
E
0.80√[(3.2 − 4)² + (0 − 0)²]
LateYes
D
1.02√[(3.2 − 3)² + (0 − 1)²]
LateYes
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.

PointDistanceClassVote
C
0.20√[(3.2 − 3)² + (0 − 0)²]
On timeYes
E
0.80√[(3.2 − 4)² + (0 − 0)²]
LateYes
D
1.02√[(3.2 − 3)² + (0 − 1)²]
LateYes
A
1.20√[(3.2 − 2)² + (0 − 0)²]
On timeYes
F
1.28√[(3.2 − 4)² + (0 − 1)²]
LateYes
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.

PointDistanceClassVote
C
0.20√[(3.2 − 3)² + (0 − 0)²]
On timeYes
E
0.80√[(3.2 − 4)² + (0 − 0)²]
LateYes
D
1.02√[(3.2 − 3)² + (0 − 1)²]
LateYes
A
1.20√[(3.2 − 2)² + (0 − 0)²]
On timeYes
F
1.28√[(3.2 − 4)² + (0 − 1)²]
LateYes
B
1.56√[(3.2 − 2)² + (0 − 1)²]
On timeYes
G
1.80√[(3.2 − 5)² + (0 − 0)²]
On timeYes
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.

RankPointDistanceClassVotes?

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.

CS203 · Week 11 course materials