Supervised Learning

GATE Relevance: Very High

🎯 Learning Objectives

  • Distinguish classification from regression
  • Derive and apply Linear Regression (normal equations and gradient descent)
  • Understand Logistic Regression, sigmoid function, and cross-entropy loss
  • Build Decision Trees using entropy, information gain, and Gini impurity
  • Explain SVM, KNN, Naive Bayes, and Random Forests

1. Classification vs Regression

Supervised learning problems fall into two categories based on the type of output variable:

AspectClassificationRegression
Output typeDiscrete (categorical)Continuous (numerical)
GoalAssign input to a classPredict a real-valued output
ExamplesSpam detection, digit recognitionHouse price, temperature prediction
EvaluationAccuracy, F1, AUCMSE, RMSE, R²
AlgorithmsLogistic Regression, SVM, Decision TreesLinear Regression, SVR, Regression Trees

2. Linear Regression

Linear Regression models the relationship between a dependent variable y and one or more independent variables x as a linear function.

Model

For a single feature: h(x) = w₁x + w₀ (or ŷ = mx + b).
For multiple features: h(x) = wᵀx + b = w₀ + w₁x₁ + w₂x₂ + … + wₙxₙ.
In matrix form: ŷ = Xw where X has a column of 1s for the intercept.

2.1 Cost Function (MSE)

The cost function measures how well the model fits the training data:

Mean Squared Error

J(w) = (1/2n) ∑ᵢ₁ⁿ (h(xᵢ) − yᵢ)²

The factor of 1/2 is a mathematical convenience that simplifies the gradient. The goal is to find w that minimises J(w).

2.2 Normal Equations (Closed-Form Solution)

Setting the gradient of J(w) to zero and solving gives the analytical solution:

Normal Equation

w* = (XᵀX)⁻¹ Xᵀy

This directly computes the optimal weights without iteration. Time complexity: O(n·d² + d³) where n = samples and d = features.

When Normal Equations Fail

If XᵀX is singular (not invertible), the normal equation cannot be applied directly. This happens when features are linearly dependent (multicollinearity) or when d > n. Solutions: use pseudo-inverse, add regularisation, or use gradient descent.

2.3 Gradient Descent for Linear Regression

Gradient descent iteratively updates weights in the direction of steepest descent:

Gradient Descent Update Rule
Repeat until convergence:
  w_j := w_j - α · (1/n) ∑_{i=1}^{n} (h(x_i) - y_i) · x_{i,j}

where α is the learning rate.
GATE Numerical: Linear Regression

Q: Given data points (1, 2), (2, 4), (3, 5), (4, 4), (5, 5), find the best-fit line y = w₁x + w₀ using least squares.

Solution:

n=5, ∑x=15, ∑y=20, ∑xy=67, ∑x²=55, x̄=3, ȳ=4

w₁ = (n∑xy − ∑x∑y) / (n∑x² − (∑x)²) = (335 − 300) / (275 − 225) = 35/50 = 0.7

w₀ = ȳ − w₁·x̄ = 4 − 0.7×3 = 1.9

Best-fit line: y = 0.7x + 1.9

3. Logistic Regression

Despite its name, Logistic Regression is a classification algorithm used for binary classification. It models the probability that an input belongs to class 1.

Sigmoid (Logistic) Function

σ(z) = 1 / (1 + e⁻ẑ)

Properties: output range (0, 1), σ(0) = 0.5, monotonically increasing, derivative: σ′(z) = σ(z)(1 − σ(z)).

Model

P(y=1|x) = σ(wᵀx + b) = 1 / (1 + e⁻⁽ʷᵀˣ⁺ᵇ⁾)

Decision boundary: Predict class 1 if P(y=1|x) ≥ 0.5, i.e., if wᵀx + b ≥ 0.

3.1 Cross-Entropy Loss (Log Loss)

Binary Cross-Entropy Loss

J(w) = −(1/n) ∑ᵢ [yᵢ log(ŷᵢ) + (1 − yᵢ) log(1 − ŷᵢ)]

where ŷᵢ = σ(wᵀxᵢ + b). This loss is convex, ensuring gradient descent finds the global minimum.

Why not MSE for Classification?

Using MSE with the sigmoid function creates a non-convex optimisation surface with many local minima. Cross-entropy loss is convex for logistic regression, guaranteeing convergence to the global optimum.

GATE Numerical: Logistic Regression

Q: For a logistic regression model with w = [2, -1] and b = 0.5, what is the predicted probability for x = [1, 3]?

Solution: z = wᵀx + b = 2(1) + (-1)(3) + 0.5 = 2 - 3 + 0.5 = -0.5

P(y=1|x) = σ(-0.5) = 1/(1 + e⁰·⁵) = 1/(1 + 1.6487) = 1/2.6487 ≈ 0.3775

Since 0.3775 < 0.5, predict class 0.

4. Decision Trees

Decision Trees partition the feature space by recursively splitting on feature values. Each internal node represents a test on a feature, each branch represents an outcome, and each leaf node represents a class label (classification) or value (regression).

4.1 Entropy

Entropy (Shannon Entropy)

H(S) = −∑ᵢ pᵢ log₂(pᵢ)

where pᵢ is the proportion of class i in dataset S. Entropy measures the impurity or disorder of a set.

  • H(S) = 0 when all elements belong to one class (pure)
  • H(S) = 1 for binary classification with equal proportions (maximum impurity)

4.2 Information Gain

Information Gain (ID3 Algorithm)

IG(S, A) = H(S) − ∑ᵥ (|Sᵥ|/|S|) · H(Sᵥ)

where A is the attribute being tested, and Sᵥ is the subset of S for which attribute A has value v. The attribute with the highest information gain is chosen for splitting.

4.3 Gini Impurity

Gini Impurity (CART Algorithm)

Gini(S) = 1 − ∑ᵢ pᵢ²

Gini impurity ranges from 0 (pure) to 0.5 (for binary classification with equal proportions). CART uses Gini impurity by default.

GATE Numerical: Information Gain

Q: A dataset has 14 examples: 9 positive, 5 negative. Attribute A splits them into: A=yes (6+, 2−) and A=no (3+, 3−). Compute Information Gain.

Solution:

H(S) = −(9/14)log₂(9/14) − (5/14)log₂(5/14) = 0.940 bits

H(A=yes) = −(6/8)log₂(6/8) − (2/8)log₂(2/8) = 0.811 bits

H(A=no) = −(3/6)log₂(3/6) − (3/6)log₂(3/6) = 1.000 bits

IG(S, A) = 0.940 − (8/14)×0.811 − (6/14)×1.000 = 0.940 − 0.463 − 0.429 = 0.048 bits

4.4 ID3, C4.5, and CART

FeatureID3C4.5CART
Split criterionInformation GainGain RatioGini Impurity
Feature typesCategorical onlyCategorical + ContinuousCategorical + Continuous
Tree typeMulti-way splitsMulti-way splitsBinary splits only
PruningNo built-in pruningError-based pruningCost-complexity pruning
Missing valuesCannot handleHandles missing valuesSurrogate splits

5. Random Forests

Random Forest is an ensemble method that builds multiple decision trees and aggregates their predictions.

Random Forest Algorithm
For i = 1 to B (number of trees):
  1. Draw a bootstrap sample D_i of size n from training data (with replacement)
  2. Build a decision tree T_i on D_i:
     - At each node, randomly select m features (m << d)
     - Choose the best split among these m features
     - Grow tree fully (no pruning)
  3. Add T_i to the forest

Prediction:
  Classification: Majority vote across all B trees
  Regression: Average prediction across all B trees

Key hyperparameters: B (number of trees, typically 100–500), m (features per split, typically √d for classification, d/3 for regression).

Why Random Forests Work

By introducing randomness (bootstrap sampling + feature subset selection), the individual trees become decorrelated. Averaging decorrelated estimators reduces variance without increasing bias, leading to better generalisation than a single tree.

6. Support Vector Machines (SVM)

SVM finds the hyperplane that maximally separates the classes. The data points closest to the hyperplane are called support vectors.

6.1 Hard Margin SVM

Optimisation Problem

Minimise: (1/2)||w||²
Subject to: yᵢ(wᵀxᵢ + b) ≥ 1 for all i

The margin is 2/||w||. Maximising the margin is equivalent to minimising ||w||².

6.2 Soft Margin SVM

When data is not linearly separable, we introduce slack variables ξᵢ to allow some misclassifications:

Soft Margin Formulation

Minimise: (1/2)||w||² + C ∑ᵢ ξᵢ
Subject to: yᵢ(wᵀxᵢ + b) ≥ 1 − ξᵢ, ξᵢ ≥ 0

C is a regularisation parameter: large C → less tolerance for violations (may overfit), small C → wider margin with more violations (may underfit).

6.3 The Kernel Trick

For non-linearly separable data, SVM maps inputs to a higher-dimensional space using a kernel function, where a linear separator may exist.

KernelFormula K(x, x′)Use Case
Linearxᵀx′Linearly separable data
Polynomial(xᵀx′ + c)ᵈNon-linear boundaries, degree d
RBF (Gaussian)exp(−γ||x − x′||²)Most common; works well in practice
Sigmoidtanh(α xᵀx′ + c)Similar to neural network

7. K-Nearest Neighbours (KNN)

KNN is a non-parametric, instance-based algorithm. It stores all training data and classifies new points by majority vote among the K nearest neighbours.

KNN Algorithm
Input: Training set D, query point x, integer K
1. Compute distance from x to every point in D
2. Select the K nearest points (neighbours)
3. Classification: Return the majority class among K neighbours
   Regression: Return the average value of K neighbours

Distance metrics: Euclidean: √(∑(xᵢ − yᵢ)²), Manhattan: ∑|xᵢ − yᵢ|, Minkowski (generalisation).

Choosing K

K=1: Highly sensitive to noise (overfitting). Large K: Smooth decision boundary (underfitting). K should be odd for binary classification to avoid ties. Typical choice: K = √n. Always use cross-validation to select K.

GATE Numerical: KNN

Q: Points: A(1,1) Class+, B(2,2) Class+, C(5,5) Class−, D(6,6) Class−. Classify query point Q(3,3) using K=3 with Euclidean distance.

Solution:

d(Q,A) = √((3-1)²+(3-1)²) = √8 = 2.83

d(Q,B) = √((3-2)²+(3-2)²) = √2 = 1.41

d(Q,C) = √((3-5)²+(3-5)²) = √8 = 2.83

d(Q,D) = √((3-6)²+(3-6)²) = √18 = 4.24

3 nearest: B(+), A(+), C(−). Majority: Class +

8. Naive Bayes Classifier

Naive Bayes applies Bayes’ theorem with the “naive” assumption that features are conditionally independent given the class.

Bayes’ Theorem for Classification

P(C|x) = P(x|C) · P(C) / P(x)

With the naive independence assumption:

P(x|C) = ∏ᵤ P(xᵤ|C)

Classify as: C* = argmaxↄ P(C) ∏ᵤ P(xᵤ|C)

GATE Numerical: Naive Bayes

Q: Given: P(Spam)=0.4, P(Ham)=0.6. For the word “free”: P(free|Spam)=0.8, P(free|Ham)=0.1. For the word “meeting”: P(meeting|Spam)=0.1, P(meeting|Ham)=0.5. Classify the email containing “free meeting”.

Solution:

P(Spam|free, meeting) ∝ P(Spam) · P(free|Spam) · P(meeting|Spam) = 0.4 × 0.8 × 0.1 = 0.032

P(Ham|free, meeting) ∝ P(Ham) · P(free|Ham) · P(meeting|Ham) = 0.6 × 0.1 × 0.5 = 0.030

Since 0.032 > 0.030, classify as Spam.

Normalised: P(Spam) = 0.032/(0.032+0.030) = 0.032/0.062 ≈ 0.516

9. Algorithm Comparison Summary

AlgorithmTypeParametric?StrengthsWeaknesses
Linear RegressionRegressionYesSimple, interpretable, fastAssumes linearity
Logistic RegressionClassificationYesProbabilistic output, convex lossLinear boundary only
Decision TreeBothNoInterpretable, handles non-linearProne to overfitting
Random ForestBothNoRobust, reduced varianceLess interpretable
SVMBothYes*Effective in high dimensionsSlow for large datasets
KNNBothNoSimple, no training phaseSlow prediction, curse of dimensionality
Naive BayesClassificationYesFast, works with small dataIndependence assumption

*SVM with linear kernel is parametric; with RBF kernel it is non-parametric.