CACS486 Machine Learning

Machine LearningUnit 311 min read

Classification Algorithms: Perceptron, KNN, Decision Trees

Unit 3 of Machine Learning covers three foundational supervised learning algorithms—Perceptron (linear classification), K-Nearest Neighbors (instance-based learning), and Decision Trees (rule-based splitting)—with their mathematical foundations, step-by-step implementations, and real-world applications in Nepalese tech

TAKEAWAYS

  • Perceptron is a linear classifier that learns a decision boundary using gradient descent, but fails on non-linearly separable data unless combined with kernels (e.g., SVM).
  • KNN classifies points by majority vote of their k nearest neighbors, where k controls bias-variance tradeoff: small k = high variance (overfitting), large k = high bias (underfitting).
  • Decision Trees (ID3/C4.5) split data recursively using entropy/information gain, but suffer from overfitting unless pruned (e.g., max depth = 3 for Ncell churn prediction).
  • Real-world tie: eSewa uses KNN to flag fraudulent transactions by comparing new payments to historical "normal" patterns (low k = 3 for sensitivity).
  • Visual intuition: Perceptron’s weight updates trace a path in feature space; KNN’s decision boundary is a Voronoi diagram; Decision Trees are hierarchical "if-else" rules.
  • Exam focus: Derive perceptron updates, compute KNN predictions, and build ID3 trees from scratch (given entropy tables).

1. Perceptron: The Linear Classifier

1.1 Definition and Intuition

The perceptron is the simplest supervised learning algorithm for binary classification. It models a linear decision boundary (hyperplane) in d-dimensional space: where:

  • = weight vector (normal to the hyperplane),
  • = bias term,
  • if , else .
012345k=1 (High variance)k=3 (Balanced)k=5 (High bias)
Bias-variance tradeoff in KNN (small k = overfitting, large k = underfitting)

1.2 How It Learns: Gradient Descent

The perceptron updates weights using the perceptron learning rule: where:

  • = learning rate (e.g., 0.1),
  • = true label ( or ),
  • = predicted label ().

Worked Example: Spam Filter Train a perceptron on emails with features:

  • = "contains 'free'" (1 if yes, 0 else),
  • = "contains 'urgent'" (1 if yes, 0 else). Data:
    Email Label (Spam=+1, Not=−1)
    A 1 0 +1
    B 0 1 +1
    C 0 0 −1

Steps:

  1. Initialize , , .
  2. For Email A: (error). Update:
  3. Repeat for B and C until convergence (e.g., after 3 epochs, , ).

Mermaid Diagram: Perceptron Learning Path

-1-0.8-0.6-0.4-0.20.20.40.60.810.050.10.150.20.250.30.350.4xyUpdate rule: b = b + α * (y - ŷ)Epoch 0: w=[0,0], b=0After Email A: w=[0.2,0], b=0.2After Email B: w=[0.3,0.3], b=0.4 (Converged)
Perceptron weight/bias updates (gradient descent steps)

1.3 Limitations

  • Linearly separable only: Fails on XOR or concentric circles (see IMAGE: "XOR problem non-linear" | Two classes forming an X shape).
  • No probabilistic output: Only hard decisions ( or ).
  • Sensitive to feature scaling: Normalize inputs (e.g., min-max to ).

Real-World Use in Nepal:

  • Ncell’s churn prediction: A perceptron classifies customers likely to switch providers based on call duration/features (linear boundary works if features like "average calls/day" and "data usage" separate churners clearly).

2. K-Nearest Neighbors (KNN): Lazy Learning

2.1 Definition

KNN is an instance-based algorithm:

  1. Store the entire training dataset.
  2. For a new point , find its k nearest neighbors in feature space (using Euclidean distance).
  3. Assign the majority class label among those k neighbors.

Distance Metric:

2.2 Choosing k: Bias-Variance Tradeoff

k Value Bias Variance Overfitting Risk Example Use Case
k = 1 Low High Very high Fraud detection (eSewa)
k = 3 Med Med Moderate Medical diagnosis
k = 15 High Low None Spam classification

Worked Example: Daraz Customer Segmentation Data: 6 customers with features age, monthly_spend and labels (Loyal=1, Churn=0):

Customer Age Spend (₹) Label
A 25 5000 1
B 30 3000 0
C 28 4000 1
D 40 2000 0
E 22 6000 1
F 35 2500 0

New Customer: Age=27, Spend=4500. Find k=3 neighbors:

  1. Compute distances:
    • , , .
  2. Nearest 3: C, A, B → Labels: 1, 1, 0 → Majority = 1 (Loyal).

Mermaid Diagram: KNN Voronoi Boundaries

50030010002000ABCDE
KNN Voronoi boundaries (k=3): New point E classified as Class 1 (majority of A, C, B)

2.3 Advantages/Disadvantages

Pros Cons
No training time (lazy learner) Slow prediction (scans all data)
Works for non-linear boundaries Sensitive to irrelevant features
No assumptions on data distribution Requires feature scaling

Real-World Use in Nepal:

  • eSewa Fraud Detection: Uses KNN (k=3) to flag transactions. If a new payment’s features (amount, time, location) match 3/3 known fraudulent transactions, it’s blocked.
  • Pathao Driver Matching: Finds k=5 nearest drivers (by location/speed) to assign a ride.

3. Decision Trees: Hierarchical Rules

3.1 ID3 Algorithm (Entropy-Based Splitting)

Decision trees split data recursively using information gain (reduction in entropy).

Entropy measures impurity: where = proportion of class i in set S.

Information Gain of feature A:

Worked Example: NTC Customer Complaint Routing Data:

Complaint ID Day Outlook Temperature Humidity Wind Decision (Yes=1, No=0)
1 Sunny Hot High Weak 1
2 Sunny Hot High Strong 0
3 Overcast Hot High Weak 1
4 Rain Mild High Weak 0

Steps:

  1. Calculate entropy of root:
  2. Compute IG for each feature:
    • Outlook:
      • Sunny: (2 Yes, 1 No),
      • Overcast: (1 Yes),
      • Rain: (1 No).
    • Temperature: IG = 0.152 (lower than Outlook).
  3. Split on Outlook=Sunny:
    • Left subtree: Check Wind (IG = 0.918).
    • Right subtree: Overcast → Yes; Rain → No.

Mermaid Diagram: ID3 Tree for NTC Complaints

WeakStrongWindSunnyOvercastRainOutlook
ID3 Decision Tree for NTC Complaints (Entropy-based splits)

3.2 Advantages/Disadvantages

Pros Cons
Interpretable rules Prone to overfitting
Handles non-linear relationships Sensitive to small data changes
No feature scaling needed Biased if some features dominant
023.7547.571.2595Decision Trees85Random Forests92SVM78Neural Networks95Accuracy (%) on Iris Dataset
Comparison of classifier performance (hypothetical data for discussion)

Real-World Use in Nepal:

  • Nepal Rastra Bank Loan Approval: Decision trees evaluate loan applications by splitting on criteria like "income > 50k", "credit score > 700", etc.
  • Khalti Transaction Risk: Splits transactions by amount, user history, and time to flag high-risk ones.

## In the Real World

  1. eSewa Fraud Detection

    • Algorithm: KNN (k=3) with features = [transaction amount, time of day, user location].
    • How it works: If a new payment’s features match 3/3 known fraudulent transactions (e.g., high amount + unusual time), it’s flagged for review.
    • Why KNN? Simple to implement and effective for anomaly detection in small datasets.
  2. Daraz Recommendation System

    • Algorithm: Decision Trees (or Random Forest) to predict "buy again" probability.
    • Features: [user browsing history, item category, price, discounts].
    • Example: A user who buys electronics frequently gets recommended a new smartphone if the tree splits on "category=electronics" → "price < 30k" → "buy probability = 0.85".
  3. Ncell Customer Churn Prediction

    • Algorithm: Perceptron (for linear separability) or Decision Tree (for rules).
    • Features: [average calls/day, data usage, customer tenure].
    • Real Example: If a customer’s calls drop below 5/day and data usage < 1GB/month, the tree predicts churn with 80% accuracy.

## Exam Tip

  1. Perceptron:

    • Must-know: Update rule .
    • Common pitfall: Forgetting to normalize features (e.g., age vs. income scales differ).
    • Exam question: Given a small dataset, derive the final weights after 1–2 epochs.
  2. KNN:

    • Must-know: How k affects bias/variance; Euclidean distance formula.
    • Common pitfall: Not scaling features (e.g., age in years vs. income in lakhs).
    • Exam question: Given 2D points, find the class of a new point using k=3.
  3. Decision Trees (ID3):

    • Must-know: Entropy formula, information gain calculation, and how to build the tree step-by-step.
    • Common pitfall: Stopping splits too early (overfitting) or too late (complexity).
    • Exam question: Given a table, compute IG for each feature and choose the best split.
  4. Visuals in Exams:

    • Always draw the decision boundary for perceptron/KNN.
    • For decision trees, show the tree structure with splits and leaf nodes labeled by class.
  5. Real-World Connection:

    • Link KNN to fraud detection (eSewa) or medical diagnosis.
    • Link decision trees to loan approval (NRB) or customer segmentation (Daraz).

Based on the TU BCA syllabus for Machine Learning (CACS486), unit 3.

Discussion

Loading…