🌸 K-Nearest Neighbors (KNN)
Step-by-step mathematical demonstration of KNN using the Iris dataset.
(IMPLEMENTATION ON IRIS DATA SET - CLICK1 CLICK2 )
1. What is KNN?
K-Nearest Neighbors (KNN) is a supervised machine learning algorithm used mainly for classification and regression.
For classification, KNN looks at the nearest training examples and uses their labels to predict the class of a new data point.
2. Iris Training Dataset
| Point | Sepal Length | Sepal Width | Petal Length | Petal Width | Class |
|---|---|---|---|---|---|
| P1 | 5.1 | 3.5 | 1.4 | 0.2 | Setosa |
| P2 | 4.9 | 3.0 | 1.4 | 0.2 | Setosa |
| P3 | 6.4 | 3.2 | 4.5 | 1.5 | Versicolor |
| P4 | 6.9 | 3.1 | 4.9 | 1.5 | Versicolor |
| P5 | 6.3 | 3.3 | 6.0 | 2.5 | Virginica |
| P6 | 5.8 | 2.7 | 5.1 | 1.9 | Virginica |
1 New Flower
We want to classify a new flower.
Sepal Length = 5.0 Sepal Width = 3.1 Petal Length = 1.5 Petal Width = 0.2
2 Choose K
We will select the three closest flowers.
3 Euclidean Distance Formula
The four features are:
4 Calculate Distance to P1
= √[ 0.01 + 0.16 + 0.01 + 0 ]
= √0.18
= 0.424
P1 belongs to Setosa.
5 Calculate Distance to P2
= √[ 0.01 + 0.01 + 0.01 + 0 ]
= √0.03
= 0.173
P2 belongs to Setosa.
6 Calculate Distance to P3
= √[ 1.96 + 0.01 + 9.00 + 1.69 ]
= √12.66
= 3.558
P3 belongs to Versicolor.
7 Calculate Distance to P4
= √[ 3.61 + 0 + 11.56 + 1.69 ]
= √16.86
= 4.106
8 Calculate Distance to P5
= √[ 1.69 + 0.04 + 20.25 + 5.29 ]
= √27.27
= 5.222
9 Calculate Distance to P6
= √[ 0.64 + 0.16 + 12.96 + 2.89 ]
= √16.65
= 4.080
10 Sort the Distances
| Rank | Point | Distance | Class |
|---|---|---|---|
| 1 | P2 | 0.173 | Setosa |
| 2 | P1 | 0.424 | Setosa |
| 3 | P3 | 3.558 | Versicolor |
| 4 | P6 | 4.080 | Virginica |
| 5 | P4 | 4.106 | Versicolor |
| 6 | P5 | 5.222 | Virginica |
11 Select K = 3 Neighbors
Nearest Neighbor 2: P1 → Setosa → 0.424
Nearest Neighbor 3: P3 → Versicolor → 3.558
12 Majority Voting
Versicolor: P3 = 1 Vote
Virginica: = 0 Votes
13. Complete KNN Process
14. ⭐ Interactive Step-by-Step KNN
① Input
This is the new flower that we want to classify.
② Choose K
The three nearest training points will participate in voting.
③ Distance Formula
④ Calculate Distance
P2 → Setosa
⑤ Sort Distances
0.424 → P1
3.558 → P3
4.080 → P6
4.106 → P4
5.222 → P5
⑥ Select 3 Neighbors
P1 → Setosa
P3 → Versicolor
⑦ Voting
Versicolor = 1
Virginica = 0
The class with the highest number of votes wins.
No comments:
Post a Comment