Skip to main content

Unit 3 Cheat Sheet: Classification & Instance-Based Learning

Course - Machine Learning Algorithms (21CSC555J)
Assessment - Continuous Test 2 (CT-2)
Scope - Naïve Bayes Spam Filtering, Classifier Selection (SVM vs KNN vs NB), and K-Nearest Neighbors
Primary Reference - Unit 3 Full Exam Solutions


1. Exam Overview & Fast Mapping (40 Marks)​

QuestionAlgorithm & TopicCore Numerical ShortcutEssential Theory Keywords
Q1.a (15M)Naïve Bayes: Spam FilteringP(Win∣NotSpam)=0.0  ⟹  L(NotSpam)=0.0  ⟹  100% SpamP(\text{Win} \mid \text{NotSpam}) = 0.0 \implies \mathcal{L}(\text{NotSpam}) = 0.0 \implies \mathbf{100\% \text{ Spam}}Class-conditional independence: P(X1,X2∣C)=∏P(Xi∣C)P(X_1, X_2 \mid C) = \prod P(X_i \mid C)
Q1.b (5M)Classifier Trade-offs: Medical DataMnemonic S-K-N: SVM for Shapes, KNN Killed by noise, NB for NullsInfinite-dimensional Hilbert space, Curse of dimensionality, Marginalization
Q2 (20M)KNN (K=3K=3): Customer SpendingDistances to (40,15)(40, 15): C3 & C5 tie at 11.1811.18, C4 at 16.55  ⟹  2 High vs 1 Low16.55 \implies \mathbf{2 \text{ High vs } 1 \text{ Low}}Scale dominance, Overfitting (K=1K=1) vs Underfitting (K→NK \to N), Recommenders

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

Step 1: Prior Probabilities (1 Mark)​

  • Total N=4  ⟹  NSpam=2,  NNotSpam=2N = 4 \implies N_{\text{Spam}} = 2, \; N_{\text{NotSpam}} = 2
  • P(Spam)=24=0.5,P(NotSpam)=24=0.5P(\text{Spam}) = \frac{2}{4} = \mathbf{0.5}, \qquad P(\text{NotSpam}) = \frac{2}{4} = \mathbf{0.5}

Step 2: Conditional Probabilities Table (5 Marks)​

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)}

Feature (XiX_i)ValueP(Xi∣Spam)P(X_i \mid \text{Spam})P(Xi∣NotSpam)P(X_i \mid \text{NotSpam})Exam Memory Note
Contains 'Offer'Yes12=0.5\frac{1}{2} = \mathbf{0.5}12=0.5\frac{1}{2} = \mathbf{0.5}Balanced across both classes
Contains 'Win'Yes22=1.0\frac{2}{2} = \mathbf{1.0}02=0.0\frac{0}{2} = \mathbf{0.0}Zero likelihood for NotSpam!

Step 3: Joint Likelihoods for Query: Offer=Yes, Win=Yes (5 Marks)​

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

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

  • Evidence & Posterior Normalization: P(X)=0.25+0.0=0.25P(\mathbf{X}) = 0.25 + 0.0 = 0.25 P(Spam∣X)=0.250.25=1.0(100%),P(NotSpam∣X)=0.00.25=0.0(0%)P(\text{Spam} \mid \mathbf{X}) = \frac{0.25}{0.25} = \mathbf{1.0 \quad (100\%)}, \qquad P(\text{NotSpam} \mid \mathbf{X}) = \frac{0.0}{0.25} = \mathbf{0.0 \quad (0\%)}

Step 4: Prediction & Theory (5 Marks)​

  • Prediction: By MAP rule (arg⁡max⁡\arg\max), since 1.0>0.01.0 > 0.0, predicted class is Spam.
  • Independence Assumption: Assumes features are mutually conditionally independent given the class label: P(X1,X2∣C)=P(X1∣C)⋅P(X2∣C)P(X_1, X_2 \mid C) = P(X_1 \mid C) \cdot P(X_2 \mid C)
  • Why "Naïve": Keywords correlate in real natural language (promotional words like "Offer" and "Win" co-occur).
  • Why It Works: Accurate classification only requires correct ranking (arg⁡max⁡\arg\max), not calibrated probabilities. It reduces parameter complexity from O(2d)O(2^d) to O(d)O(d).

3. Question 1.b: Classifier Trade-offs (5 Marks)​

Remember the "S - K - N" rule:

  • (a) Non-Linear Boundaries (2M): SVM with RBF Kernel

    • Infinite-Dimensional Mapping: Projects inputs into infinite-dimensional Hilbert space via K(x,x′)=exp⁡(−γ∥x−x′∥2)K(\mathbf{x}, \mathbf{x}') = \exp(-\gamma \|\mathbf{x} - \mathbf{x}'\|^2).
    • Smooth Decision Surfaces: A linear hyperplane in this space maps to smooth, continuous non-linear contours.
  • (b) Most Sensitive to Irrelevant Features (1.5M): KNN (k=5k=5)

    • 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}).
  • (c) Best for Small Data + Missing Values (1.5M): Gaussian Naïve Bayes

    • Parameter Efficiency: Estimates only 22 parameters per feature (μ,σ2\mu, \sigma^2), resisting overfitting on small samples.
    • Native Missing Value Handling: Computes likelihoods by simply omitting missing attributes from conditional product: P=∏j∈obsP(xj∣C)P = \prod_{j \in \text{obs}} P(x_j \mid C).

4. Question 2: KNN Customer Spending (20 Marks)​

  • Query Point: Xnew=(Income=40, Spending=15)\mathbf{X}_{\text{new}} = (\text{Income}=40, \, \text{Spending}=15)
  • Euclidean Distance Formula: d=(x2−x1)2+(y2−y1)2=(40−xi1)2+(15−xi2)2d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2} = \sqrt{(40 - x_{i1})^2 + (15 - x_{i2})^2}
  • Manhattan Distance Formula: dM=∣x2−x1∣+∣y2−y1∣=∣40−xi1∣+∣15−xi2∣d_M = \lvert x_2 - x_1 \rvert + \lvert y_2 - y_1 \rvert = \lvert 40 - x_{i1} \rvert + \lvert 15 - x_{i2} \rvert

Arithmetic Accelerator Table (Distances to All Points)​

CustomerClassIncome (x1x_1)Spending (x2x_2)Euclidean Distance (dEd_E)Manhattan Distance (dMd_M)
C1Low205500≈22.36\sqrt{500} \approx \mathbf{22.36}30\mathbf{30}
C2Low257289=17.00\sqrt{289} = \mathbf{17.00} (Exact!)23\mathbf{23}
C3High5020125≈11.18\sqrt{125} \approx \mathbf{11.18}15\mathbf{15}
C4High5522274≈16.55\sqrt{274} \approx \mathbf{16.55}22\mathbf{22}
C5Low3010125≈11.18\sqrt{125} \approx \mathbf{11.18}15\mathbf{15}
note

Speed Memory Hooks

  • 152+82=225+64=289=172  ⟹  15^2 + 8^2 = 225 + 64 = 289 = 17^2 \implies C2 has exact integer distance 17.00.
  • 102+52=125  ⟹  10^2 + 5^2 = 125 \implies C3 and C5 have identical distance 11.18 (Tie for Rank 1 & 2).

Ranking & Majority Voting (K=3K=3)​

  1. Top-3 Nearest Neighbors:
    • Rank 1 (Tie): C3 (dE≈11.18d_E \approx 11.18)   ⟹  \implies High
    • Rank 2 (Tie): C5 (dE≈11.18d_E \approx 11.18)   ⟹  \implies Low
    • Rank 3: C4 (dE≈16.55d_E \approx 16.55)   ⟹  \implies High
  2. Majority Voting: 2 High2 \text{ High} vs 1 Low  ⟹  1 \text{ Low} \implies Predicted class: High Spender (P=2/3≈66.7%P = 2/3 \approx 66.7\%).
  3. Manhattan Comparison: Sorted distances are C3 (1515), C5 (1515), C4 (2222)   ⟹  \implies Same top-3 set {C3,C5,C4}  ⟹  \{C3, C5, C4\} \implies Prediction is topologically invariant.

Theory Scoring Points​

  • Feature Scaling (1.5 Marks): Features with larger numeric ranges dominate Euclidean distances, collapsing multidimensional space into a 1D space. Fix with Min-Max Normalization ([0,1][0, 1]) or Z-Score Standardization.
  • Choice of KK (1.5 Marks):
    • K=1K = 1: Low Bias, High Variance   ⟹  \implies captures noise and outliers (Overfitting).
    • K→NK \to N: High Bias, Low Variance   ⟹  \implies overly smooth, predicts majority class (Underfitting).
    • Rule: Choose odd KK to prevent ties (K≈NK \approx \sqrt{N}).
  • Real-World Application (2 Marks): Recommender Systems / Collaborative Filtering (e.g., Amazon, Spotify). Preferred because lazy learning requires zero offline training time (instant profile ingestion) and intuitive neighborhood matching.

5. Golden Numbers & Exam Traps​

Quick Number Reference​

  • Q1.a: Likelihood Spam =0.25= 0.25, Likelihood NotSpam =0.0= 0.0, Posteriors =100%= 100\% Spam vs 0%0\% NotSpam.
  • Q2: C3 & C5 =11.18= 11.18 (Manhattan 1515), C4 =16.55= 16.55 (Manhattan 2222), C2 =17.00= 17.00 (Manhattan 2323), C1 =22.36= 22.36 (Manhattan 3030).
danger

Avoid These Common Traps

  1. Forgetting Prior P(C)P(C) in Bayes: Don't just multiply feature likelihoods; always include P(Spam)=0.5P(\text{Spam}) = 0.5.
  2. Missing Normalization: Always show P(Spam∣X)=0.250.25=1.0P(\text{Spam} \mid \mathbf{X}) = \frac{0.25}{0.25} = 1.0 so the posterior sums to 11.
  3. Omitting the Square Root in KNN: Δ2=125  ⟹  d=125≈11.18\Delta^2 = 125 \implies d = \sqrt{125} \approx 11.18.
  4. K=1K=1 is NOT High Bias: Remember: K=1K=1 is complex and sensitive to noise   ⟹  \implies Low Bias, High Variance.