Skip to main content

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 W1Class W2Class W3
X1X2X1X2X1X2
Sample 12.4912.1674.218−2.075−2.5200.483
Sample 21.0530.667−1.156−2.992−12.1633.161
Sample 35.7923.425−4.4251.408−13.4382.414
Sample 42.045−1.467−1.467−2.838−4.4672.298
Sample 50.5504.020−2.137−2.473−3.7114.364

Predict the class label for the following test samples using:

  1. Euclidean Distance
  2. City Block (Manhattan) Distance
  3. Mahalanobis Distance
  4. Cosine Distance
Test SampleX1X2
T12.5430.046
T2−2.7990.746
T3−7.4292.329

🧮 Step 0 — Compute Class Mean Vectors

The classifier compares each test sample to the mean (centroid) of each class.

Formula:

μk=1ni=1nxi(k)\mu_k = \frac{1}{n}\sum_{i=1}^{n} x_i^{(k)}

Class W1 Mean (μ₁)

μ1,X1=2.491+1.053+5.792+2.045+0.5505=11.9315=2.386\mu_{1,X1} = \frac{2.491 + 1.053 + 5.792 + 2.045 + 0.550}{5} = \frac{11.931}{5} = 2.386 μ1,X2=2.167+0.667+3.425+(1.467)+4.0205=8.8125=1.762\mu_{1,X2} = \frac{2.167 + 0.667 + 3.425 + (-1.467) + 4.020}{5} = \frac{8.812}{5} = 1.762 μ1=(2.386, 1.762)\boxed{\mu_1 = (2.386,\ 1.762)}

Class W2 Mean (μ₂)

μ2,X1=4.218+(1.156)+(4.425)+(1.467)+(2.137)5=4.9675=0.993\mu_{2,X1} = \frac{4.218 + (-1.156) + (-4.425) + (-1.467) + (-2.137)}{5} = \frac{-4.967}{5} = -0.993 μ2,X2=(2.075)+(2.992)+1.408+(2.838)+(2.473)5=8.9705=1.794\mu_{2,X2} = \frac{(-2.075) + (-2.992) + 1.408 + (-2.838) + (-2.473)}{5} = \frac{-8.970}{5} = -1.794 μ2=(0.993, 1.794)\boxed{\mu_2 = (-0.993,\ -1.794)}

Class W3 Mean (μ₃)

μ3,X1=(2.520)+(12.163)+(13.438)+(4.467)+(3.711)5=36.2995=7.260\mu_{3,X1} = \frac{(-2.520) + (-12.163) + (-13.438) + (-4.467) + (-3.711)}{5} = \frac{-36.299}{5} = -7.260 μ3,X2=0.483+3.161+2.414+2.298+4.3645=12.7205=2.544\mu_{3,X2} = \frac{0.483 + 3.161 + 2.414 + 2.298 + 4.364}{5} = \frac{12.720}{5} = 2.544 μ3=(7.260, 2.544)\boxed{\mu_3 = (-7.260,\ 2.544)}

📏 Part 1 — Euclidean Distance

Formula (distance from test point x to class mean μ):

dE(x, μ)=(x1μ1)2+(x2μ2)2d_E(x,\ \mu) = \sqrt{(x_1 - \mu_1)^2 + (x_2 - \mu_2)^2}

The test sample is assigned to the class with the smallest Euclidean distance.


Test Sample T1 = (2.543, 0.046)

vs W1:

dE=(2.5432.386)2+(0.0461.762)2=(0.157)2+(1.716)2=0.025+2.945=2.9701.72 Smallestd_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}

vs W2:

dE=(2.543(0.993))2+(0.046(1.794))2=(3.536)2+(1.840)2=12.503+3.386=15.8893.99d_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

vs W3:

dE=(2.543(7.260))2+(0.0462.544)2=(9.803)2+(2.498)2=96.099+6.240=102.33910.12d_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

→ T1 is classified as Class W1 (d = 1.72, smallest)


Test Sample T2 = (−2.799, 0.746)

vs W1:

dE=(2.7992.386)2+(0.7461.762)2=(5.185)2+(1.016)2=26.884+1.032=27.9165.28d_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

vs W2:

dE=(2.799(0.993))2+(0.746(1.794))2=(1.806)2+(2.540)2=3.262+6.452=9.7143.12 Smallestd_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}

vs W3:

dE=(2.799(7.260))2+(0.7462.544)2=(4.461)2+(1.798)2=19.900+3.233=23.1334.81d_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

→ T2 is classified as Class W2 (d = 3.12, smallest)


Test Sample T3 = (−7.429, 2.329)

vs W1:

dE=(7.4292.386)2+(2.3291.762)2=(9.815)2+(0.567)2=96.334+0.321=96.6559.83d_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

vs W2:

dE=(7.429(0.993))2+(2.329(1.794))2=(6.436)2+(4.123)2=41.422+16.999=58.4217.64d_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

vs W3:

dE=(7.429(7.260))2+(2.3292.544)2=(0.169)2+(0.215)2=0.029+0.046=0.0750.27 Smallestd_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}

→ T3 is classified as Class W3 (d = 0.27, smallest)


🏙️ Part 2 — City Block (Manhattan) Distance

Formula:

dCB(x, μ)=x1μ1+x2μ2d_{CB}(x,\ \mu) = |x_1 - \mu_1| + |x_2 - \mu_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:

dCB=2.5432.386+0.0461.762=0.157+1.716=1.873 Smallestd_{CB} = |2.543 - 2.386| + |0.046 - 1.762| = 0.157 + 1.716 = 1.873\ \text{Smallest}

vs W2:

dCB=2.543(0.993)+0.046(1.794)=3.536+1.840=5.376d_{CB} = |2.543 - (-0.993)| + |0.046 - (-1.794)| = 3.536 + 1.840 = 5.376

vs W3:

dCB=2.543(7.260)+0.0462.544=9.803+2.498=12.301d_{CB} = |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:

dCB=2.7992.386+0.7461.762=5.185+1.016=6.201d_{CB} = |-2.799 - 2.386| + |0.746 - 1.762| = 5.185 + 1.016 = 6.201

vs W2:

dCB=2.799(0.993)+0.746(1.794)=1.806+2.540=4.346 Smallestd_{CB} = |-2.799 - (-0.993)| + |0.746 - (-1.794)| = 1.806 + 2.540 = 4.346\ \text{Smallest}

vs W3:

dCB=2.799(7.260)+0.7462.544=4.461+1.798=6.259d_{CB} = |-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:

dCB=7.4292.386+2.3291.762=9.815+0.567=10.382d_{CB} = |-7.429 - 2.386| + |2.329 - 1.762| = 9.815 + 0.567 = 10.382

vs W2:

dCB=7.429(0.993)+2.329(1.794)=6.436+4.123=10.559d_{CB} = |-7.429 - (-0.993)| + |2.329 - (-1.794)| = 6.436 + 4.123 = 10.559

vs W3:

dCB=7.429(7.260)+2.3292.544=0.169+0.215=0.384 Smallestd_{CB} = |-7.429 - (-7.260)| + |2.329 - 2.544| = 0.169 + 0.215 = 0.384\ \text{Smallest}

→ T3 is classified as Class W3 (d = 0.384, smallest)


📐 Part 3 — Mahalanobis Distance

Formula:

dMd(x, μ)=(xμ)TΣ1(xμ)d_{Md}(x,\ \mu) = \sqrt{(x - \mu)^T \Sigma^{-1} (x - \mu)}

Since the features are uncorrelated (ρ = 0), the covariance matrix is diagonal:

Σ=(σX1200σX22)Σ1=(1/σX12001/σX22)\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}

This simplifies the Mahalanobis distance to:

dMd=(x1μ1)2σX12+(x2μ2)2σX22d_{Md} = \sqrt{\frac{(x_1 - \mu_1)^2}{\sigma_{X1}^2} + \frac{(x_2 - \mu_2)^2}{\sigma_{X2}^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:

σ=1n1k=1n(xkxˉ)2\sigma = \sqrt{\frac{1}{n-1}\sum_{k=1}^{n}(x_k - \bar{x})^2}

From the lecture notes, the computed variances are:

Σ1=(4.22004.90),Σ2=(10.13002.77),Σ3=(26.26002.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}

Test Sample T1 = (2.543, 0.046)

vs W1 (σ²_X1 = 4.22, σ²_X2 = 4.90):

dMd=(2.5432.386)24.22+(0.0461.762)24.90=0.0254.22+2.9454.90=0.006+0.601=0.6070.779 Smallestd_{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}

vs W2 (σ²_X1 = 10.13, σ²_X2 = 2.77):

dMd=(2.543+0.993)210.13+(0.046+1.794)22.77=12.50310.13+3.3862.77=1.234+1.222=2.4561.567d_{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

vs W3 (σ²_X1 = 26.26, σ²_X2 = 2.00):

dMd=(2.543+7.260)226.26+(0.0462.544)22.00=96.09926.26+6.2402.00=3.660+3.120=6.7802.604d_{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

→ 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):

dMd=(2.7992.386)24.22+(0.7461.762)24.90=26.8844.22+1.0324.90=6.370+0.211=6.5812.565d_{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

vs W2 (σ²_X1 = 10.13, σ²_X2 = 2.77):

dMd=(2.799+0.993)210.13+(0.746+1.794)22.77=3.26210.13+6.4522.77=0.322+2.329=2.6511.628d_{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

vs W3 (σ²_X1 = 26.26, σ²_X2 = 2.00):

dMd=(2.799+7.260)226.26+(0.7462.544)22.00=19.90026.26+3.2332.00=0.758+1.617=2.3751.541 Smallestd_{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}

→ 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):

dMd=(7.4292.386)24.22+(2.3291.762)24.90=96.3344.22+0.3214.90=22.829+0.066=22.8954.785d_{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

vs W2 (σ²_X1 = 10.13, σ²_X2 = 2.77):

dMd=(7.429+0.993)210.13+(2.329+1.794)22.77=41.42210.13+16.9992.77=4.089+6.137=10.2263.198d_{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

vs W3 (σ²_X1 = 26.26, σ²_X2 = 2.00):

dMd=(7.429+7.260)226.26+(2.3292.544)22.00=0.02926.26+0.0462.00=0.001+0.023=0.0240.156 Smallestd_{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}

→ T3 is classified as Class W3 (d = 0.156, smallest)


🔭 Part 4 — Cosine Distance

Formula:

cosθ=ABA×B\cos\theta = \frac{A \cdot B}{\|A\| \times \|B\|} Cosine Distance=1cosθ\text{Cosine Distance} = 1 - \cos\theta

Cosine similarity measures the angle between two vectors, ignoring magnitude. Cosine distance = 0 means identical direction (most similar); = 1 means orthogonal; = 2 means opposite directions.

The test sample is assigned to the class with the smallest cosine distance to the class mean.


Test Sample T1 = (2.543, 0.046)

vs W1: μ₁ = (2.386, 1.762)

AB=(2.543)(2.386)+(0.046)(1.762)=6.067+0.081=6.148A \cdot B = (2.543)(2.386) + (0.046)(1.762) = 6.067 + 0.081 = 6.148 A=(2.543)2+(0.046)2=6.467+0.002=6.4692.544\|A\| = \sqrt{(2.543)^2 + (0.046)^2} = \sqrt{6.467 + 0.002} = \sqrt{6.469} \approx 2.544 B=(2.386)2+(1.762)2=5.693+3.105=8.7982.966\|B\| = \sqrt{(2.386)^2 + (1.762)^2} = \sqrt{5.693 + 3.105} = \sqrt{8.798} \approx 2.966 cosθ=6.1482.544×2.966=6.1487.5470.815\cos\theta = \frac{6.148}{2.544 \times 2.966} = \frac{6.148}{7.547} \approx 0.815 Cosine Distance=10.815=0.185 Smallest\text{Cosine Distance} = 1 - 0.815 = 0.185\ \text{Smallest}

vs W2: μ₂ = (-0.993, -1.794)

AB=(2.543)(0.993)+(0.046)(1.794)=2.525+(0.083)=2.608A \cdot B = (2.543)(-0.993) + (0.046)(-1.794) = -2.525 + (-0.083) = -2.608 B=(0.993)2+(1.794)2=0.986+3.218=4.2042.050\|B\| = \sqrt{(0.993)^2 + (1.794)^2} = \sqrt{0.986 + 3.218} = \sqrt{4.204} \approx 2.050 cosθ=2.6082.544×2.050=2.6085.2150.500\cos\theta = \frac{-2.608}{2.544 \times 2.050} = \frac{-2.608}{5.215} \approx -0.500 Cosine Distance=1(0.500)=1.500\text{Cosine Distance} = 1 - (-0.500) = 1.500

vs W3: μ₃ = (-7.260, 2.544)

AB=(2.543)(7.260)+(0.046)(2.544)=18.462+0.117=18.345A \cdot B = (2.543)(-7.260) + (0.046)(2.544) = -18.462 + 0.117 = -18.345 B=(7.260)2+(2.544)2=52.708+6.472=59.1807.693\|B\| = \sqrt{(7.260)^2 + (2.544)^2} = \sqrt{52.708 + 6.472} = \sqrt{59.180} \approx 7.693 cosθ=18.3452.544×7.693=18.34519.5710.937\cos\theta = \frac{-18.345}{2.544 \times 7.693} = \frac{-18.345}{19.571} \approx -0.937 Cosine Distance=1(0.937)=1.937\text{Cosine Distance} = 1 - (-0.937) = 1.937

→ T1 is classified as Class W1 (distance = 0.185, smallest)


Test Sample T2 = (−2.799, 0.746)

vs W1: μ₁ = (2.386, 1.762)

AB=(2.799)(2.386)+(0.746)(1.762)=6.678+1.314=5.364A \cdot B = (-2.799)(2.386) + (0.746)(1.762) = -6.678 + 1.314 = -5.364 A=(2.799)2+(0.746)2=7.834+0.557=8.3912.897\|A\| = \sqrt{(2.799)^2 + (0.746)^2} = \sqrt{7.834 + 0.557} = \sqrt{8.391} \approx 2.897 cosθ=5.3642.897×2.966=5.3648.5910.624\cos\theta = \frac{-5.364}{2.897 \times 2.966} = \frac{-5.364}{8.591} \approx -0.624 Cosine Distance=1(0.624)=1.624\text{Cosine Distance} = 1 - (-0.624) = 1.624

vs W2: μ₂ = (-0.993, -1.794)

AB=(2.799)(0.993)+(0.746)(1.794)=2.779+(1.338)=1.441A \cdot B = (-2.799)(-0.993) + (0.746)(-1.794) = 2.779 + (-1.338) = 1.441 cosθ=1.4412.897×2.050=1.4415.9390.243\cos\theta = \frac{1.441}{2.897 \times 2.050} = \frac{1.441}{5.939} \approx 0.243 Cosine Distance=10.243=0.757\text{Cosine Distance} = 1 - 0.243 = 0.757

vs W3: μ₃ = (-7.260, 2.544)

AB=(2.799)(7.260)+(0.746)(2.544)=20.321+1.898=22.219A \cdot B = (-2.799)(-7.260) + (0.746)(2.544) = 20.321 + 1.898 = 22.219 A=(2.799)2+(0.746)2=7.834+0.557=8.3912.897\|A\| = \sqrt{(2.799)^2 + (0.746)^2} = \sqrt{7.834 + 0.557} = \sqrt{8.391} \approx 2.897 cosθ=22.2192.897×7.693=22.21922.2870.997\cos\theta = \frac{22.219}{2.897 \times 7.693} = \frac{22.219}{22.287} \approx 0.997 Cosine Distance=10.997=0.003 Smallest\text{Cosine Distance} = 1 - 0.997 = 0.003\ \text{Smallest}

→ T2 is classified as Class W3 (distance = 0.003, smallest)

⚠️ Note: Euclidean and City Block assign T2 → W2, Mahalanobis assigns T2 → W3, and Cosine Distance also assigns T2 → W3. Cosine distance cares about direction, not magnitude — T2 points in almost the same direction as μ₃.


Test Sample T3 = (−7.429, 2.329)

vs W1: μ₁ = (2.386, 1.762)

AB=(7.429)(2.386)+(2.329)(1.762)=17.726+4.104=13.622A \cdot B = (-7.429)(2.386) + (2.329)(1.762) = -17.726 + 4.104 = -13.622 A=(7.429)2+(2.329)2=55.190+5.424=60.6147.786\|A\| = \sqrt{(7.429)^2 + (2.329)^2} = \sqrt{55.190 + 5.424} = \sqrt{60.614} \approx 7.786 cosθ=13.6227.786×2.966=13.62223.0900.590\cos\theta = \frac{-13.622}{7.786 \times 2.966} = \frac{-13.622}{23.090} \approx -0.590 Cosine Distance=1(0.590)=1.590\text{Cosine Distance} = 1 - (-0.590) = 1.590

vs W2: μ₂ = (-0.993, -1.794)

AB=(7.429)(0.993)+(2.329)(1.794)=7.377+(4.178)=3.199A \cdot B = (-7.429)(-0.993) + (2.329)(-1.794) = 7.377 + (-4.178) = 3.199 cosθ=3.1997.786×2.050=3.19915.9610.200\cos\theta = \frac{3.199}{7.786 \times 2.050} = \frac{3.199}{15.961} \approx 0.200 Cosine Distance=10.200=0.800\text{Cosine Distance} = 1 - 0.200 = 0.800

vs W3: μ₃ = (-7.260, 2.544)

AB=(7.429)(7.260)+(2.329)(2.544)=53.935+5.926=59.861A \cdot B = (-7.429)(-7.260) + (2.329)(2.544) = 53.935 + 5.926 = 59.861 cosθ=59.8617.786×7.693=59.86159.8920.999\cos\theta = \frac{59.861}{7.786 \times 7.693} = \frac{59.861}{59.892} \approx 0.999 Cosine Distance=10.999=0.001 Smallest\text{Cosine Distance} = 1 - 0.999 = 0.001\ \text{Smallest}

→ T3 is classified as Class W3 (distance = 0.001, smallest)


📊 Final Results Summary

Test SampleEuclidean →City Block →Mahalanobis →Cosine →
T1 = (2.543, 0.046)W1 (1.72)W1 (1.873)W1 (0.779)W1 (0.185)
T2 = (−2.799, 0.746)W2 (3.12)W2 (4.346)W3 (1.541)W3 (0.003)
T3 = (−7.429, 2.329)W3 (0.27)W3 (0.384)W3 (0.156)W3 (0.001)

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.

Cosine Distance vs Euclidean Distance

  • Cosine distance measures direction (angle between vectors), ignoring magnitude — two vectors pointing the same way have distance ≈ 0 even if one is much longer.
  • Euclidean distance measures absolute position in space — a long vector pointing the same direction as a short one still has a large distance.
  • Cosine distance is especially useful in high-dimensional spaces (text classification, NLP) where magnitude varies wildly but direction carries the signal.

Short Explanation Video

Generated using NotebookLM: