MACHINE LEARNING

106403
Back to MACHINE LEARNING

Unit 3: Classification & Regression

  1. Q1e. Logistic regression uses which loss function? (i) Mean Squared Error (ii) Euclidean Distance (iii) Cross-Entropy (iv) Hinge Loss20252m

    Unit 3: Classification & Regression

    Logistic regression uses which loss function?
    (i) Mean Squared Error
    (ii) Euclidean Distance
    (iii) Cross-Entropy
    (iv) Hinge Loss

    View this question on its own page →
    Worked Solution

    Answer: (iii) Cross-Entropy (Log Loss / Binary Cross-Entropy)

    Explanation:

    Logistic regression models p(y=1∣x)=σ(wTx+b)p(y=1|x)= \sigma(w^T x+b) where σ(z)=1/(1+e−z)\sigma(z)=1/(1+e^{-z}) and maximizes likelihood, equivalent to minimizing:

    J(w)=−1m∑i[yilog⁡y^i+(1−yi)log⁡(1−y^i)]J(w)= -\frac1m \sum_{i} [y_i \log \hat{y}_i + (1-y_i)\log(1-\hat{y}_i)] — Cross-Entropy / Log Loss

    • MSE (i) is for linear regression; using it with sigmoid makes optimization non-convex and slow.
    • Euclidean Distance (ii) not a classification loss.
    • Hinge Loss (iv) is for SVM: max⁡(0,1−y⋅f(x))\max(0,1-y \cdot f(x)).

    Thus (iii) Cross-Entropy is correct. It heavily penalizes confident wrong predictions and yields convex optimization.

  2. Q1f. A perceptron has weights w=[0.5, -0.5] and bias b=0. For input x=[1,1] with label y=1, the weight update with learning rate η=0.1 is (i) w=[0.6, -0.4], b=0.1 (ii) w=[0.4, -0.6], b=-0.1 (iii) w=[0.5, -0.5], b=0 (iv) w=[0.7, -0.3], b=0.220252m

    Unit 3: Classification & Regression

    A perceptron has weights w=[0.5, -0.5] and bias b=0. For input x=[1,1] with label y=1, the weight update with learning rate η=0.1 is
    (i) w=[0.6, -0.4], b=0.1
    (ii) w=[0.4, -0.6], b=-0.1
    (iii) w=[0.5, -0.5], b=0
    (iv) w=[0.7, -0.3], b=0.2

    View this question on its own page →
    Worked Solution

    Answer: (i) w=[0.6, -0.4], b=0.1

    Solution:

    Perceptron update rule: w:=w+η(y−y^)xw := w + \eta (y - \hat{y}) x, b:=b+η(y−y^)b := b + \eta (y - \hat{y})

    Given w=[0.5,−0.5],b=0,x=[1,1],y=1,η=0.1w=[0.5,-0.5], b=0, x=[1,1], y=1, \eta=0.1

    1. Compute activation: z=wTx+b=0.5∗1+(−0.5)∗1+0=0z = w^T x + b = 0.5*1 + (-0.5)*1 +0 =0
    2. Prediction: y^=step(z)=1\hat{y}= step(z) = 1 if z>0z>0 else 00 (or signsign). Here z=0z=0 → y^=0\hat{y}=0 (misclassified, since true y=1y=1). Some texts use y^∈{−1,1}\hat{y}\in\{-1,1\} with 00 as negative, same result.
    3. Error =y−y^=1−0=1= y - \hat{y}=1-0=1
    4. Δw=η∗error∗x=0.1∗1∗[1,1]=[0.1,0.1]\Delta w = \eta * error * x =0.1*1*[1,1]=[0.1,0.1]
      wnew=[0.5+0.1,−0.5+0.1]=[0.6,−0.4]w_{new}= [0.5+0.1, -0.5+0.1]=[0.6,-0.4]
    5. Δb=0.1∗1=0.1\Delta b =0.1*1=0.1, bnew=0.1b_{new}=0.1

    If model predicted correctly (y^=1\hat{y}=1), no update → (iii). But with threshold at >0>0, z=0z=0 is treated as incorrect, so update occurs → (i) is expected exam answer.

    Note: If using signsign with y∈{−1,1}y\in\{-1,1\} and z=0z=0 counted as error, same. If using y^=1\hat{y}=1 for z≥0z\ge0, answer would be (iii) no update. Most exam keys expect (i).

  3. Q1g. The kernel trick in SVM is used to (i) Map data to higher-dimensional space for linear separation (ii) Compute probabilities (iii) Reduce feature dimensions (iv) Normalize input features20252m

    Unit 3: Classification & Regression

    The kernel trick in SVM is used to
    (i) Map data to higher-dimensional space for linear separation
    (ii) Compute probabilities
    (iii) Reduce feature dimensions
    (iv) Normalize input features

    View this question on its own page →
    Worked Solution

    Answer: (i) Map data to higher-dimensional space for linear separation

    Explanation:

    SVM finds optimal linear hyperplane wTx+b=0w^T x + b =0 maximizing margin. Data not linearly separable in original space can be made separable by mapping ϕ:X→H\phi: \mathcal{X} \rightarrow \mathcal{H} to higher dimension where linear separation is possible (Cover's theorem).

    • Kernel trick: Avoids explicit ϕ(x)\phi(x) computation; computes inner products in H\mathcal{H} via kernel K(xi,xj)=ϕ(xi)Tϕ(xj)K(x_i,x_j)=\phi(x_i)^T\phi(x_j). Examples: Polynomial K=(xTy+c)dK=(x^T y +c)^d, RBF K=exp⁡(−γ∣∣x−y∣∣2)K=\exp(-\gamma||x-y||^2).

    • (ii) Compute probabilities — not kernel purpose (Platt scaling does, but not trick)

    • (iii) Reduce dimensions — opposite; it increases dimension implicitly (PCA reduces, kernel doesn't)

    • (iv) Normalize features — preprocessing, unrelated.

    Thus (i) correct.

  4. Q1h. Naive Bayes assumes (i) Features are dependent (ii) Features are independent given the class (iii) Class probabilities are equal (iv) No prior knowledge is required20252m

    Unit 3: Classification & Regression

    Naive Bayes assumes
    (i) Features are dependent
    (ii) Features are independent given the class
    (iii) Class probabilities are equal
    (iv) No prior knowledge is required

    View this question on its own page →
    Worked Solution

    Answer: (ii) Features are independent given the class

    Explanation:

    Naive Bayes uses: P(Ck∣x)∝P(Ck)∏i=1nP(xi∣Ck)P(C_k|x) \propto P(C_k)\prod_{i=1}^n P(x_i|C_k)

    Naive assumption: xi⊥xj∣Cx_i \perp x_j | C, i.e., features conditionally independent given class label. This drastically reduces parameters from O(2n)O(2^n) to O(n)O(n).

    • (i) dependent — opposite of assumption
    • (iii) class probabilities equal (P(Ck)=1/KP(C_k)=1/K) — not assumed; priors estimated from data P(Ck)=Nk/NP(C_k)=N_k/N
    • (iv) no prior — false; needs P(Ck)P(C_k) (estimated or uniform)

    Therefore (ii) is correct. The independence is "naive" because in reality features often correlate, but model still works well in practice.

  5. Q4a. Explain the concept of linear classifiers. How do they separate classes in feature space? Give examples of linearly separable and non-separable datasets.20257m

    Unit 3: Classification & Regression

    Explain the concept of linear classifiers. How do they separate classes in feature space? Give examples of linearly separable and non-separable datasets.

    View this question on its own page →
    Worked Solution

    Linear Classifiers:

    Classifier where decision boundary is linear function wTx+b=0\mathbf{w}^T\mathbf{x}+b=0. Prediction: y^=sign(wTx+b)\hat y = sign(\mathbf{w}^T\mathbf{x}+b) (binary), or softmaxsoftmax for multi-class. Hypothesis space: half-spaces.

    How they separate in feature space:

    • In Rd\mathbb{R}^d, hyperplane splits space into two half-spaces. Distance from point to hyperplane =∣wTx+b∣/∣∣w∣∣=|w^T x+b|/||w||.
    • Learning finds w,bw,b minimizing loss (perceptron, logistic, SVM hinge). Geometrically, orientation and offset adjusted to separate classes.
    • For d=2d=2, boundary is line; d=3d=3 plane; higher dd hyperplane.
    • Margin intuition (SVM): maximize distance between boundary and nearest points.

    Linearly Separable example:
    AND gate data: (0,0)→0,(0,1)→0,(1,0)→0,(1,1)→1(0,0)\to0, (0,1)\to0,(1,0)\to0,(1,1)\to1 can be separated by x1+x2−1.5=0x_1+x_2-1.5=0. Points on opposite sides perfectly separable. Iris dataset (setosa vs versicolor with sepal length/width) approx linear.

    Non-separable example:
    XOR: (0,0)→0,(1,0)→1,(0,1)→1,(1,1)→0(0,0)\to0,(1,0)\to1,(0,1)\to1,(1,1)\to0 — no single line separates. Need non-linear boundary. Concentric circles, interleaving moons. Solution: map to higher dim via features ϕ(x)=(x1,x2,x1x2)\phi(x)=(x_1,x_2,x_1x_2) or use kernel/nonlinear model (MLP with hidden layer).

    Limitation: Linear classifiers cannot capture XOR, curved boundaries without feature engineering/kernel trick. Advantage: simple, fast, interpretable, works when data approx linear or high-dim sparse (text).

  6. Q4b. Explain the Perceptron learning algorithm. How does it update weights? Discuss its convergence properties.20257m

    Unit 3: Classification & Regression

    Explain the Perceptron learning algorithm. How does it update weights? Discuss its convergence properties.

    View this question on its own page →
    Worked Solution

    Perceptron Learning Algorithm (Rosenblatt 1957):

    Linear binary classifier f(x)=sign(wTx+b)f(x)=sign(w^T x + b), y∈{−1,1}y\in\{-1,1\} (or {0,1}\{0,1\}).

    Algorithm:
    Initialize w=0,b=0w=0, b=0
    Repeat until convergence:
    For each (xi,yi)(x_i,y_i) in training (random order):
    Compute z=wTxi+bz = w^T x_i + b
    Predict y^=sign(z)\hat y = sign(z)
    If yiz≤0y_i z \le 0 (misclassified):
    w:=w+ηyixiw := w + \eta y_i x_i
    b:=b+ηyib := b + \eta y_i
    else no update
    η>0\eta>0 learning rate (often 1).

    Weight Update Intuition: Move weight vector toward misclassified positive example (+yx+y x) and away from negative, increasing score for correct class.

    With {0,1}\{0,1\} labels: w:=w+η(y−y^)xw:=w+\eta(y-\hat y)x, b:=b+η(y−y^)b:=b+\eta(y-\hat y)

    Convergence Properties:

    • Perceptron Convergence Theorem: If data is linearly separable with margin γ>0\gamma>0 and radius R=max⁡∣∣xi∣∣R=\max||x_i||, perceptron makes at most (R/γ)2(R/\gamma)^2 mistakes and converges to a separating hyperplane in finite steps, regardless of initialization or order. Does not guarantee maximum margin (unlike SVM).
    • If data not separable, algorithm never converges, cycles — use Pocket algorithm (keep best weights) or averaging.
    • Not sensitive to η\eta when separable (scaling).
    • Online algorithm, mistake-driven, O(nd)O(nd) per epoch, fast.

    Limitations: Cannot solve XOR, needs separable data; output is hard threshold, no probabilities. Extensions: kernel perceptron, voted perceptron, averaged perceptron for stability.
    Example: AND gate converges in few steps; XOR never converges without hidden layer.

  7. Q5a. Explain the concept of SVM for linear classification. Define margin, support vectors, and how the optimal hyperplane is determined.20257m

    Unit 3: Classification & Regression

    Explain the concept of SVM for linear classification. Define margin, support vectors, and how the optimal hyperplane is determined.

    View this question on its own page →
    Worked Solution

    SVM for Linear Classification:

    Goal: Find hyperplane wTx+b=0w^T x + b =0 that maximally separates classes y∈{−1,1}y\in\{-1,1\}.

    Margin: Distance from hyperplane to closest training point. For canonical hyperplane yi(wTxi+b)≥1y_i(w^T x_i+b)\ge1, geometric margin =1/∣∣w∣∣=1/||w||, total margin width =2/∣∣w∣∣=2/||w||. SVM maximizes this.

    Support Vectors: Training points lying exactly on margin boundaries yi(wTxi+b)=1y_i(w^T x_i+b)=1. They are critical — removing non-support vectors doesn't change solution. Solution w=∑αiyixiw=\sum \alpha_i y_i x_i depends only on SVs where αi>0\alpha_i>0.

    Optimal Hyperplane Determination:

    Primal (hard margin):
    min⁡w,b12∣∣w∣∣2\min_{w,b} \frac12||w||^2 s.t. yi(wTxi+b)≥1y_i(w^T x_i+b)\ge1

    For non-separable, soft margin with slack ξi\xi_i:
    min⁡w,b,ξ12∣∣w∣∣2+C∑ξi\min_{w,b,\xi} \frac12||w||^2 + C\sum \xi_i s.t. yi(wTxi+b)≥1−ξiy_i(w^T x_i+b)\ge1-\xi_i, ξi≥0\xi_i\ge0

    CC trades margin vs errors.

    Solving:
    Lagrangian dual: max⁡α∑αi−12∑i,jαiαjyiyjxiTxj\max_\alpha \sum\alpha_i -\frac12\sum_{i,j}\alpha_i\alpha_j y_i y_j x_i^T x_j s.t. 0≤αi≤C0\le\alpha_i\le C, ∑αiyi=0\sum\alpha_i y_i=0. Quadratic programming. Then w=∑αiyixiw=\sum\alpha_i y_i x_i, bb from SV (average). Decision: f(x)=sign(wTx+b)f(x)=sign(w^T x + b).

    Geometric intuition: Push two parallel supporting hyperplanes wTx+b=±1w^T x+b=\pm1 as far apart as possible without violating, supports touch boundaries. Largest margin gives best generalization (VC dimension). Kernel trick extends to non-linear by K(xi,xj)K(x_i,x_j).

  8. Q5b. Explain the backpropagation algorithm. How are weights updated in a neural network using gradient descent?20257m

    Unit 3: Classification & Regression

    Explain the backpropagation algorithm. How are weights updated in a neural network using gradient descent?

    View this question on its own page →
    Worked Solution

    Backpropagation Algorithm — Weight Update via Gradient Descent:

    For MLP with LL layers, loss E=12∑(y−o(L))2E=\frac12\sum(y-o^{(L)})^2 or cross-entropy.

    Steps:

    1. Forward: netj(l)=∑iwji(l)oi(l−1)+bj(l)net_j^{(l)}=\sum_i w_{ji}^{(l)}o_i^{(l-1)}+b_j^{(l)}, oj(l)=f(netj(l))o_j^{(l)}=f(net_j^{(l)}) where ff is sigmoid/ReLU. Propagate to output.

    2. Backward (output layer LL): Error term δj(L)=(yj−oj(L))f′(netj(L))\delta_j^{(L)} = (y_j - o_j^{(L)})f'(net_j^{(L)}) (for MSE) or (yj−oj)f′(y_j - o_j)f' generally $ \delta^{(L)} =
      abla E \odot f'(net)$.

    3. Hidden layers: δj(l)=f′(netj(l))∑kδk(l+1)wkj(l+1)\delta_j^{(l)} = f'(net_j^{(l)})\sum_k \delta_k^{(l+1)} w_{kj}^{(l+1)} — error weighted by downstream weights.

    4. Gradients: ∂E∂wji(l)=−δj(l)oi(l−1)\frac{\partial E}{\partial w_{ji}^{(l)}} = -\delta_j^{(l)} o_i^{(l-1)}, ∂E∂bj(l)=−δj(l)\frac{\partial E}{\partial b_j^{(l)}} = -\delta_j^{(l)}

    5. Update (SGD): wji(l):=wji(l)+ηδj(l)oi(l−1)w_{ji}^{(l)} := w_{ji}^{(l)} + \eta \delta_j^{(l)} o_i^{(l-1)}, bj(l):=bj(l)+ηδj(l)b_j^{(l)}:=b_j^{(l)}+ \eta \delta_j^{(l)}

    For batch: average over examples, momentum $\Delta w(t)= -\eta
    abla E + \alpha\Delta w(t-1),Adamusesadaptive, Adam uses adaptive \eta$.

    Role of Gradient Descent: Iteratively moves weights opposite gradient $w:=w-\eta
    abla Etominimizeloss,errorsurfacedescent.Learningrate to minimize loss, error surface descent. Learning rate \etacontrolsstepsize;toolargediverges,toosmallslow.Backpropmakesgradientcomputation controls step size; too large diverges, too small slow. Backprop makes gradient computation O(W)$ via chain rule, enabling deep learning.

    Variants: Vanilla SGD (per sample), mini-batch SGD, batch GD. Improvements: momentum, RMSProp, Adam, weight decay (L2).

  9. Q8(b). Apply the ID3 algorithm on the following data to draw a decision tree. Show the Information Gain at each split. ### Dataset | Outlook | Temperature | Humidity | Windy | PlayTennis | |-----------|-------------|----------|-------|-------------| | Sunny | Hot | High | False | No | | Sunny | Hot | High | True | No | | Overcast | Hot | High | False | Yes | | Rainy | Mild | High | False | Yes | | Rainy | Cool | Normal | False | Yes | | Rainy | Cool | Normal | True | No | | Overcast | Cool | Normal | True | Yes | | Sunny | Mild | High | False | No | | Sunny | Cool | Normal | False | Yes | | Rainy | Mild | Normal | False | Yes | | Sunny | Mild | Normal | True | Yes | | Overcast | Mild | High | True | Yes | | Overcast | Hot | Normal | False | Yes | | Rainy | Mild | High | True | No |202510m

    Unit 3: Classification & Regression

    Apply the ID3 algorithm on the following data to draw a decision tree. Show the Information Gain at each split.

    Dataset

    Outlook Temperature Humidity Windy PlayTennis
    Sunny Hot High False No
    Sunny Hot High True No
    Overcast Hot High False Yes
    Rainy Mild High False Yes
    Rainy Cool Normal False Yes
    Rainy Cool Normal True No
    Overcast Cool Normal True Yes
    Sunny Mild High False No
    Sunny Cool Normal False Yes
    Rainy Mild Normal False Yes
    Sunny Mild Normal True Yes
    Overcast Mild High True Yes
    Overcast Hot Normal False Yes
    Rainy Mild High True No
    View this question on its own page →
    Worked Solution

    ID3 on PlayTennis (14 examples: 9 Yes, 5 No, H(S)=0.940H(S)=0.940)

    Calculate Gain(S,A)=H(S)−∑v∣Sv∣∣S∣H(Sv)Gain(S,A)=H(S)-\sum_v \frac{|S_v|}{|S|}H(S_v)

    • Outlook: Sunny (5:2Y3N, H=0.971H=0.971), Overcast (4:4Y0N H=0H=0), Rainy (5:3Y2N H=0.971H=0.971) → Weighted =5/14∗0.971+4/14∗0+5/14∗0.971=0.694=5/14*0.971+4/14*0+5/14*0.971=0.694 → Gain=0.246

    • Temperature: Hot 4:2Y2N H=1H=1, Mild 6:4Y2N H=0.918H=0.918, Cool 4:3Y1N H=0.811H=0.811 → Weighted 0.9110.911 → Gain 0.0290.029

    • Humidity: High 7:3Y4N H=0.985H=0.985, Normal 7:6Y1N H=0.592H=0.592 → Weighted 0.7880.788 → Gain 0.1520.152

    • Windy: False 8:6Y2N H=0.811H=0.811, True 6:3Y3N H=1H=1 → Weighted 0.8920.892 → Gain 0.0480.048

    Root = Outlook (max gain).

    • Outlook=Overcast → Leaf Yes (4/4 pure)

    • Outlook=Sunny (5 ex: 2Y3N, H=0.971H=0.971): Evaluate remaining (Temp,Humidity,Windy):

      • Gain(Humidity)=0.971 (High 3:0Y3N H=0H=0, Normal 2:2Y0N H=0H=0 → weighted 0) → Best = Humidity
      • Humidity=High → Leaf No, Humidity=Normal → Leaf Yes
    • Outlook=Rainy (5 ex: 3Y2N): Evaluate:

      • Windy: False 3:3Y0N H=0H=0, True 2:0Y2N H=0H=0 → Weighted 0 → Gain =0.971=0.971 → Best=Windy
      • Humidity gain 0.020.02, Temp 0.020.02
      • Windy=False → Leaf Yes, Windy=True → Leaf No

    Final Tree:

    Outlook?
    ├─ Sunny → Humidity?
    │         ├─ High → No (3)
    │         └─ Normal → Yes (2)
    ├─ Overcast → Yes (4)
    └─ Rainy → Windy?
               ├─ False → Yes (3)
               └─ True → No (2)
    

    This tree achieves 100% training accuracy (14/14). Equivalent textbook result; order of Humidity/Windy nodes may swap if tie but this is standard ID3 outcome. Information Gains shown above at each split.

  10. Q9a. Write short note on: Logistic Regression and Sigmoid Function20257m

    Unit 3: Classification & Regression

    Write short note on: Logistic Regression and Sigmoid Function

    View this question on its own page →
    Worked Solution

    Logistic Regression and Sigmoid Function:

    Logistic Regression:
    Discriminative linear classifier for binary classification. Despite name, it's classification, not regression. Models posterior P(y=1∣x)=σ(wTx+b)P(y=1|x)=\sigma(w^T x + b) where σ\sigma is sigmoid.

    • Hypothesis: hw(x)=σ(wTx+b)∈(0,1)h_w(x)=\sigma(w^T x+b)\in(0,1)
    • Decision: predict 1 if hw≥0.5h_w\ge0.5 (wTx+b≥0w^T x+b\ge0) else 0 — linear boundary.
    • Loss: Binary cross-entropy (log loss) J(w)=−1m∑[ylog⁡h+(1−y)log⁡(1−h)]J(w)= -\frac1m\sum [y\log h + (1-y)\log(1-h)] — convex MLE under Bernoulli.
    • Gradient: $
      abla J = \frac1m X^T (h-y)$, solved via SGD, Newton.
    • Regularization: L2L2 λ∣∣w∣∣2\lambda||w||^2 prevents overfit.
    • Multi-class extends via softmax P(y=k∣x)=ewkTx/∑ewjTxP(y=k|x)=e^{w_k^T x}/\sum e^{w_j^T x} (Multinomial Logistic).
    • Interpretable: coefficients as log-odds ratio, exp⁡(wj)\exp(w_j) = odds multiplier for unit feature increase.
    • Use when need probabilities, linear separable, baseline for text, medical.

    Sigmoid Function:
    σ(z)=11+e−z\sigma(z)=\frac1{1+e^{-z}}, shape S-curve (−∞→0,0→0.5,+∞→1)( -\infty\to0, 0\to0.5, +\infty\to1), derivative σ′(z)=σ(z)(1−σ(z))\sigma'(z)=\sigma(z)(1-\sigma(z)).

    Properties: squashes real to (0,1), smooth, differentiable, but suffers vanishing gradient for large ∣z∣|z| (near 0 or 1) — replaced by ReLU in deep hidden layers but still core for logistic output, gating (LSTM).

    Example: z=0→0.5z=0→0.5, z=2→0.881z=2→0.881, z=−2→0.119z=-2→0.119.

    Together, sigmoid makes linear score interpretable as probability, optimized via cross-entropy, yielding calibrated classifier.