ML Study Hub

Home / Study together / Weeks 5-8 together

MLF + MLT + MLP: weeks 5-8 together

Weeks 5 to 8 (with a week-4 bridge): MLF tools, MLT algorithms and MLP code in the best order.

The big idea of weeks 5 to 8

Think of it like this

In weeks 1 to 4 you built the tools (vectors, derivatives, projection, eigenvectors) and the first unsupervised algorithms. Weeks 5 to 8 are the second chapter: now we have labels, and we learn to predict. The same tools come back wearing new names:

  • least squares is a projection (MLF 3) solved with a symmetric matrix (MLF 5);
  • ridge is that same solution made safe, and SVD (MLF 6) shows exactly how it shrinks;
  • gradient descent (MLF 7) is how a computer walks to the answer, and convexity (MLF 8) is why it always arrives;
  • k-NN, trees and Naive Bayes (MLT 7, 8) are the first classifiers, and Naive Bayes needs Bayes' rule from MLF's probability weeks.

MLP then makes you type all of it in scikit-learn.

Tip

Where this sits in the exams. Quiz 2 is taken to cover weeks 5 to 8 in each of the three courses (the official pages do not print the quiz boundary, so this split is inferred from the question topics). Do the lessons in the braided order below and you prepare all three Quiz 2 papers at once. Week 4 (eigenvalues, EM) is the bridge: skim MLF week 4 first if eigenvalues feel rusty.

Dependency map (the tool, and where it is used next)

MLF idea (the tool)Where weeks 5 to 8 of MLT and MLP use it
W3 projection, normal equations XTXw=XTyX^TX\mathbf w=X^T\mathbf yMLT W5 least squares is this; MLP W3 LinearRegression solves it
W5 symmetric matrices, real eigenvalues, PSDXTXX^TX is symmetric PSD, so least squares has a solid solution; MLT W6 adds λI\lambda I to push every eigenvalue up
W6 SVD and low-rankMLT W6 shows ridge shrinks each SVD direction by σ2/(σ2+λ)\sigma^2/(\sigma^2+\lambda); MLP W4 Ridge/Lasso
W7 gradient descentMLP W3 SGDRegressor (batch and stochastic); MLP W5 logistic regression trained by GD
W8 convexityleast squares and ridge are convex, so one minimum and GD cannot get stuck; LASSO is convex but has a corner (MLT W6)
W10 Bayes rule (borrowed early)MLT W8 Naive Bayes; MLP W6 probabilities and thresholds
MLT W7 k-NN and treesMLP W9 KNeighbors..., DecisionTree... (the code comes later)

The braided plan

Sixteen sessions. The MLP sessions are short: run the code and compare with the hand calculation from the session before.

#SessionReadWhy nowTime
1Symmetric matricesMLF week 5XTXX^TX and covariance matrices are symmetric; this explains why they behave80 min
2SVD and PCAMLF week 6one tool that explains PCA, ridge and low-rank90 min
3Least squares as regressionMLT week 5MLF 3's projection, now with data and noise, plus its Bayesian reading90 min
4Linear regression in codeMLP week 3 (first half)type what you just solved by hand45 min
5Gradient descentMLF week 7the computer's way to the same answer80 min
6GD and SGD in sklearnMLP week 3 (second half)eta0, learning_rate, batch vs stochastic45 min
7ConvexityMLF week 8tells you when GD is guaranteed to win80 min
8Ridge and LASSOMLT week 6uses sessions 1, 2, 5 and 7 together90 min
9Polynomial and regularised modelsMLP week 4PolynomialFeatures, Ridge, Lasso, direction of alpha50 min
10Bayes rule, borrowed earlyMLF week 10 (conditional probability and Bayes only)Naive Bayes needs exactly this40 min
11k-NN and decision treesMLT week 7distance (MLF 1) as a classifier; entropy as a splitting rule80 min
12Naive BayesMLT week 8Bayes rule plus an independence assumption80 min
13Logistic regression in codeMLP week 5LogisticRegression, C, probabilities45 min
14Binary classifier metricsMLP week 6confusion matrix, precision, recall, PR curve60 min
15MulticlassMLP week 7OvR needs KK classifiers, OvO needs K(K−1)/2K(K-1)/245 min
16SVMs in sklearnMLP week 8SVC, kernel, C, gamma (the theory is MLT week 10, later)50 min

Do each lesson's quick quiz and real exam questions straight after. Tick Mark this week as done so the home page tracks you.

Five braided problems (solve them, then check)

Your turn: 1. Least squares, then a safer version (MLF 3 and 5 meet MLT 5 and 6)

Data: x=1,2,3x=1,2,3 and y=2,4,7y=2,4,7, one feature, no intercept. (a) Find the least-squares weight ww. (b) Find the ridge weight with λ=1\lambda=1 using w=(XTX+λI)−1XTyw=(X^TX+\lambda I)^{-1}X^T\mathbf y. (c) Which is smaller, and why?

Need a hint?

Here XTXX^TX is a single number, ∑x2\sum x^2.

Show the full solution

XTX=1+4+9=14X^TX=1+4+9=14 and XTy=2+8+21=31X^T\mathbf y=2+8+21=31. (a) w=3114≈2.214w=\frac{31}{14}\approx\mathbf{2.214}. (b) w=3114+1=3115≈2.067w=\frac{31}{14+1}=\frac{31}{15}\approx\mathbf{2.067}. (c) The ridge weight is smaller: adding λ\lambda to the denominator shrinks the weight towards zero. That is the whole idea of ridge: a slightly worse fit on the training data, in return for a steadier weight.

Your turn: 2. Why ridge never fails to invert (MLF 5 meets MLT 6)

Two features are exact copies, so XTX=(3223)X^TX=\begin{pmatrix}3&2\\2&3\end{pmatrix} is replaced in a bad case by A=(2222)A=\begin{pmatrix}2&2\\2&2\end{pmatrix}. (a) Is AA invertible? Give its eigenvalues. (b) Find the eigenvalues and determinant of A+λIA+\lambda I for λ=1\lambda=1.

Need a hint?

For a symmetric matrix the eigenvalues add λ\lambda when you add λI\lambda I.

Show the full solution

(a) det⁡A=4−4=0\det A=4-4=0, so AA is not invertible. Trace =4=4 and determinant =0=0 give eigenvalues 4\mathbf{4} and 0\mathbf{0}. (b) A+I=(3223)A+I=\begin{pmatrix}3&2\\2&3\end{pmatrix} with determinant 9−4=5≠09-4=\mathbf5\neq0. Its eigenvalues are 4+1=54+1=5 and 0+1=10+1=1 (product 55 ✓). Adding λI\lambda I lifts every eigenvalue by λ\lambda, so none can be 00. That is why ridge works even when features are copies of each other.

Your turn: 3. How much does ridge shrink each direction? (MLF 6 meets MLT 6)

In the SVD of XX, ridge multiplies the least-squares answer along a direction with singular value σ\sigma by σ2σ2+λ\dfrac{\sigma^2}{\sigma^2+\lambda}. For λ=1\lambda=1, compute the factor for σ=3\sigma=3 and for σ=1\sigma=1. Which direction is shrunk more?

Need a hint?

Plug in. Compare with 1 (no shrinkage).

Show the full solution

σ=3\sigma=3: 99+1=0.9\frac{9}{9+1}=\mathbf{0.9}. σ=1\sigma=1: 11+1=0.5\frac{1}{1+1}=\mathbf{0.5}. The small-singular-value direction is shrunk far more. Small σ\sigma means the data barely varies in that direction, so the weight there is the least trustworthy: ridge quietly distrusts it. Least squares has factor 11 everywhere.

Your turn: 4. Gradient descent on the same problem (MLF 7 and 8 meet MLP 3)

For the data of Problem 1, the loss is L(w)=∑(wxi−yi)2=14w2−62w+69L(w)=\sum(wx_i-y_i)^2=14w^2-62w+69. (a) Find L′(w)L'(w) and L′′(w)L''(w) and say why GD is guaranteed to find the minimum. (b) Do one GD step from w0=0w_0=0 with η=0.01\eta=0.01. (c) What is the largest η\eta that still converges?

Need a hint?

L′′>0L''>0 everywhere means convex.

Show the full solution

(a) L′(w)=28w−62L'(w)=28w-62, L′′(w)=28>0L''(w)=28>0 everywhere, so LL is convex with one minimum, at L′=0L'=0: w=6228=3114w=\frac{62}{28}=\frac{31}{14}, the same answer as before. (b) w1=0−0.01×(28⋅0−62)=0.62w_1=0-0.01\times(28\cdot0-62)=\mathbf{0.62}. (c) For a quadratic with curvature c=28c=28, GD converges when η<2c=228≈0.0714\eta<\frac2c=\frac2{28}\approx\mathbf{0.0714}. In scikit-learn this is exactly why too large an eta0 makes SGDRegressor blow up.

Your turn: 5. Bayes rule in one line (MLF 10 meets MLT 8)

P(spam)=0.4P(\text{spam})=0.4. The word "free" appears in 50%50\% of spam and 10%10\% of non-spam. An email contains "free". Find P(spam∣free)P(\text{spam}\mid\text{free}).

Need a hint?

Bayes: posterior == likelihood ×\times prior, divided by the total.

Show the full solution

Numerator: 0.5×0.4=0.200.5\times0.4=0.20. Other branch: 0.1×0.6=0.060.1\times0.6=0.06. So P=0.200.20+0.06=0.200.26≈0.769P=\frac{0.20}{0.20+0.06}=\frac{0.20}{0.26}\approx\mathbf{0.769}. Naive Bayes does this once per word and multiplies the likelihoods, pretending the words are independent.

What the weeks become in each other (cheat-sheet)

IdeaIn MLFIn MLTIn MLP
Normal equationsW3 projectionW5 least squaresLinearRegression
Adding λI\lambda IW5 eigenvalue shiftW6 ridgeRidge(alpha=...)
Sparse solutionsW8 convex, non-smoothW6 LASSOLasso(alpha=...)
Walk downhillW7 gradient descentW5 to W6 iterative solversSGDRegressor(eta0, learning_rate)
ProbabilityW10 BayesW8 Naive BayesGaussianNB, predict_proba
Many classes-W9 to W10W7 OvR KK, OvO K(K−1)/2K(K-1)/2
The idea

Counting skills that pay every time in Quiz 2. Number of fits in a grid search == combinations ×\times folds (+1+1 refit). OvR trains KK models, OvO trains K(K−1)/2K(K-1)/2. Larger ridge/LASSO penalty ⇒\Rightarrow smaller weights: alpha up, C down is the stronger regularisation. Learn these as three one-line rules.

Short on time?

The idea

4-day crash version (Quiz 2 only). Day 1: MLF 5 and 6 (focus on PSD and SVD facts). Day 2: MLT 5 and 6 plus MLP 3 and 4 (the ridge story). Day 3: MLF 7 and 8 plus MLP 5 and 6 (GD, convexity, metrics). Day 4: MLT 7 and 8 plus MLP 7 and 8 (classifiers). Finish every day with that week's real exam questions.

After the sessions, sit one full Quiz 2 paper per course from Past papers under a timer. Then continue to weeks 1 to 8 together or weeks 1 to 12 together.

Course structure from the official IIT Madras BS course pages (BSCS2004 MLF, BSCS2007 MLT, BSCS2008 MLP). Past papers and answer keys come from quizpractice.space. This is an independent study aid - always check the current term's grading rules on the official course page.