Skip to main content

Unit 3: Classification & Instance-Based Learning

Course - Machine Learning Algorithms (21CSC555J)
Assessment - Continuous Test 2 (CT-2)
Unit - Unit 3: Naïve Bayes & K-Nearest Neighbors
Total Weightage - 40 Marks (Question 1.a: 15 Marks; Question 1.b: 5 Marks; Question 2: 20 Marks)


Question 1.a: Naïve Bayes Spam Filtering (15 Marks)​

info

Question Statement

An email filtering system uses Naïve Bayes to classify messages as Spam or Not Spam. Given the training dataset:

Training Dataset for Spam Classification

EmailContains 'Offer'Contains 'Win'Spam
E1YesYesYes
E2NoYesYes
E3YesNoNo
E4NoNoNo
  • i) Calculate the conditional probabilities (5 marks)
    P(Offer∣Spam)P(\text{Offer} \mid \text{Spam}), P(Win∣Spam)P(\text{Win} \mid \text{Spam}), P(Offer∣NotSpam)P(\text{Offer} \mid \text{NotSpam}), P(Win∣NotSpam)P(\text{Win} \mid \text{NotSpam}).
  • ii) For a new email with "Offer = Yes, Win = Yes", compute (5 marks)
    P(Spam∣Email)P(\text{Spam} \mid \text{Email}) and P(NotSpam∣Email)P(\text{NotSpam} \mid \text{Email})
  • iii) Predict the class and briefly explain the independence assumption. (5 marks)

Solution 1.a​

📝 Exam-Ready Answer (Structured for 15 Marks)​

(i) Calculate Conditional Probabilities (5 Marks)​
  1. Step 1: Dataset Partitioning & Class Totals (1 Mark)

    • Total number of training samples: N=4N = 4
    • Total Spam emails (C=SpamC = \text{Spam}): NSpam=2N_{\text{Spam}} = 2 (Emails: E1, E2)
    • Total Not Spam emails (C=NotSpamC = \text{NotSpam}): NNotSpam=2N_{\text{NotSpam}} = 2 (Emails: E3, E4)
  2. Step 2: General Formula for Maximum Likelihood Conditional Probability (1 Mark) P(Xi=x∣C=c)=Count(Xi=x and C=c)Count(C=c)P(X_i = x \mid C = c) = \frac{\text{Count}(X_i = x \text{ and } C = c)}{\text{Count}(C = c)}

  3. Step 3: Evaluate Conditional Probabilities for Spam Class (1.5 Marks)

    • For feature 'Offer' = Yes: P(Offer=Yes∣Spam)=Count(Offer=Yes and Spam)Count(Spam)=12=0.5P(\text{Offer} = \text{Yes} \mid \text{Spam}) = \frac{\text{Count}(\text{Offer}=\text{Yes} \text{ and } \text{Spam})}{\text{Count}(\text{Spam})} = \frac{1}{2} = \mathbf{0.5} (Email E1 has Offer=Yes; E2 has Offer=No)
    • For feature 'Win' = Yes: P(Win=Yes∣Spam)=Count(Win=Yes and Spam)Count(Spam)=22=1.0P(\text{Win} = \text{Yes} \mid \text{Spam}) = \frac{\text{Count}(\text{Win}=\text{Yes} \text{ and } \text{Spam})}{\text{Count}(\text{Spam})} = \frac{2}{2} = \mathbf{1.0} (Both emails E1 and E2 have Win=Yes)
  4. Step 4: Evaluate Conditional Probabilities for Not Spam Class (1.5 Marks)

    • For feature 'Offer' = Yes: P(Offer=Yes∣NotSpam)=Count(Offer=Yes and NotSpam)Count(NotSpam)=12=0.5P(\text{Offer} = \text{Yes} \mid \text{NotSpam}) = \frac{\text{Count}(\text{Offer}=\text{Yes} \text{ and } \text{NotSpam})}{\text{Count}(\text{NotSpam})} = \frac{1}{2} = \mathbf{0.5} (Email E3 has Offer=Yes; E4 has Offer=No)
    • For feature 'Win' = Yes: P(Win=Yes∣NotSpam)=Count(Win=Yes and NotSpam)Count(NotSpam)=02=0.0P(\text{Win} = \text{Yes} \mid \text{NotSpam}) = \frac{\text{Count}(\text{Win}=\text{Yes} \text{ and } \text{NotSpam})}{\text{Count}(\text{NotSpam})} = \frac{0}{2} = \mathbf{0.0} (Neither E3 nor E4 has Win=Yes)
Conditional Probability Summary Table​
Feature (XiX_i)ValueP(Xi∣Spam)P(X_i \mid \text{Spam})P(Xi∣NotSpam)P(X_i \mid \text{NotSpam})
Contains 'Offer'Yes1/2=0.51/2 = \mathbf{0.5}1/2=0.51/2 = \mathbf{0.5}
Contains 'Win'Yes2/2=1.02/2 = \mathbf{1.0}0/2=0.00/2 = \mathbf{0.0}

(ii) Compute Probabilities for New Email: Xnew=(Offer=Yes,Win=Yes)\mathbf{X}_{\text{new}} = (\text{Offer} = \text{Yes}, \text{Win} = \text{Yes}) (5 Marks)​
  1. Step 1: Compute Prior Class Probabilities (1 Mark) P(Spam)=NSpamN=24=0.5P(\text{Spam}) = \frac{N_{\text{Spam}}}{N} = \frac{2}{4} = \mathbf{0.5} P(NotSpam)=NNotSpamN=24=0.5P(\text{NotSpam}) = \frac{N_{\text{NotSpam}}}{N} = \frac{2}{4} = \mathbf{0.5}

  2. Step 2: State Bayes' Posterior Formula with Conditional Independence (1 Mark) P(C∣X)=P(X∣C)⋅P(C)P(X)=P(Offer=Yes∣C)⋅P(Win=Yes∣C)⋅P(C)P(X)P(C \mid \mathbf{X}) = \frac{P(\mathbf{X} \mid C) \cdot P(C)}{P(\mathbf{X})} = \frac{P(\text{Offer}=\text{Yes} \mid C) \cdot P(\text{Win}=\text{Yes} \mid C) \cdot P(C)}{P(\mathbf{X})}

  3. Step 3: Calculate Unnormalized Joint Likelihoods (1.5 Marks)

    • For class Spam: L(Spam)=P(Offer=Yes∣Spam)×P(Win=Yes∣Spam)×P(Spam)\mathcal{L}(\text{Spam}) = P(\text{Offer}=\text{Yes} \mid \text{Spam}) \times P(\text{Win}=\text{Yes} \mid \text{Spam}) \times P(\text{Spam}) L(Spam)=0.5×1.0×0.5=0.25\mathcal{L}(\text{Spam}) = 0.5 \times 1.0 \times 0.5 = \mathbf{0.25}

    • For class Not Spam: L(NotSpam)=P(Offer=Yes∣NotSpam)×P(Win=Yes∣NotSpam)×P(NotSpam)\mathcal{L}(\text{NotSpam}) = P(\text{Offer}=\text{Yes} \mid \text{NotSpam}) \times P(\text{Win}=\text{Yes} \mid \text{NotSpam}) \times P(\text{NotSpam}) L(NotSpam)=0.5×0.0×0.5=0.0\mathcal{L}(\text{NotSpam}) = 0.5 \times 0.0 \times 0.5 = \mathbf{0.0}

  4. Step 4: Compute Evidence (Marginal Probability P(X)P(\mathbf{X})) (0.5 Marks) P(X)=L(Spam)+L(NotSpam)=0.25+0.0=0.25P(\mathbf{X}) = \mathcal{L}(\text{Spam}) + \mathcal{L}(\text{NotSpam}) = 0.25 + 0.0 = \mathbf{0.25}

  5. Step 5: Normalize to Obtain Final Posterior Probabilities (1 Mark) P(Spam∣Email)=L(Spam)P(X)=0.250.25=1.0(100%)P(\text{Spam} \mid \text{Email}) = \frac{\mathcal{L}(\text{Spam})}{P(\mathbf{X})} = \frac{0.25}{0.25} = \mathbf{1.0} \quad (100\%) P(NotSpam∣Email)=L(NotSpam)P(X)=0.00.25=0.0(0%)P(\text{NotSpam} \mid \text{Email}) = \frac{\mathcal{L}(\text{NotSpam})}{P(\mathbf{X})} = \frac{0.0}{0.25} = \mathbf{0.0} \quad (0\%)


(iii) Class Prediction & Independence Assumption (5 Marks)​
  • Class Prediction (2 Marks):

    • MAP Decision Rule: y^=arg⁡max⁡cP(C=c∣X)\hat{y} = \arg\max_{c} P(C = c \mid \mathbf{X})
    • Since P(Spam∣Email)=1.0>P(NotSpam∣Email)=0.0P(\text{Spam} \mid \text{Email}) = 1.0 > P(\text{NotSpam} \mid \text{Email}) = 0.0, the email is classified as Spam.
  • Class-Conditional Independence Assumption (1.5 Marks):

    • Formal Definition: All features are assumed conditionally independent given the class label: P(X1,X2,…,Xd∣C)=∏j=1dP(Xj∣C)P(X_1, X_2, \dots, X_d \mid C) = \prod_{j=1}^d P(X_j \mid C)
    • In this context, knowing the email contains 'Offer' provides zero information about 'Win', once the class is fixed.
  • Practical Validity & Trade-offs (1.5 Marks):

    • Why "Naïve": Real-world text features are strongly correlated (e.g., "Offer" and "Win" co-occur in spam).
    • Why It Works: Classification only requires the correct probability ranking (arg⁡max⁡\arg\max), not calibrated values. It reduces parameter complexity from O(2d)O(2^d) to O(d)O(d), preventing overfitting.

Question 1.b: Comparative Analysis of Classifiers on Medical Data (5 Marks)​

info

Question Statement

A data scientist is working on three models for classifying medical test results:

  • KNN (k=5k=5)
  • Gaussian Naïve Bayes
  • SVM with RBF kernel

Assume given is a small dataset with numeric features:

  • a. Which algorithm handles non-linear boundaries best and why? (2 Marks)
  • b. Which algorithm is most sensitive to irrelevant features? (1.5 Marks)
  • c. Which algorithm performs better when data is small and has missing values? (1.5 Marks)

Solution 1.b​

📝 Exam-Ready Answer (Crisp 5-Mark Structure)​

(a) Best for Non-Linear Boundaries: SVM with RBF Kernel (2 Marks)​
  • Algorithm: SVM with RBF Kernel
  • Why (Key Points):
    • Infinite-Dimensional Mapping: The RBF kernel K(x,x′)=exp⁡(−γ∥x−x′∥2)K(\mathbf{x}, \mathbf{x}') = \exp(-\gamma \|\mathbf{x} - \mathbf{x}'\|^2) projects inputs into an infinite-dimensional Hilbert space.
    • Smooth Decision Surfaces: A linear hyperplane in this transformed space maps back to smooth, continuous non-linear contours in the original feature space.
(b) Most Sensitive to Irrelevant Features: KNN (k=5k=5) (1.5 Marks)​
  • Algorithm: K-Nearest Neighbors (KNN)
  • Why (Key Points):
    • Uniform Distance Metric: Calculates Euclidean distance equally across all DD features: d=∑j=1D(xj−yj)2d = \sqrt{\sum_{j=1}^D (x_j - y_j)^2}.
    • Curse of Dimensionality: Irrelevant dimensions inject random noise, collapsing distance contrast (dmax⁡≈dmin⁡d_{\max} \approx d_{\min}) and destroying neighborhood ranking.
(c) Best for Small Data and Missing Values: Gaussian Naïve Bayes (1.5 Marks)​
  • Algorithm: Gaussian Naïve Bayes
  • Why (Key Points):
    • Parameter Efficiency: Estimates only 22 parameters per feature (μ,σ2\mu, \sigma^2), resisting overfitting on small samples via high bias/low variance.
    • Native Missing Value Handling: Computes likelihoods by simply omitting missing attributes from the conditional product: P(xobs∣C)=∏j∈obsP(xj∣C)P(\mathbf{x}_{\text{obs}} \mid C) = \prod_{j \in \text{obs}} P(x_j \mid C).
Quick Comparison Summary​
CriterionWinnerCore Reason
Non-Linear Boundary (2M)SVM (RBF)Kernel trick creates smooth boundaries in infinite-dimensional space.
Sensitive to Irrelevant Features (1.5M)KNN (k=5k=5)Euclidean distance equally sums noise from all dimensions.
Small Data + Missing Values (1.5M)Gaussian NBFew parameters (μ,σ2\mu, \sigma^2) + naturally omits missing features in product.

Question 2: K-Nearest Neighbors Customer Spending Classification (20 Marks)​

info

Question Statement

A company wants to classify new customers as "High Spender" or "Low Spender" based on their income and average monthly spending.

Training data:

CustomerIncome (₹1000s)Spending (₹1000s)Class
C1205Low
C2257Low
C35020High
C45522High
C53010Low
  • (a) For a new customer with Income=40\text{Income} = 40 and Spending=15\text{Spending} = 15, calculate the Euclidean distance from each training point. (5 Marks)
  • (b) Predict the class when K=3K=3. Show all steps clearly. (5 Marks)
  • (c) Recalculate distances using the Manhattan distance metric and compare the result. (5 Marks)
  • (d) Explain: How feature scaling/normalization affects KNN results, and what happens when KK is too small or too large. (3 Marks)
  • (e) Mention one real-world application where KNN is used and why it is preferred there. (2 Marks)

Solution 2​

📝 Exam-Ready Answer (Structured for 20 Marks)​

Let the query point for the new customer be: Xnew=(x1,x2)=(40,15)\mathbf{X}_{\text{new}} = (x_1, x_2) = (40, 15) where x1x_1 represents Income (in ₹1000s) and x2x_2 represents Spending (in ₹1000s).


(a) Euclidean Distance Calculation (5 Marks)​
  1. Step 1: Formula Definition (1 Mark)

    • Euclidean Distance Formula between two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2): d=(x2−x1)2+(y2−y1)2d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}
    • Applied to Query Point (40,15)(40, 15) and Training Point (xi1,xi2)(x_{i1}, x_{i2}): dE=(40−xi1)2+(15−xi2)2d_E = \sqrt{(40 - x_{i1})^2 + (15 - x_{i2})^2}
  2. Step 2: Individual Distance Computations (3 Marks)

    • For Customer C1 (20,5)(20, 5): dE(X,C1)=(40−20)2+(15−5)2=202+102=400+100=500≈22.36d_E(\mathbf{X}, C_1) = \sqrt{(40 - 20)^2 + (15 - 5)^2} = \sqrt{20^2 + 10^2} = \sqrt{400 + 100} = \sqrt{500} \approx \mathbf{22.36}

    • For Customer C2 (25,7)(25, 7): dE(X,C2)=(40−25)2+(15−7)2=152+82=225+64=289=17.00d_E(\mathbf{X}, C_2) = \sqrt{(40 - 25)^2 + (15 - 7)^2} = \sqrt{15^2 + 8^2} = \sqrt{225 + 64} = \sqrt{289} = \mathbf{17.00}

    • For Customer C3 (50,20)(50, 20): dE(X,C3)=(40−50)2+(15−20)2=(−10)2+(−5)2=100+25=125≈11.18d_E(\mathbf{X}, C_3) = \sqrt{(40 - 50)^2 + (15 - 20)^2} = \sqrt{(-10)^2 + (-5)^2} = \sqrt{100 + 25} = \sqrt{125} \approx \mathbf{11.18}

    • For Customer C4 (55,22)(55, 22): dE(X,C4)=(40−55)2+(15−22)2=(−15)2+(−7)2=225+49=274≈16.55d_E(\mathbf{X}, C_4) = \sqrt{(40 - 55)^2 + (15 - 22)^2} = \sqrt{(-15)^2 + (-7)^2} = \sqrt{225 + 49} = \sqrt{274} \approx \mathbf{16.55}

    • For Customer C5 (30,10)(30, 10): dE(X,C5)=(40−30)2+(15−10)2=102+52=100+25=125≈11.18d_E(\mathbf{X}, C_5) = \sqrt{(40 - 30)^2 + (15 - 10)^2} = \sqrt{10^2 + 5^2} = \sqrt{100 + 25} = \sqrt{125} \approx \mathbf{11.18}

  3. Step 3: Distance Summary Table (1 Mark)

CustomerIncome (xi1x_{i1})Spending (xi2x_{i2})ClassEuclidean Distance (dEd_E)
C1205Low500≈22.36\sqrt{500} \approx \mathbf{22.36}
C2257Low289=17.00\sqrt{289} = \mathbf{17.00}
C35020High125≈11.18\sqrt{125} \approx \mathbf{11.18}
C45522High274≈16.55\sqrt{274} \approx \mathbf{16.55}
C53010Low125≈11.18\sqrt{125} \approx \mathbf{11.18}

(b) Predict Class When K=3K=3 (5 Marks)​
  1. Step 1: Rank Distances in Ascending Order (1.5 Marks)
RankCustomerEuclidean Distance (dEd_E)Class LabelIncluded in K=3K=3?
1C311.1811.18HighYes
2C511.1811.18LowYes
3C416.5516.55HighYes
4C217.0017.00LowNo
5C122.3622.36LowNo
  1. Step 2: Identify the K=3K=3 Nearest Neighbors (1.5 Marks)

    • Nearest Neighbor 1: C3 with dE=11.18d_E = 11.18   ⟹  \implies Class = High
    • Nearest Neighbor 2: C5 with dE=11.18d_E = 11.18   ⟹  \implies Class = Low
    • Nearest Neighbor 3: C4 with dE=16.55d_E = 16.55   ⟹  \implies Class = High
  2. Step 3: Majority Voting & Posterior Probability (1 Mark)

    • Vote count for Class High: 22 (from C3, C4)
    • Vote count for Class Low: 11 (from C5)
    • Empirical class probability: P(High∣X)=23≈66.67%P(\text{High} \mid \mathbf{X}) = \frac{2}{3} \approx 66.67\% P(Low∣X)=13≈33.33%P(\text{Low} \mid \mathbf{X}) = \frac{1}{3} \approx 33.33\%
  3. Step 4: Final Prediction Statement (1 Mark)

    • By majority voting rule (2>12 > 1), the new customer with Income=40\text{Income} = 40 and Spending=15\text{Spending} = 15 is predicted as: y^=High Spender\hat{y} = \text{\textbf{High Spender}}

(c) Recalculate Distances Using Manhattan Metric & Compare (5 Marks)​
  1. Step 1: Formula Definition (1 Mark)

    • Manhattan Distance Formula between two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2): dM=∣x2−x1∣+∣y2−y1∣d_M = \lvert x_2 - x_1 \rvert + \lvert y_2 - y_1 \rvert
    • Applied to Query Point (40,15)(40, 15) and Training Point (xi1,xi2)(x_{i1}, x_{i2}): dM=∣40−xi1∣+∣15−xi2∣d_M = \lvert 40 - x_{i1} \rvert + \lvert 15 - x_{i2} \rvert
  2. Step 2: Individual Computations (2 Marks)

    • For C1 (20,5)(20, 5): dM=∣40−20∣+∣15−5∣=20+10=30d_M = \lvert 40 - 20 \rvert + \lvert 15 - 5 \rvert = 20 + 10 = \mathbf{30}
    • For C2 (25,7)(25, 7): dM=∣40−25∣+∣15−7∣=15+8=23d_M = \lvert 40 - 25 \rvert + \lvert 15 - 7 \rvert = 15 + 8 = \mathbf{23}
    • For C3 (50,20)(50, 20): dM=∣40−50∣+∣15−20∣=10+5=15d_M = \lvert 40 - 50 \rvert + \lvert 15 - 20 \rvert = 10 + 5 = \mathbf{15}
    • For C4 (55,22)(55, 22): dM=∣40−55∣+∣15−22∣=15+7=22d_M = \lvert 40 - 55 \rvert + \lvert 15 - 22 \rvert = 15 + 7 = \mathbf{22}
    • For C5 (30,10)(30, 10): dM=∣40−30∣+∣15−10∣=10+5=15d_M = \lvert 40 - 30 \rvert + \lvert 15 - 10 \rvert = 10 + 5 = \mathbf{15}
  3. Step 3: Metric Comparison Table (1 Mark)

CustomerClassEuclidean Distance (dEd_E)Euclidean RankManhattan Distance (dMd_M)Manhattan Rank
C3High11.1811.181 (Tie)15151 (Tie)
C5Low11.1811.181 (Tie)15151 (Tie)
C4High16.5516.55322223
C2Low17.0017.00423234
C1Low22.3622.36530305
  1. Step 4: Analytical Comparison & Conclusion (1 Mark)
    • Set Invariance: The set of K=3K=3 nearest neighbors under Manhattan distance is identical: {C3,C5,C4}\{C_3, C_5, C_4\}.
    • Tally & Outcome: The majority vote remains 2 High2 \text{ High} vs 1 Low1 \text{ Low}.
    • Conclusion: The prediction remains unchanged as High Spender. This demonstrates that the local neighborhood around query point (40,15)(40, 15) is topologically stable across both L1L_1 and L2L_2 geometric norms.

(d) Feature Scaling & Choice of K (3 Marks)​
  • Impact of Feature Scaling (1.5 Marks):

    • Scale Dominance: KNN distance calculations are sensitive to magnitude. Features with larger ranges (e.g., Income in thousands) dominate features with smaller ranges (e.g., Spending in tens), distorting distance measurements.
    • Remedy: Apply Min-Max Normalization ([0,1][0, 1]) or Z-Score Standardization so all dimensions contribute proportionally.
  • Effect of Parameter KK (1.5 Marks):

    • KK Too Small (K=1K = 1): Low Bias, High Variance   ⟹  \implies sensitive to noise and outliers, causing Overfitting (jagged boundaries).
    • KK Too Large (K→NK \to N): High Bias, Low Variance   ⟹  \implies overly smooth boundaries, degenerating into global majority voting (Underfitting).
    • Tip: Choose an odd KK to break ties (rule of thumb: K≈NK \approx \sqrt{N}).

(e) Real-World Application of KNN (2 Marks)​
  • Application (1 Mark):
    • Recommender Systems / Collaborative Filtering (e.g., Amazon product recommendations or Spotify music matching).
  • Why Preferred (1 Mark):
    • Lazy Learning (Zero Training Cost): Stores training data directly; new users or products are ingested in real time without expensive model retraining.
    • Intuitive & Non-Parametric: Recommends items by finding users with similar consumption profiles without assuming any underlying mathematical distribution.