Distance-Based Classification โ Questions & Full Solutions
๐ The Questionโ
Given the following training dataset (three classes, each class has 5 samples , each sample has two features ):
Class W1 Class W2 Class W3 X1 X2 X1 X2 X1 X2 Sample 1 2.491 2.167 4.218 โ2.075 โ2.520 0.483 Sample 2 1.053 0.667 โ1.156 โ2.992 โ12.163 3.161 Sample 3 5.792 3.425 โ4.425 1.408 โ13.438 2.414 Sample 4 2.045 โ1.467 โ1.467 โ2.838 โ4.467 2.298 Sample 5 0.550 4.020 โ2.137 โ2.473 โ3.711 4.364
Predict the class label for the following test samples using:
Euclidean Distance
City Block (Manhattan) Distance
Mahalanobis Distance
Test Sample X1 X2 T1 2.543 0.046 T2 โ2.799 0.746 T3 โ7.429 2.329
๐งฎ Step 0 โ Compute Class Mean Vectorsโ
The classifier compares each test sample to the mean (centroid) of each class.
Formula:
ฮผ k = 1 n โ i = 1 n x i ( k ) \mu_k = \frac{1}{n}\sum_{i=1}^{n} x_i^{(k)} ฮผ k โ = n 1 โ i = 1 โ n โ x i ( k ) โ
Class W1 Mean (ฮผโ)โ
ฮผ 1 , X 1 = 2.491 + 1.053 + 5.792 + 2.045 + 0.550 5 = 11.931 5 = 2.386 \mu_{1,X1} = \frac{2.491 + 1.053 + 5.792 + 2.045 + 0.550}{5} = \frac{11.931}{5} = 2.386 ฮผ 1 , X 1 โ = 5 2.491 + 1.053 + 5.792 + 2.045 + 0.550 โ = 5 11.931 โ = 2.386
ฮผ 1 , X 2 = 2.167 + 0.667 + 3.425 + ( โ 1.467 ) + 4.020 5 = 8.812 5 = 1.762 \mu_{1,X2} = \frac{2.167 + 0.667 + 3.425 + (-1.467) + 4.020}{5} = \frac{8.812}{5} = 1.762 ฮผ 1 , X 2 โ = 5 2.167 + 0.667 + 3.425 + ( โ 1.467 ) + 4.020 โ = 5 8.812 โ = 1.762
ฮผ 1 = ( 2.386 , ย 1.762 ) \boxed{\mu_1 = (2.386,\ 1.762)} ฮผ 1 โ = ( 2.386 , ย 1.762 ) โ
Class W2 Mean (ฮผโ)โ
ฮผ 2 , X 1 = 4.218 + ( โ 1.156 ) + ( โ 4.425 ) + ( โ 1.467 ) + ( โ 2.137 ) 5 = โ 4.967 5 = โ 0.993 \mu_{2,X1} = \frac{4.218 + (-1.156) + (-4.425) + (-1.467) + (-2.137)}{5} = \frac{-4.967}{5} = -0.993 ฮผ 2 , X 1 โ = 5 4.218 + ( โ 1.156 ) + ( โ 4.425 ) + ( โ 1.467 ) + ( โ 2.137 ) โ = 5 โ 4.967 โ = โ 0.993
ฮผ 2 , X 2 = ( โ 2.075 ) + ( โ 2.992 ) + 1.408 + ( โ 2.838 ) + ( โ 2.473 ) 5 = โ 8.970 5 = โ 1.794 \mu_{2,X2} = \frac{(-2.075) + (-2.992) + 1.408 + (-2.838) + (-2.473)}{5} = \frac{-8.970}{5} = -1.794 ฮผ 2 , X 2 โ = 5 ( โ 2.075 ) + ( โ 2.992 ) + 1.408 + ( โ 2.838 ) + ( โ 2.473 ) โ = 5 โ 8.970 โ = โ 1.794
ฮผ 2 = ( โ 0.993 , ย โ 1.794 ) \boxed{\mu_2 = (-0.993,\ -1.794)} ฮผ 2 โ = ( โ 0.993 , ย โ 1.794 ) โ
Class W3 Mean (ฮผโ)โ
ฮผ 3 , X 1 = ( โ 2.520 ) + ( โ 12.163 ) + ( โ 13.438 ) + ( โ 4.467 ) + ( โ 3.711 ) 5 = โ 36.299 5 = โ 7.260 \mu_{3,X1} = \frac{(-2.520) + (-12.163) + (-13.438) + (-4.467) + (-3.711)}{5} = \frac{-36.299}{5} = -7.260 ฮผ 3 , X 1 โ = 5 ( โ 2.520 ) + ( โ 12.163 ) + ( โ 13.438 ) + ( โ 4.467 ) + ( โ 3.711 ) โ = 5 โ 36.299 โ = โ 7.260
ฮผ 3 , X 2 = 0.483 + 3.161 + 2.414 + 2.298 + 4.364 5 = 12.720 5 = 2.544 \mu_{3,X2} = \frac{0.483 + 3.161 + 2.414 + 2.298 + 4.364}{5} = \frac{12.720}{5} = 2.544 ฮผ 3 , X 2 โ = 5 0.483 + 3.161 + 2.414 + 2.298 + 4.364 โ = 5 12.720 โ = 2.544
ฮผ 3 = ( โ 7.260 , ย 2.544 ) \boxed{\mu_3 = (-7.260,\ 2.544)} ฮผ 3 โ = ( โ 7.260 , ย 2.544 ) โ
๐ Part 1 โ Euclidean Distanceโ
Formula (distance from test point x to class mean ฮผ):
d E ( x , ย ฮผ ) = ( x 1 โ ฮผ 1 ) 2 + ( x 2 โ ฮผ 2 ) 2 d_E(x,\ \mu) = \sqrt{(x_1 - \mu_1)^2 + (x_2 - \mu_2)^2} d E โ ( x , ย ฮผ ) = ( x 1 โ โ ฮผ 1 โ ) 2 + ( x 2 โ โ ฮผ 2 โ ) 2 โ
The test sample is assigned to the class with the smallest Euclidean distance.
Test Sample T1 = (2.543, 0.046)โ
vs W1:
d E = ( 2.543 โ 2.386 ) 2 + ( 0.046 โ 1.762 ) 2 = ( 0.157 ) 2 + ( โ 1.716 ) 2 = 0.025 + 2.945 = 2.970 โ 1.72 ย Smallest d_E = \sqrt{(2.543 - 2.386)^2 + (0.046 - 1.762)^2} = \sqrt{(0.157)^2 + (-1.716)^2} = \sqrt{0.025 + 2.945} = \sqrt{2.970} \approx 1.72\ \text{Smallest} d E โ = ( 2.543 โ 2.386 ) 2 + ( 0.046 โ 1.762 ) 2 โ = ( 0.157 ) 2 + ( โ 1.716 ) 2 โ = 0.025 + 2.945 โ = 2.970 โ โ 1.72 ย Smallest
vs W2:
d E = ( 2.543 โ ( โ 0.993 ) ) 2 + ( 0.046 โ ( โ 1.794 ) ) 2 = ( 3.536 ) 2 + ( 1.840 ) 2 = 12.503 + 3.386 = 15.889 โ 3.99 d_E = \sqrt{(2.543 - (-0.993))^2 + (0.046 - (-1.794))^2} = \sqrt{(3.536)^2 + (1.840)^2} = \sqrt{12.503 + 3.386} = \sqrt{15.889} \approx 3.99 d E โ = ( 2.543 โ ( โ 0.993 ) ) 2 + ( 0.046 โ ( โ 1.794 ) ) 2 โ = ( 3.536 ) 2 + ( 1.840 ) 2 โ = 12.503 + 3.386 โ = 15.889 โ โ 3.99
vs W3:
d E = ( 2.543 โ ( โ 7.260 ) ) 2 + ( 0.046 โ 2.544 ) 2 = ( 9.803 ) 2 + ( โ 2.498 ) 2 = 96.099 + 6.240 = 102.339 โ 10.12 d_E = \sqrt{(2.543 - (-7.260))^2 + (0.046 - 2.544)^2} = \sqrt{(9.803)^2 + (-2.498)^2} = \sqrt{96.099 + 6.240} = \sqrt{102.339} \approx 10.12 d E โ = ( 2.543 โ ( โ 7.260 ) ) 2 + ( 0.046 โ 2.544 ) 2 โ = ( 9.803 ) 2 + ( โ 2.498 ) 2 โ = 96.099 + 6.240 โ = 102.339 โ โ 10.12
โ T1 is classified as Class W1 (d = 1.72, smallest)
Test Sample T2 = (โ2.799, 0.746)โ
vs W1:
d E = ( โ 2.799 โ 2.386 ) 2 + ( 0.746 โ 1.762 ) 2 = ( โ 5.185 ) 2 + ( โ 1.016 ) 2 = 26.884 + 1.032 = 27.916 โ 5.28 d_E = \sqrt{(-2.799 - 2.386)^2 + (0.746 - 1.762)^2} = \sqrt{(-5.185)^2 + (-1.016)^2} = \sqrt{26.884 + 1.032} = \sqrt{27.916} \approx 5.28 d E โ = ( โ 2.799 โ 2.386 ) 2 + ( 0.746 โ 1.762 ) 2 โ = ( โ 5.185 ) 2 + ( โ 1.016 ) 2 โ = 26.884 + 1.032 โ = 27.916 โ โ 5.28
vs W2:
d E = ( โ 2.799 โ ( โ 0.993 ) ) 2 + ( 0.746 โ ( โ 1.794 ) ) 2 = ( โ 1.806 ) 2 + ( 2.540 ) 2 = 3.262 + 6.452 = 9.714 โ 3.12 ย Smallest d_E = \sqrt{(-2.799 - (-0.993))^2 + (0.746 - (-1.794))^2} = \sqrt{(-1.806)^2 + (2.540)^2} = \sqrt{3.262 + 6.452} = \sqrt{9.714} \approx 3.12\ \text{Smallest} d E โ = ( โ 2.799 โ ( โ 0.993 ) ) 2 + ( 0.746 โ ( โ 1.794 ) ) 2 โ = ( โ 1.806 ) 2 + ( 2.540 ) 2 โ = 3.262 + 6.452 โ = 9.714 โ โ 3.12 ย Smallest
vs W3:
d E = ( โ 2.799 โ ( โ 7.260 ) ) 2 + ( 0.746 โ 2.544 ) 2 = ( 4.461 ) 2 + ( โ 1.798 ) 2 = 19.900 + 3.233 = 23.133 โ 4.81 d_E = \sqrt{(-2.799 - (-7.260))^2 + (0.746 - 2.544)^2} = \sqrt{(4.461)^2 + (-1.798)^2} = \sqrt{19.900 + 3.233} = \sqrt{23.133} \approx 4.81 d E โ = ( โ 2.799 โ ( โ 7.260 ) ) 2 + ( 0.746 โ 2.544 ) 2 โ = ( 4.461 ) 2 + ( โ 1.798 ) 2 โ = 19.900 + 3.233 โ = 23.133 โ โ 4.81
โ T2 is classified as Class W2 (d = 3.12, smallest)
Test Sample T3 = (โ7.429, 2.329)โ
vs W1:
d E = ( โ 7.429 โ 2.386 ) 2 + ( 2.329 โ 1.762 ) 2 = ( โ 9.815 ) 2 + ( 0.567 ) 2 = 96.334 + 0.321 = 96.655 โ 9.83 d_E = \sqrt{(-7.429 - 2.386)^2 + (2.329 - 1.762)^2} = \sqrt{(-9.815)^2 + (0.567)^2} = \sqrt{96.334 + 0.321} = \sqrt{96.655} \approx 9.83 d E โ = ( โ 7.429 โ 2.386 ) 2 + ( 2.329 โ 1.762 ) 2 โ = ( โ 9.815 ) 2 + ( 0.567 ) 2 โ = 96.334 + 0.321 โ = 96.655 โ โ 9.83
vs W2:
d E = ( โ 7.429 โ ( โ 0.993 ) ) 2 + ( 2.329 โ ( โ 1.794 ) ) 2 = ( โ 6.436 ) 2 + ( 4.123 ) 2 = 41.422 + 16.999 = 58.421 โ 7.64 d_E = \sqrt{(-7.429 - (-0.993))^2 + (2.329 - (-1.794))^2} = \sqrt{(-6.436)^2 + (4.123)^2} = \sqrt{41.422 + 16.999} = \sqrt{58.421} \approx 7.64 d E โ = ( โ 7.429 โ ( โ 0.993 ) ) 2 + ( 2.329 โ ( โ 1.794 ) ) 2 โ = ( โ 6.436 ) 2 + ( 4.123 ) 2 โ = 41.422 + 16.999 โ = 58.421 โ โ 7.64
vs W3:
d E = ( โ 7.429 โ ( โ 7.260 ) ) 2 + ( 2.329 โ 2.544 ) 2 = ( โ 0.169 ) 2 + ( โ 0.215 ) 2 = 0.029 + 0.046 = 0.075 โ 0.27 ย Smallest d_E = \sqrt{(-7.429 - (-7.260))^2 + (2.329 - 2.544)^2} = \sqrt{(-0.169)^2 + (-0.215)^2} = \sqrt{0.029 + 0.046} = \sqrt{0.075} \approx 0.27\ \text{Smallest} d E โ = ( โ 7.429 โ ( โ 7.260 ) ) 2 + ( 2.329 โ 2.544 ) 2 โ = ( โ 0.169 ) 2 + ( โ 0.215 ) 2 โ = 0.029 + 0.046 โ = 0.075 โ โ 0.27 ย Smallest
โ T3 is classified as Class W3 (d = 0.27, smallest)
๐๏ธ Part 2 โ City Block (Manhattan) Distanceโ
Formula:
d C B ( x , ย ฮผ ) = โฃ x 1 โ ฮผ 1 โฃ + โฃ x 2 โ ฮผ 2 โฃ d_{CB}(x,\ \mu) = |x_1 - \mu_1| + |x_2 - \mu_2| d C B โ ( x , ย ฮผ ) = โฃ x 1 โ โ ฮผ 1 โ โฃ + โฃ x 2 โ โ ฮผ 2 โ โฃ
Sometimes called the L1 norm or Manhattan distance โ it sums the absolute differences along each axis (like city blocks on a grid).
Test Sample T1 = (2.543, 0.046)โ
vs W1:
d C B = โฃ 2.543 โ 2.386 โฃ + โฃ 0.046 โ 1.762 โฃ = 0.157 + 1.716 = 1.873 ย Smallest d_{CB} = |2.543 - 2.386| + |0.046 - 1.762| = 0.157 + 1.716 = 1.873\ \text{Smallest} d C B โ = โฃ2.543 โ 2.386โฃ + โฃ0.046 โ 1.762โฃ = 0.157 + 1.716 = 1.873 ย Smallest
vs W2:
d C B = โฃ 2.543 โ ( โ 0.993 ) โฃ + โฃ 0.046 โ ( โ 1.794 ) โฃ = 3.536 + 1.840 = 5.376 d_{CB} = |2.543 - (-0.993)| + |0.046 - (-1.794)| = 3.536 + 1.840 = 5.376 d C B โ = โฃ2.543 โ ( โ 0.993 ) โฃ + โฃ0.046 โ ( โ 1.794 ) โฃ = 3.536 + 1.840 = 5.376
vs W3:
d C B = โฃ 2.543 โ ( โ 7.260 ) โฃ + โฃ 0.046 โ 2.544 โฃ = 9.803 + 2.498 = 12.301 d_{CB} = |2.543 - (-7.260)| + |0.046 - 2.544| = 9.803 + 2.498 = 12.301 d C B โ = โฃ2.543 โ ( โ 7.260 ) โฃ + โฃ0.046 โ 2.544โฃ = 9.803 + 2.498 = 12.301
โ T1 is classified as Class W1 (d = 1.873, smallest)
Test Sample T2 = (โ2.799, 0.746)โ
vs W1:
d C B = โฃ โ 2.799 โ 2.386 โฃ + โฃ 0.746 โ 1.762 โฃ = 5.185 + 1.016 = 6.201 d_{CB} = |-2.799 - 2.386| + |0.746 - 1.762| = 5.185 + 1.016 = 6.201 d C B โ = โฃ โ 2.799 โ 2.386โฃ + โฃ0.746 โ 1.762โฃ = 5.185 + 1.016 = 6.201
vs W2:
d C B = โฃ โ 2.799 โ ( โ 0.993 ) โฃ + โฃ 0.746 โ ( โ 1.794 ) โฃ = 1.806 + 2.540 = 4.346 ย Smallest d_{CB} = |-2.799 - (-0.993)| + |0.746 - (-1.794)| = 1.806 + 2.540 = 4.346\ \text{Smallest} d C B โ = โฃ โ 2.799 โ ( โ 0.993 ) โฃ + โฃ0.746 โ ( โ 1.794 ) โฃ = 1.806 + 2.540 = 4.346 ย Smallest
vs W3:
d C B = โฃ โ 2.799 โ ( โ 7.260 ) โฃ + โฃ 0.746 โ 2.544 โฃ = 4.461 + 1.798 = 6.259 d_{CB} = |-2.799 - (-7.260)| + |0.746 - 2.544| = 4.461 + 1.798 = 6.259 d C B โ = โฃ โ 2.799 โ ( โ 7.260 ) โฃ + โฃ0.746 โ 2.544โฃ = 4.461 + 1.798 = 6.259
โ T2 is classified as Class W2 (d = 4.346, smallest)
Test Sample T3 = (โ7.429, 2.329)โ
vs W1:
d C B = โฃ โ 7.429 โ 2.386 โฃ + โฃ 2.329 โ 1.762 โฃ = 9.815 + 0.567 = 10.382 d_{CB} = |-7.429 - 2.386| + |2.329 - 1.762| = 9.815 + 0.567 = 10.382 d C B โ = โฃ โ 7.429 โ 2.386โฃ + โฃ2.329 โ 1.762โฃ = 9.815 + 0.567 = 10.382
vs W2:
d C B = โฃ โ 7.429 โ ( โ 0.993 ) โฃ + โฃ 2.329 โ ( โ 1.794 ) โฃ = 6.436 + 4.123 = 10.559 d_{CB} = |-7.429 - (-0.993)| + |2.329 - (-1.794)| = 6.436 + 4.123 = 10.559 d C B โ = โฃ โ 7.429 โ ( โ 0.993 ) โฃ + โฃ2.329 โ ( โ 1.794 ) โฃ = 6.436 + 4.123 = 10.559
vs W3:
d C B = โฃ โ 7.429 โ ( โ 7.260 ) โฃ + โฃ 2.329 โ 2.544 โฃ = 0.169 + 0.215 = 0.384 ย Smallest d_{CB} = |-7.429 - (-7.260)| + |2.329 - 2.544| = 0.169 + 0.215 = 0.384\ \text{Smallest} d C B โ = โฃ โ 7.429 โ ( โ 7.260 ) โฃ + โฃ2.329 โ 2.544โฃ = 0.169 + 0.215 = 0.384 ย Smallest
โ T3 is classified as Class W3 (d = 0.384, smallest)
๐ Part 3 โ Mahalanobis Distanceโ
Formula:
d M d ( x , ย ฮผ ) = ( x โ ฮผ ) T ฮฃ โ 1 ( x โ ฮผ ) d_{Md}(x,\ \mu) = \sqrt{(x - \mu)^T \Sigma^{-1} (x - \mu)} d M d โ ( x , ย ฮผ ) = ( x โ ฮผ ) T ฮฃ โ 1 ( x โ ฮผ ) โ
Since the features are uncorrelated (ฯ = 0), the covariance matrix is diagonal :
ฮฃ = ( ฯ X 1 2 0 0 ฯ X 2 2 ) โ ฮฃ โ 1 = ( 1 / ฯ X 1 2 0 0 1 / ฯ X 2 2 ) \Sigma = \begin{pmatrix} \sigma_{X1}^2 & 0 \\ 0 & \sigma_{X2}^2 \end{pmatrix} \Rightarrow \Sigma^{-1} = \begin{pmatrix} 1/\sigma_{X1}^2 & 0 \\ 0 & 1/\sigma_{X2}^2 \end{pmatrix} ฮฃ = ( ฯ X 1 2 โ 0 โ 0 ฯ X 2 2 โ โ ) โ ฮฃ โ 1 = ( 1/ ฯ X 1 2 โ 0 โ 0 1/ ฯ X 2 2 โ โ )
This simplifies the Mahalanobis distance to:
d M d = ( x 1 โ ฮผ 1 ) 2 ฯ X 1 2 + ( x 2 โ ฮผ 2 ) 2 ฯ X 2 2 d_{Md} = \sqrt{\frac{(x_1 - \mu_1)^2}{\sigma_{X1}^2} + \frac{(x_2 - \mu_2)^2}{\sigma_{X2}^2}} d M d โ = ฯ X 1 2 โ ( x 1 โ โ ฮผ 1 โ ) 2 โ + ฯ X 2 2 โ ( x 2 โ โ ฮผ 2 โ ) 2 โ โ
The key difference from Euclidean: each dimension is normalized by its variance , so classes with high spread along an axis are not penalized unfairly.
Computing Covariance Matricesโ
Standard deviation formula:
ฯ = 1 n โ 1 โ k = 1 n ( x k โ x ห ) 2 \sigma = \sqrt{\frac{1}{n-1}\sum_{k=1}^{n}(x_k - \bar{x})^2} ฯ = n โ 1 1 โ k = 1 โ n โ ( x k โ โ x ห ) 2 โ
From the lecture notes, the computed variances are:
ฮฃ 1 = ( 4.22 0 0 4.90 ) , ฮฃ 2 = ( 10.13 0 0 2.77 ) , ฮฃ 3 = ( 26.26 0 0 2.00 ) \Sigma_1 = \begin{pmatrix} 4.22 & 0 \\ 0 & 4.90 \end{pmatrix}, \quad \Sigma_2 = \begin{pmatrix} 10.13 & 0 \\ 0 & 2.77 \end{pmatrix}, \quad \Sigma_3 = \begin{pmatrix} 26.26 & 0 \\ 0 & 2.00 \end{pmatrix} ฮฃ 1 โ = ( 4.22 0 โ 0 4.90 โ ) , ฮฃ 2 โ = ( 10.13 0 โ 0 2.77 โ ) , ฮฃ 3 โ = ( 26.26 0 โ 0 2.00 โ )
Test Sample T1 = (2.543, 0.046)โ
vs W1 (ฯยฒ_X1 = 4.22, ฯยฒ_X2 = 4.90):
d M d = ( 2.543 โ 2.386 ) 2 4.22 + ( 0.046 โ 1.762 ) 2 4.90 = 0.025 4.22 + 2.945 4.90 = 0.006 + 0.601 = 0.607 โ 0.779 ย Smallest d_{Md} = \sqrt{\frac{(2.543 - 2.386)^2}{4.22} + \frac{(0.046 - 1.762)^2}{4.90}} = \sqrt{\frac{0.025}{4.22} + \frac{2.945}{4.90}} = \sqrt{0.006 + 0.601} = \sqrt{0.607} \approx 0.779\ \text{Smallest} d M d โ = 4.22 ( 2.543 โ 2.386 ) 2 โ + 4.90 ( 0.046 โ 1.762 ) 2 โ โ = 4.22 0.025 โ + 4.90 2.945 โ โ = 0.006 + 0.601 โ = 0.607 โ โ 0.779 ย Smallest
vs W2 (ฯยฒ_X1 = 10.13, ฯยฒ_X2 = 2.77):
d M d = ( 2.543 + 0.993 ) 2 10.13 + ( 0.046 + 1.794 ) 2 2.77 = 12.503 10.13 + 3.386 2.77 = 1.234 + 1.222 = 2.456 โ 1.567 d_{Md} = \sqrt{\frac{(2.543 + 0.993)^2}{10.13} + \frac{(0.046 + 1.794)^2}{2.77}} = \sqrt{\frac{12.503}{10.13} + \frac{3.386}{2.77}} = \sqrt{1.234 + 1.222} = \sqrt{2.456} \approx 1.567 d M d โ = 10.13 ( 2.543 + 0.993 ) 2 โ + 2.77 ( 0.046 + 1.794 ) 2 โ โ = 10.13 12.503 โ + 2.77 3.386 โ โ = 1.234 + 1.222 โ = 2.456 โ โ 1.567
vs W3 (ฯยฒ_X1 = 26.26, ฯยฒ_X2 = 2.00):
d M d = ( 2.543 + 7.260 ) 2 26.26 + ( 0.046 โ 2.544 ) 2 2.00 = 96.099 26.26 + 6.240 2.00 = 3.660 + 3.120 = 6.780 โ 2.604 d_{Md} = \sqrt{\frac{(2.543 + 7.260)^2}{26.26} + \frac{(0.046 - 2.544)^2}{2.00}} = \sqrt{\frac{96.099}{26.26} + \frac{6.240}{2.00}} = \sqrt{3.660 + 3.120} = \sqrt{6.780} \approx 2.604 d M d โ = 26.26 ( 2.543 + 7.260 ) 2 โ + 2.00 ( 0.046 โ 2.544 ) 2 โ โ = 26.26 96.099 โ + 2.00 6.240 โ โ = 3.660 + 3.120 โ = 6.780 โ โ 2.604
โ T1 is classified as Class W1 (d = 0.779, smallest)
Test Sample T2 = (โ2.799, 0.746)โ
vs W1 (ฯยฒ_X1 = 4.22, ฯยฒ_X2 = 4.90):
d M d = ( โ 2.799 โ 2.386 ) 2 4.22 + ( 0.746 โ 1.762 ) 2 4.90 = 26.884 4.22 + 1.032 4.90 = 6.370 + 0.211 = 6.581 โ 2.565 d_{Md} = \sqrt{\frac{(-2.799 - 2.386)^2}{4.22} + \frac{(0.746 - 1.762)^2}{4.90}} = \sqrt{\frac{26.884}{4.22} + \frac{1.032}{4.90}} = \sqrt{6.370 + 0.211} = \sqrt{6.581} \approx 2.565 d M d โ = 4.22 ( โ 2.799 โ 2.386 ) 2 โ + 4.90 ( 0.746 โ 1.762 ) 2 โ โ = 4.22 26.884 โ + 4.90 1.032 โ โ = 6.370 + 0.211 โ = 6.581 โ โ 2.565
vs W2 (ฯยฒ_X1 = 10.13, ฯยฒ_X2 = 2.77):
d M d = ( โ 2.799 + 0.993 ) 2 10.13 + ( 0.746 + 1.794 ) 2 2.77 = 3.262 10.13 + 6.452 2.77 = 0.322 + 2.329 = 2.651 โ 1.628 d_{Md} = \sqrt{\frac{(-2.799 + 0.993)^2}{10.13} + \frac{(0.746 + 1.794)^2}{2.77}} = \sqrt{\frac{3.262}{10.13} + \frac{6.452}{2.77}} = \sqrt{0.322 + 2.329} = \sqrt{2.651} \approx 1.628 d M d โ = 10.13 ( โ 2.799 + 0.993 ) 2 โ + 2.77 ( 0.746 + 1.794 ) 2 โ โ = 10.13 3.262 โ + 2.77 6.452 โ โ = 0.322 + 2.329 โ = 2.651 โ โ 1.628
vs W3 (ฯยฒ_X1 = 26.26, ฯยฒ_X2 = 2.00):
d M d = ( โ 2.799 + 7.260 ) 2 26.26 + ( 0.746 โ 2.544 ) 2 2.00 = 19.900 26.26 + 3.233 2.00 = 0.758 + 1.617 = 2.375 โ 1.541 ย Smallest d_{Md} = \sqrt{\frac{(-2.799 + 7.260)^2}{26.26} + \frac{(0.746 - 2.544)^2}{2.00}} = \sqrt{\frac{19.900}{26.26} + \frac{3.233}{2.00}} = \sqrt{0.758 + 1.617} = \sqrt{2.375} \approx 1.541\ \text{Smallest} d M d โ = 26.26 ( โ 2.799 + 7.260 ) 2 โ + 2.00 ( 0.746 โ 2.544 ) 2 โ โ = 26.26 19.900 โ + 2.00 3.233 โ โ = 0.758 + 1.617 โ = 2.375 โ โ 1.541 ย Smallest
โ T2 is classified as Class W3 (d = 1.541, smallest)
โ ๏ธ Note: Euclidean and City Block both assigned T2 โ W2, but Mahalanobis assigns T2 โ W3 . This is because W3 has a very large variance along X1 (ฯยฒ=26.26), making the large X1 gap of 4.461 less significant after normalization.
Test Sample T3 = (โ7.429, 2.329)โ
vs W1 (ฯยฒ_X1 = 4.22, ฯยฒ_X2 = 4.90):
d M d = ( โ 7.429 โ 2.386 ) 2 4.22 + ( 2.329 โ 1.762 ) 2 4.90 = 96.334 4.22 + 0.321 4.90 = 22.829 + 0.066 = 22.895 โ 4.785 d_{Md} = \sqrt{\frac{(-7.429 - 2.386)^2}{4.22} + \frac{(2.329 - 1.762)^2}{4.90}} = \sqrt{\frac{96.334}{4.22} + \frac{0.321}{4.90}} = \sqrt{22.829 + 0.066} = \sqrt{22.895} \approx 4.785 d M d โ = 4.22 ( โ 7.429 โ 2.386 ) 2 โ + 4.90 ( 2.329 โ 1.762 ) 2 โ โ = 4.22 96.334 โ + 4.90 0.321 โ โ = 22.829 + 0.066 โ = 22.895 โ โ 4.785
vs W2 (ฯยฒ_X1 = 10.13, ฯยฒ_X2 = 2.77):
d M d = ( โ 7.429 + 0.993 ) 2 10.13 + ( 2.329 + 1.794 ) 2 2.77 = 41.422 10.13 + 16.999 2.77 = 4.089 + 6.137 = 10.226 โ 3.198 d_{Md} = \sqrt{\frac{(-7.429 + 0.993)^2}{10.13} + \frac{(2.329 + 1.794)^2}{2.77}} = \sqrt{\frac{41.422}{10.13} + \frac{16.999}{2.77}} = \sqrt{4.089 + 6.137} = \sqrt{10.226} \approx 3.198 d M d โ = 10.13 ( โ 7.429 + 0.993 ) 2 โ + 2.77 ( 2.329 + 1.794 ) 2 โ โ = 10.13 41.422 โ + 2.77 16.999 โ โ = 4.089 + 6.137 โ = 10.226 โ โ 3.198
vs W3 (ฯยฒ_X1 = 26.26, ฯยฒ_X2 = 2.00):
d M d = ( โ 7.429 + 7.260 ) 2 26.26 + ( 2.329 โ 2.544 ) 2 2.00 = 0.029 26.26 + 0.046 2.00 = 0.001 + 0.023 = 0.024 โ 0.156 ย Smallest d_{Md} = \sqrt{\frac{(-7.429 + 7.260)^2}{26.26} + \frac{(2.329 - 2.544)^2}{2.00}} = \sqrt{\frac{0.029}{26.26} + \frac{0.046}{2.00}} = \sqrt{0.001 + 0.023} = \sqrt{0.024} \approx 0.156\ \text{Smallest} d M d โ = 26.26 ( โ 7.429 + 7.260 ) 2 โ + 2.00 ( 2.329 โ 2.544 ) 2 โ โ = 26.26 0.029 โ + 2.00 0.046 โ โ = 0.001 + 0.023 โ = 0.024 โ โ 0.156 ย Smallest
โ T3 is classified as Class W3 (d = 0.156, smallest)
๐ Final Results Summaryโ
Test Sample Euclidean โ City Block โ Mahalanobis โ T1 = (2.543, 0.046) W1 (1.72)W1 (1.873)W1 (0.779)T2 = (โ2.799, 0.746) W2 (3.12)W2 (4.346)W3 (1.541)T3 = (โ7.429, 2.329) W3 (0.27)W3 (0.384)W3 (0.156)
Values in parentheses are the winning (minimum) distances.
๐ก Key Concepts & Explanationsโ
Why Minimum Distance to Means?โ
Each class is summarized by its mean vector . An unknown point is classified to whichever class centroid is nearest. This is simple, fast, and effective when classes are roughly spherical and well-separated.
Euclidean vs City Blockโ
Euclidean (L2) treats distance as the "straight-line" diagonal โ it's rotationally invariant.
City Block (L1) adds absolute differences along each axis, like walking along a grid. It's not rotationally invariant but is more robust to outliers .
Why Mahalanobis is Differentโ
Euclidean distance treats all dimensions equally. If one feature has a much higher variance (spread), it dominates the distance. Mahalanobis normalizes each dimension by its variance , effectively placing all features on equal footing. This is why T2 changes classification from W2 to W3 โ W3 has enormous variance along X1 (ฯยฒ=26.26), so the large gap in X1 matters less.
When Does Mahalanobis Match Euclidean?โ
When all variances are equal (ฯยฒ_X1 = ฯยฒ_X2 = constant), Mahalanobis reduces to Euclidean distance.
Short Explanation Videoโ
Generated using NotebookLM :