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)
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
| Contains 'Offer' | Contains 'Win' | Spam | |
|---|---|---|---|
| E1 | Yes | Yes | Yes |
| E2 | No | Yes | Yes |
| E3 | Yes | No | No |
| E4 | No | No | No |
- i) Calculate the conditional probabilities (5 marks)
, , , . - ii) For a new email with "Offer = Yes, Win = Yes", compute (5 marks)
and - 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)
-
Step 1: Dataset Partitioning & Class Totals (1 Mark)
- Total number of training samples:
- Total Spam emails (): (Emails: E1, E2)
- Total Not Spam emails (): (Emails: E3, E4)
-
Step 2: General Formula for Maximum Likelihood Conditional Probability (1 Mark)
-
Step 3: Evaluate Conditional Probabilities for Spam Class (1.5 Marks)
- For feature 'Offer' = Yes: (Email E1 has Offer=Yes; E2 has Offer=No)
- For feature 'Win' = Yes: (Both emails E1 and E2 have Win=Yes)
-
Step 4: Evaluate Conditional Probabilities for Not Spam Class (1.5 Marks)
- For feature 'Offer' = Yes: (Email E3 has Offer=Yes; E4 has Offer=No)
- For feature 'Win' = Yes: (Neither E3 nor E4 has Win=Yes)
Conditional Probability Summary Table
| Feature () | Value | ||
|---|---|---|---|
| Contains 'Offer' | Yes | ||
| Contains 'Win' | Yes |
(ii) Compute Probabilities for New Email: (5 Marks)
-
Step 1: Compute Prior Class Probabilities (1 Mark)
-
Step 2: State Bayes' Posterior Formula with Conditional Independence (1 Mark)
-
Step 3: Calculate Unnormalized Joint Likelihoods (1.5 Marks)
-
For class Spam:
-
For class Not Spam:
-
-
Step 4: Compute Evidence (Marginal Probability ) (0.5 Marks)
-
Step 5: Normalize to Obtain Final Posterior Probabilities (1 Mark)
(iii) Class Prediction & Independence Assumption (5 Marks)
-
Class Prediction (2 Marks):
- MAP Decision Rule:
- Since , 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:
- 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 (), not calibrated values. It reduces parameter complexity from to , preventing overfitting.
Question 1.b: Comparative Analysis of Classifiers on Medical Data (5 Marks)
Question Statement
A data scientist is working on three models for classifying medical test results:
- KNN ()
- 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 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 () (1.5 Marks)
- Algorithm: K-Nearest Neighbors (KNN)
- Why (Key Points):
- Uniform Distance Metric: Calculates Euclidean distance equally across all features: .
- Curse of Dimensionality: Irrelevant dimensions inject random noise, collapsing distance contrast () 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 parameters per feature (), 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: .
Quick Comparison Summary
| Criterion | Winner | Core Reason |
|---|---|---|
| Non-Linear Boundary (2M) | SVM (RBF) | Kernel trick creates smooth boundaries in infinite-dimensional space. |
| Sensitive to Irrelevant Features (1.5M) | KNN () | Euclidean distance equally sums noise from all dimensions. |
| Small Data + Missing Values (1.5M) | Gaussian NB | Few parameters () + naturally omits missing features in product. |
Question 2: K-Nearest Neighbors Customer Spending Classification (20 Marks)
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:
| Customer | Income (₹1000s) | Spending (₹1000s) | Class |
|---|---|---|---|
| C1 | 20 | 5 | Low |
| C2 | 25 | 7 | Low |
| C3 | 50 | 20 | High |
| C4 | 55 | 22 | High |
| C5 | 30 | 10 | Low |
- (a) For a new customer with and , calculate the Euclidean distance from each training point. (5 Marks)
- (b) Predict the class when . 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 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: where represents Income (in ₹1000s) and represents Spending (in ₹1000s).
(a) Euclidean Distance Calculation (5 Marks)
-
Step 1: Formula Definition (1 Mark)
- Euclidean Distance Formula between two points and :
- Applied to Query Point and Training Point :
-
Step 2: Individual Distance Computations (3 Marks)
-
For Customer C1 :
-
For Customer C2 :
-
For Customer C3 :
-
For Customer C4 :
-
For Customer C5 :
-
-
Step 3: Distance Summary Table (1 Mark)
| Customer | Income () | Spending () | Class | Euclidean Distance () |
|---|---|---|---|---|
| C1 | 20 | 5 | Low | |
| C2 | 25 | 7 | Low | |
| C3 | 50 | 20 | High | |
| C4 | 55 | 22 | High | |
| C5 | 30 | 10 | Low |
(b) Predict Class When (5 Marks)
- Step 1: Rank Distances in Ascending Order (1.5 Marks)
| Rank | Customer | Euclidean Distance () | Class Label | Included in ? |
|---|---|---|---|---|
| 1 | C3 | High | Yes | |
| 2 | C5 | Low | Yes | |
| 3 | C4 | High | Yes | |
| 4 | C2 | Low | No | |
| 5 | C1 | Low | No |
-
Step 2: Identify the Nearest Neighbors (1.5 Marks)
- Nearest Neighbor 1: C3 with Class = High
- Nearest Neighbor 2: C5 with Class = Low
- Nearest Neighbor 3: C4 with Class = High
-
Step 3: Majority Voting & Posterior Probability (1 Mark)
- Vote count for Class High: (from C3, C4)
- Vote count for Class Low: (from C5)
- Empirical class probability:
-
Step 4: Final Prediction Statement (1 Mark)
- By majority voting rule (), the new customer with and is predicted as:
(c) Recalculate Distances Using Manhattan Metric & Compare (5 Marks)
-
Step 1: Formula Definition (1 Mark)
- Manhattan Distance Formula between two points and :
- Applied to Query Point and Training Point :
-
Step 2: Individual Computations (2 Marks)
- For C1 :
- For C2 :
- For C3 :
- For C4 :
- For C5 :
-
Step 3: Metric Comparison Table (1 Mark)
| Customer | Class | Euclidean Distance () | Euclidean Rank | Manhattan Distance () | Manhattan Rank |
|---|---|---|---|---|---|
| C3 | High | 1 (Tie) | 1 (Tie) | ||
| C5 | Low | 1 (Tie) | 1 (Tie) | ||
| C4 | High | 3 | 3 | ||
| C2 | Low | 4 | 4 | ||
| C1 | Low | 5 | 5 |
- Step 4: Analytical Comparison & Conclusion (1 Mark)
- Set Invariance: The set of nearest neighbors under Manhattan distance is identical: .
- Tally & Outcome: The majority vote remains vs .
- Conclusion: The prediction remains unchanged as High Spender. This demonstrates that the local neighborhood around query point is topologically stable across both and 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 () or Z-Score Standardization so all dimensions contribute proportionally.
-
Effect of Parameter (1.5 Marks):
- Too Small (): Low Bias, High Variance sensitive to noise and outliers, causing Overfitting (jagged boundaries).
- Too Large (): High Bias, Low Variance overly smooth boundaries, degenerating into global majority voting (Underfitting).
- Tip: Choose an odd to break ties (rule of thumb: ).
(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.