Renovate our 40 products and stay live

K-Nearest Neighbors (KNN) Classification

Given Dataset

s_w s_l p_w p_l class
5.1 3.5 1.4 0.2 1
4.9 3.0 1.4 0.2 1
4.7 3.2 1.3 0.2 2
4.6 3.1 1.5 0.2 2
5.0 3.6 1.4 0.2 3
5.4 3.9 1.7 0.4 3

New Data Point for Classification

s_w s_l p_w p_l class
5.0 3.0 1.0 0.3 ?

Euclidean Distance Calculation (for k = 3)

1. For the first row (class 1):

  • Formula: √(x2 - x1)2 + (y2 - y1)2
  • Calculation: √(5.0 - 5.1)2 + (3.0 - 3.5)2 ≈ 0.656

2. For the second row (class 1):

  • Formula: √(x2 - x1)2 + (y2 - y1)2
  • Calculation: √(5.0 - 4.9)2 + (3.0 - 3.0)2 ≈ 0.424

3. For the third row (class 2):

  • Formula: √(x2 - x1)2 + (y2 - y1)2
  • Calculation: √(5.0 - 4.7)2 + (3.0 - 3.2)2 ≈ 0.479

4. For the fourth row (class 2):

  • Formula: √(x2 - x1)2 + (y2 - y1)2
  • Calculation: √(5.0 - 4.6)2 + (3.0 - 3.1)2 ≈ 0.656

5. For the fifth row (class 3):

  • Formula: √(x2 - x1)2 + (y2 - y1)2
  • Calculation: √(5.0 - 5.0)2 + (3.0 - 3.6)2 ≈ 0.729

6. For the sixth row (class 3):

  • Formula: √(x2 - x1)2 + (y2 - y1)2
  • Calculation: √(5.0 - 5.4)2 + (3.0 - 3.9)2 ≈ 1.213

Identify k Nearest Neighbors

The three smallest distances:

  • Row 2 (class 1): √(0.18) ≈ 0.424
  • Row 3 (class 2): √(0.23) ≈ 0.479
  • Row 1 (class 1): √(0.43) ≈ 0.656

Determine Majority Class

Majority class among the three smallest distances: Class 1 (2 occurrences).

Conclusion

If k = 3, the predicted class for the new data point (5.0, 3.0, 1.0, 0.3) using k-Nearest Neighbors is Class 1.

<!DOCTYPE html>

Euclidean Distance Calculation

For the first row (class 1):

distance = (5.05.1)2 + (3.03.5)2 + (1.01.4)2 + (0.30.2)2

distance 0.01 + 0.25 + 0.16 + 0.01 ≈ 0.43 ≈ 0.656

Written on December 17, 2023