What does it mean for a computer to “learn”? This part starts from zero: the words every machine-learning question uses (feature, label, training set, loss), how to judge a classifier with a confusion matrix, and then the first real method, linear regression, which fits a straight line by making the squared errors as small as possible. Both chapters carry a past GATE DA question, and every later chapter of this module builds on them.
You need: nothing from this module. Mean and covariance from statistics, derivatives from calculus and solving a small linear system from linear algebra help in Chapter 1.2; each is recalled where it is used.
What machine learning is: learning a rule from examples
Machine learning is a way of building a prediction rule from examples: instead of writing the rule by hand, you show the computer many past cases with their answers and let it find a rule that fits them.
Every machine-learning question in GATE DA (about one mark in six of the subject paper) uses this chapter’s vocabulary, and 2026 Q47 asked directly for accuracy, precision and recall from a classifier’s mistakes.
It’s like how a new team analyst learns to read a pitch: nobody hands them a rulebook; they watch hundreds of matches, note what the pitch looked like and what happened, and slowly form their own rule.
The words (feature, label, supervised, training and test set) appear in every machine-learning question; the evaluation measures are asked directly.
A problem with no obvious rule
The Hyderabad Hyperplanes’ physio wants to know, before each match, which players are likely to pick up an injury. Nobody can write down an exact rule for that. Too many things matter: how many overs a bowler sent down last week, how much they slept on the flight, their age, the heat.
What the physio does have is a record of past seasons: for every player before every match, those numbers, and whether the player was actually injured. Machine learning turns that record into a rule.
The difference from ordinary programming is the direction of travel. In ordinary programming a person writes the rule and the computer applies it to data. In machine learning the computer is given data together with the answers, and produces the rule.
The words, one at a time
Here is a slice of the physio’s record. Each row is one player before one match.
| Overs bowled last week | Hours of sleep | Age | Injured? |
|---|---|---|---|
| 22 | 6.0 | 31 | yes |
| 8 | 8.0 | 24 | no |
| 15 | 7.5 | 27 | no |
| 25 | 5.5 | 34 | yes |
- Vocabulary
- example
- One row: one case the model can learn from (here, one player before one match). Also called a data point or an instance. The number of examples is written $n$.
- feature
- One measured input column, such as “hours of sleep”. The features of one example form a vector $\x = (x_1, \ldots, x_d)$ with $d$ entries; for the first row, $\x = (22, 6.0, 31)$ and $d = 3$.
- label
- The answer we want to predict, written $y$. Here $y$ is “injured?”, and for the first row $y = $ yes.
- dataset
- The collection of examples with their labels: $(\x_1, y_1), \ldots, (\x_n, y_n)$.
- model
- The rule being learned: a function $f$ that takes a feature vector and returns a prediction $\hat y = f(\x)$ (read “y-hat”).
- parameter
- A number inside the model that learning adjusts. In the rule "flag a player if $0.1 \times \text{overs} - 0.5 \times \text{sleep} \gt 0$", the 0.1 and the 0.5 are parameters.
- training
- Choosing the parameters so that the model’s predictions match the known labels as well as possible.
Supervised learning: every training example comes with its label, and the model learns to predict the label from the features. Two kinds, by what the label is:
- Regression: the label is a number (a team’s final total, a player’s auction price).
- Classification: the label is one of a few categories, called classes (injured or not; which of the five teams wins).
Unsupervised learning: there are no labels at all, only features. The goal is to find structure, for example groups of similar fans (clustering, Part 6) or the few directions along which the data varies most (PCA, Part 6).
Training data and test data
Suppose a model simply memorised the record: “player 7 before match 12: injured”. It would be perfect on the record and useless next season, because next season’s cases are new. What we care about is performance on examples the model has not seen. That ability is called generalisation.
So before training, the data is split in two. The training set is used to choose the parameters. The test set is locked away and used only once, at the end, to estimate how the model will do on new cases. A student who has seen the exact exam paper in advance can score full marks without understanding anything; the test set is the paper they have not seen.
Part 2 builds on this: why a model can fit its training data too well (overfitting) and how cross-validation chooses settings without touching the test set.
Loss: a number for “how wrong”
To train, the computer needs a single number that says how bad a prediction is. That number is the loss. Training means choosing the parameters that make the average loss over the training set as small as possible.
A loss function $\ell(y, \hat y)$ measures the cost of predicting $\hat y$ when the truth is $y$. Two common ones:
- Squared loss (regression): $\ell(y, \hat y) = (y - \hat y)^2$. Predicting a total of 170 when the team made 180 costs $10^2 = 100$.
- 0–1 loss (classification): $\ell = 0$ if $\hat y = y$ and $1$ otherwise. It simply counts mistakes.
The training error is the average loss over the training set, $\frac1n\sum_{i=1}^n \ell(y_i, f(\x_i))$. The same average over the test set is the test error.
Judging a classifier: the confusion matrix
Say the physio’s model flags players as “at risk” (the positive class) or “fine” (the negative class). Each prediction lands in one of four boxes, and counting them gives the confusion matrix.
| Predicted positive | Predicted negative | |
|---|---|---|
| Actually positive | TP true positive: injured, and flagged | FN false negative: injured, but missed |
| Actually negative | FP false positive: fine, but flagged | TN true negative: fine, not flagged |
$$\text{accuracy} = \frac{TP + TN}{TP + FP + FN + TN},\qquad \text{precision} = \frac{TP}{TP + FP},\qquad \text{recall} = \frac{TP}{TP + FN}.$$
- Accuracy: what fraction of all predictions were right.
- Precision: of the players we flagged, what fraction were really injured? (Read along the “predicted positive” column.)
- Recall: of the players who were really injured, what fraction did we flag? (Read along the “actually positive” row.) Also called sensitivity or true positive rate.
With more than one class of interest, each class gets its own precision and recall: treat that class as “positive” and every other class as “negative”.
The model gives each of 20 Hyderabad Hyperplanes players an injury-risk score, and flags everyone whose score is at or above the threshold. Move the threshold and watch the four counts change. Notice that lowering it raises recall (fewer injured players missed) but lets in more false alarms. Then switch to “rare injuries” and find a threshold with high accuracy but poor recall.
Consider that 20 stories of Author X and 10 stories of Author Y were kept together without mentioning the names of the authors. A classifier was then asked to predict the author (X or Y) of each of these stories. Let, out of X’s stories, 6 were classified as that of Y. On the other hand, out of Y’s stories, 2 were classified as that of X.
Considering X and Y as two classes, which of the following statements is/are true?
- Accuracy of the classifier is 11/15.
- Precision of Class X is higher than the Precision of Class Y.
- Recall of Class X is higher than the Recall of Class Y.
- Accuracy of the classifier is 14/15.
Fill the confusion matrix. X’s 20 stories: 6 called Y, so 14 called X. Y’s 10 stories: 2 called X, so 8 called Y.
Every question about these measures starts with the four counts. Rows are the truth, columns the prediction.
Accuracy $= \dfrac{14 + 8}{30} = \dfrac{22}{30} = \dfrac{11}{15}$. So (A) is true and (D) is false.
The correct predictions are the diagonal: X called X, and Y called Y.
Precision of X: the classifier said “X” $14 + 2 = 16$ times and was right 14 times, so $\tfrac{14}{16} = 0.875$. Precision of Y: it said “Y” $8 + 6 = 14$ times and was right 8 times, so $\tfrac{8}{14} \approx 0.571$. (B) is true.
Precision divides by how often a class was predicted, so read down the column.
Recall of X $= \tfrac{14}{20} = 0.7$; recall of Y $= \tfrac{8}{10} = 0.8$. X’s recall is lower, so (C) is false. Answer: (A) and (B).
Recall divides by how many truly belong to the class, so read along the row. Official key: A; B.
The Hyderabad Hyperplanes’ injury model is checked on 100 player-matches: 12 true positives, 8 false positives, 4 false negatives and 76 true negatives. Find its precision and its recall.
Precision divides TP by everything that was flagged; recall divides TP by everything that was really injured.
Precision $= \frac{12}{12 + 8} = 0.6$. Recall $= \frac{12}{12 + 4} = 0.75$. (Accuracy would be $\frac{12 + 76}{100} = 0.88$, which sounds better than either.)
Which of these is an unsupervised learning task?
- Predicting a team’s final total from its score after 10 overs, using last season’s matches.
- Splitting ticket buyers into groups with similar buying habits, with no group names given in advance.
- Deciding whether a delivery was a no-ball, from clips that umpires have already marked.
- Predicting a player’s auction price from past auctions.
Ask of each task: does the training data come with the answer we want to predict?
(B) has no labels: nobody says which group a buyer belongs to, so the method must discover the groups (clustering). (A) and (D) are supervised regression (the label is a number); (C) is supervised classification (the umpires supplied the labels).
Of 1000 DPL matches, 20 were rain-delayed. A lazy model predicts “no delay” for every match. Taking “delayed” as the positive class, find its accuracy and its recall.
It gets every non-delayed match right and every delayed match wrong. How many delays does it catch?
Correct predictions: the 980 non-delayed matches, so accuracy $= 980/1000 = 0.98$. It flags nothing, so TP = 0 and recall $= 0/20 = 0$. High accuracy, no use at all.
Which statements about training and test sets are true? (Select all that apply.)
- The test set should not be used to choose the model’s parameters.
- A training error of zero guarantees a small test error.
- The test error estimates how the model will do on new, unseen examples.
- The training and test examples should come from the same kind of data (for example, the same league and era).
Think of the student who memorised last year’s paper.
(A) If the test set helps choose parameters, it is no longer unseen and its error becomes optimistic. (C) That is its whole purpose. (D) A test on a different kind of data says little about the cases you care about. (B) is false: a model can memorise its training set (zero training error) and still do badly on new examples; that is overfitting, Part 2.
A model predicts upsets (the weaker team winning) in 200 DPL matches; 50 of them were actual upsets. On these matches its recall for “upset” is 0.8 and its precision is 0.5. What is its accuracy?
Recall gives TP. Precision then tells you how many matches were flagged in total, hence FP. The rest of the 150 non-upsets are TN.
Recall $= TP/50 = 0.8$, so $TP = 40$ and $FN = 10$.
Precision $= TP/(TP + FP) = 0.5$, so $40$ is half of everything flagged: 80 flagged, $FP = 40$.
Non-upsets: $150$, of which 40 were wrongly flagged, so $TN = 110$.
Accuracy $= \frac{40 + 110}{200} =$ 0.75.
A tempting slip is to average precision and recall (0.65), or to take $TP/200 = 0.2$; neither is accuracy, which counts every correct prediction, including true negatives.
A classifier gives each example a score and predicts “positive” when the score is at least a threshold $t$. Which statements are always true? (Select all that apply.)
- Lowering $t$ can never decrease recall.
- Lowering $t$ can never increase precision.
- Setting $t$ below every score gives recall 1.
- If 1% of examples are positive, any classifier with 99% accuracy has recall at least 0.5.
Recall’s denominator (actual positives) never changes with $t$. For precision, try three examples whose scores, from highest to lowest, are negative, positive, positive.
(A) True. Lowering $t$ only adds flagged examples, so TP can only stay the same or grow, while TP + FN (the number of actual positives) is fixed.
(C) True. Every example is flagged, so FN = 0 and recall $= TP/TP = 1$.
(B) False. With scores ordered negative, positive, positive (highest first), a high $t$ flags only the negative: precision 0. Lowering $t$ to include the next one gives precision $\tfrac12$. Precision can go either way.
(D) False. Predicting “negative” for everything is 99% accurate with recall 0.
A batter’s scores in four innings are 2, 3, 4 and 11. You must predict every innings with one constant $c$. Which pair gives, in order, the $c$ that minimises the total squared loss $\sum (y_i - c)^2$ and a $c$ that minimises the total absolute loss $\sum |y_i - c|$?
- 5 and 5
- 5 and 3.5
- 3.5 and 5
- 4 and 4
Differentiate $\sum (y_i - c)^2$ and set it to zero. For the absolute loss, ask what happens to the total when $c$ moves a little to the right: how many points get nearer, and how many get farther?
Squared loss: $\frac{d}{dc}\sum (y_i - c)^2 = -2\sum (y_i - c) = 0$ gives $c = \bar y = \frac{20}{4} = 5$, the mean. Total loss $9 + 4 + 1 + 36 = 50$.
Absolute loss: moving $c$ right by a small step lowers the loss by that step for each point above $c$ and raises it for each point below. The total stops falling once half the points are on each side, so any $c$ between the two middle values 3 and 4 is best (total 10). That is the median; 3.5 is one such value. Answer (B).
(A) assumes both losses share a minimiser; the outlier 11 drags the mean but not the median. (C) swaps them. (D) has a valid absolute-loss minimiser (4), but under squared loss 4 costs $4 + 1 + 0 + 49 = 54 \gt 50$.
- Name the positive class before computing precision or recall.
- Write the four counts (TP, FP, FN, TN) first; every measure is a ratio of them.
- Keep the test set untouched until the very end.
- Swapping precision and recall: precision divides by what was predicted positive, recall by what is positive.
- Trusting accuracy when one class is rare.
- Calling a task “unsupervised” just because the labels are numbers. Numeric labels mean regression, which is still supervised.
- Supervised learning learns a model ŷ = f(x) from examples with labels (regression for numbers, classification for categories); unsupervised learning has no labels.
- Train on the training set by minimising average loss; judge on an unseen test set.
- Accuracy = (TP + TN)/all, precision = TP/(TP + FP), recall = TP/(TP + FN).
A model predicts rain delays and is right on 95% of matches. Only 5% of matches are actually delayed. Is it a good model?
A dataset of past matches has columns “toss won”, “home ground”, “score at 10 overs” and “won the match”. A model predicts the result before the match ends. Which column is the label?
Recall answers which question?
Linear regression: the best straight line
Linear regression predicts a number as a weighted sum of the features, and chooses the weights that make the sum of squared prediction errors on the training data as small as possible.
GATE DA 2025 Q34 asked for the least-squares slope of a line through the origin, and the closed form $\mathbf{w} = (X\T X)^{-1}X\T\y$ met here is the starting point of ridge regression (2026 Q37 and Q55, Part 2).
It’s like a stiff rod held by springs, one hanging from each data point: the rod settles where the springs’ total pull balances, and with springs whose energy grows with the square of their stretch, that resting place is the least-squares line.
A direct computation (the slope of a fitted line), and the base that ridge regression changes.
The question
The Bengaluru Backprops’ analyst wants to predict how many runs a batter will score from how many balls they face. Four innings from last season:
| Balls faced $x$ | 10 | 20 | 30 | 40 |
|---|---|---|---|---|
| Runs $y$ | 18 | 24 | 42 | 52 |
Plot them and they lie roughly along a line. No single line passes through all four, so we need a way to say which line is best.
A line is $\hat y = wx + b$: $w$ is the slope (extra runs per extra ball faced) and $b$ is the intercept (the prediction at $x = 0$). In machine-learning language, $w$ is a weight and $b$ a bias; both are the model’s parameters.
For each innings, the gap between what happened and what the line predicts, $y_i - \hat y_i$, is the residual. Some residuals are positive, some negative. Squaring them makes every gap count as a cost and punishes big misses heavily. The least-squares line is the one with the smallest total of squared residuals.
Given data $(x_1, y_1), \ldots, (x_n, y_n)$, choose $w$ and $b$ to minimise the sum of squared errors $$\text{SSE}(w, b) = \sum_{i=1}^n \big(y_i - (wx_i + b)\big)^2.$$ This is the squared loss of Chapter 1.1, added up over the training set. Dividing by $n$ gives the mean squared error (MSE); the same $w, b$ minimise both.
Move the slope and intercept sliders to make the total of squared residuals (the vertical gaps, drawn dashed) as small as you can, then press “Best fit” to see how close you came. Drag a point (or select the plot and use the arrow keys; keys 1–4 pick a point) and watch the best line follow it. Switch to “through the origin” to see what forcing $b = 0$ costs.
Finding the best line with calculus
SSE is a smooth bowl-shaped function of $w$ and $b$, so its lowest point is where both partial derivatives are zero (recall from Calculus 2.2 that an interior minimum has zero gradient).
Setting $\partial\,\text{SSE}/\partial b = -2\sum (y_i - wx_i - b) = 0$ gives $\sum y_i = w\sum x_i + nb$, that is, $\bar y = w\bar x + b$. So the best line always passes through the point of means $(\bar x, \bar y)$.
Setting $\partial\,\text{SSE}/\partial w = -2\sum x_i (y_i - wx_i - b) = 0$ and substituting $b = \bar y - w\bar x$ gives the slope below.
$$w = \frac{\sum_i (x_i - \bar x)(y_i - \bar y)}{\sum_i (x_i - \bar x)^2} = \frac{\operatorname{Cov}(x, y)}{\operatorname{Var}(x)},\qquad b = \bar y - w\,\bar x.$$ (The sample covariance and variance from Statistics; their $\tfrac1{n-1}$ factors cancel.)
Through the origin. If the model is forced to be $\hat y = wx$ (no intercept), only $\frac{d}{dw}\sum (y_i - wx_i)^2 = -2\sum x_i(y_i - wx_i) = 0$ remains, so $$w = \frac{\sum_i x_i y_i}{\sum_i x_i^2}.$$
Fit $\hat y = wx + b$ to the four Backprops innings above, then fit $\hat y = wx$ through the origin, and compare the two SSEs.
Means: $\bar x = 25$, $\bar y = \frac{18 + 24 + 42 + 52}{4} = 34$. Deviations: $x_i - \bar x = -15, -5, 5, 15$ and $y_i - \bar y = -16, -10, 8, 18$.
Centring first keeps the numbers small and is exactly what the slope formula asks for.
$\sum (x_i - \bar x)(y_i - \bar y) = 240 + 50 + 40 + 270 = 600$ and $\sum (x_i - \bar x)^2 = 225 + 25 + 25 + 225 = 500$. So $w = \frac{600}{500} = 1.2$.
Each extra ball faced is worth 1.2 runs on this line: a strike rate of 120.
$b = \bar y - w\bar x = 34 - 1.2 \times 25 = 4$. The line is $\hat y = 1.2x + 4$, with predictions 16, 28, 40, 52 and residuals $2, -4, 2, 0$. SSE $= 4 + 16 + 4 + 0 = 24$.
The residuals add to zero, as they must when the fit has an intercept (that was the $\partial/\partial b = 0$ equation).
Through the origin: $\sum x_i y_i = 180 + 480 + 1260 + 2080 = 4000$ and $\sum x_i^2 = 100 + 400 + 900 + 1600 = 3000$, so $w = \frac{4000}{3000} \approx 1.333$. Its SSE is about 34.67, larger than 24.
Removing $b$ removes a parameter, so the best fit can only get worse (or stay equal). The slope steepens to make up for the missing intercept.
Given data $\{(-1, 1), (2, -5), (3, 5)\}$ of the form $(x, y)$, we fit a model $y = wx$ using linear least-squares regression. The optimal value of $w$ is ______ (Round off to three decimal places)
The model has no intercept, so use $w = \dfrac{\sum x_i y_i}{\sum x_i^2}$.
"$y = wx$" is the signal: a line forced through the origin. The centred formula would be wrong here, because it fits an intercept too.
$\sum x_i y_i = (-1)(1) + (2)(-5) + (3)(5) = -1 - 10 + 15 = 4$. $\sum x_i^2 = 1 + 4 + 9 = 14$.
Watch the signs: two of the three products are negative.
$w = \frac{4}{14} = \frac27 \approx$ 0.286.
Official key: 0.285 to 0.287.
More than one feature
Runs probably depend on more than balls faced: the batting position, the pitch, the opposition. With $d$ features the model becomes $$\hat y = w_1x_1 + w_2x_2 + \cdots + w_dx_d + b = \mathbf{w}\T\x + b.$$ This is multiple linear regression. With two features it fits a flat plane to points in 3-D; with more, a flat “hyperplane” (the higher-dimensional version of a plane).
To write all $n$ predictions at once, stack the examples as rows of a matrix. Put a column of ones first so that the intercept becomes just another weight: $$X = \begin{pmatrix} 1 & x_{11} & \cdots & x_{1d}\\ \vdots & \vdots & & \vdots\\ 1 & x_{n1} & \cdots & x_{nd}\end{pmatrix},\qquad \mathbf{w} = \begin{pmatrix} b\\ w_1\\ \vdots\\ w_d\end{pmatrix},\qquad \hat\y = X\mathbf{w}.$$ $X$ is the design matrix: $n$ rows (examples) and $d + 1$ columns. The SSE is $\norm{\y - X\mathbf{w}}^2$.
The weights that minimise $\norm{\y - X\mathbf{w}}^2$ satisfy $$X\T X\,\mathbf{w} = X\T\y.$$ If the columns of $X$ are linearly independent, $X\T X$ is invertible and $$\mathbf{w} = (X\T X)^{-1}X\T\y.$$ For a model with no intercept, simply leave out the column of ones.
Why. The gradient of $\norm{\y - X\mathbf{w}}^2$ with respect to $\mathbf{w}$ is $-2X\T(\y - X\mathbf{w})$; setting it to zero gives the normal equations. Read geometrically: the residual vector $\y - X\mathbf{w}$ is perpendicular to every column of $X$. The fitted values $X\mathbf{w}$ are the projection of $\y$ onto the column space of $X$ (recall projections from Linear algebra 3.2). The column of ones being perpendicular to the residuals is the statement “residuals add to zero”.
With one feature and an intercept, the normal equations reduce to the formulas above. For the Backprops data: $X\T X = \begin{pmatrix}4 & 100\\ 100 & 3000\end{pmatrix}$ and $X\T\y = \begin{pmatrix}136\\ 4000\end{pmatrix}$. Solving gives $b = 4$, $w = 1.2$, the same line.
When the columns are dependent (a feature repeated, a feature that is a fixed multiple of another, or fewer examples than weights), $X\T X$ is singular and infinitely many weight vectors fit equally well. Ridge regression (Part 2) fixes this by adding $\lambda I$.
"Fit $y = wx$" (no intercept): $w = \sum xy / \sum x^2$. "Fit $y = wx + b$": $w = \sum (x - \bar x)(y - \bar y) / \sum (x - \bar x)^2$ and $b = \bar y - w\bar x$. With several features, write $X\T X$ and $X\T\y$ and solve the small system; for a $2\times2$ system, $\begin{pmatrix}a & b\\ c & d\end{pmatrix}^{-1} = \frac{1}{ad - bc}\begin{pmatrix}d & -b\\ -c & a\end{pmatrix}$.
Fit the least-squares line $\hat y = wx + b$ to the points $(1, 3)$, $(2, 5)$, $(3, 4)$, $(4, 8)$.
$\bar x = 2.5$ and $\bar y = 5$. Multiply the deviations pairwise and add.
Deviations: $x - \bar x = -1.5, -0.5, 0.5, 1.5$ and $y - \bar y = -2, 0, -1, 3$.
$\sum (x - \bar x)(y - \bar y) = 3 + 0 - 0.5 + 4.5 = 7$ and $\sum (x - \bar x)^2 = 2.25 + 0.25 + 0.25 + 2.25 = 5$, so $w = 1.4$.
$b = 5 - 1.4 \times 2.5 = 1.5$.
The Backprops’ fitted line is $\hat y = 1.2x + 4$ (runs from balls faced). Predict the runs for a batter who faces 35 balls, and find the residual for an innings of 42 runs off 30 balls.
A residual is actual minus predicted.
Prediction: $1.2 \times 35 + 4 = 46$. For 30 balls the line predicts $1.2 \times 30 + 4 = 40$, so the residual is $42 - 40 = 2$.
A multiple linear regression with an intercept uses 3 features and is trained on 50 matches. What is the size of $X\T X$ in the normal equations?
- $3\times3$
- $4\times4$
- $50\times50$
- $50\times4$
First find the size of $X$ itself, remembering the column of ones.
$X$ is $50\times4$ (one row per match; a column of ones plus 3 feature columns). Then $X\T X$ is $(4\times50)(50\times4) = 4\times4$: one row and column per weight. (A) forgets the intercept; (C) is $XX\T$; (D) is $X$ itself.
A least-squares line $\hat y = wx + b$ (with intercept) is fitted to $n \ge 3$ points whose $x$ values are not all equal. Which statements are always true? (Select all that apply.)
- The residuals add up to zero.
- The line passes through $(\bar x, \bar y)$.
- At least one residual is zero.
- Fitting $x$ from $y$ instead gives slope exactly $1/w$.
(A) and (B) both come from setting $\partial\,\text{SSE}/\partial b = 0$. For (C) and (D), test the four Backprops points or a small example.
(A) and (B) are the equation $\sum (y_i - wx_i - b) = 0$, written two ways.
(C) is false: for $(1, 3), (2, 5), (3, 4), (4, 8)$ the line $1.4x + 1.5$ predicts 2.9, 4.3, 5.7, 7.1, and no residual is zero. (The Backprops fit had one zero residual by chance.)
(D) is false: the two slopes are $\frac{\text{Cov}}{\text{Var}(x)}$ and $\frac{\text{Cov}}{\text{Var}(y)}$, whose product is $r^2$ (the squared correlation), which equals 1 only if the points lie exactly on a line.
A model with two features and no intercept, $\hat y = w_1x_1 + w_2x_2$, is fitted by least squares to four examples: $(x_1, x_2; y) = (1, 0; 1)$, $(0, 1; 2)$, $(1, 1; 3)$, $(1, 2; 6)$. Find $w_2$ and the SSE of the fitted model. (Round off to two decimal places.)
Build $X$ ($4\times2$, no column of ones), then $X\T X$ and $X\T\y$, and solve the $2\times2$ system.
$X\T X = \begin{pmatrix}1+0+1+1 & 0+0+1+2\\ 3 & 0+1+1+4\end{pmatrix} = \begin{pmatrix}3 & 3\\ 3 & 6\end{pmatrix}$ and $X\T\y = \begin{pmatrix}1 + 3 + 6\\ 2 + 3 + 12\end{pmatrix} = \begin{pmatrix}10\\ 17\end{pmatrix}$.
Determinant $18 - 9 = 9$. So $w_1 = \frac{6\cdot10 - 3\cdot17}{9} = 1$ and $w_2 = \frac{3\cdot17 - 3\cdot10}{9} = \frac{21}{9} = \frac73 \approx$ 2.33.
Predictions: $1$, $\tfrac73$, $\tfrac{10}{3}$, $\tfrac{17}{3}$. Residuals: $0$, $-\tfrac13$, $-\tfrac13$, $\tfrac13$. SSE $= 3\times\tfrac19 = \tfrac13 \approx$ 0.33.
Check: the residual vector $(0, -\tfrac13, -\tfrac13, \tfrac13)$ is perpendicular to both columns of $X$, $(1, 0, 1, 1)$ and $(0, 1, 1, 2)$, as the normal equations promise. The residuals do not add to zero, because there is no intercept column.
The Backprops’ line $\hat y = 1.2x + 4$ was fitted with $x$ in balls faced. The analyst now measures $x$ in overs ($x' = x/6$) and, because of a scoring-rule change, adds 10 to every $y$. Refitting by least squares on the converted data gives $\hat y = w'x' + b'$ with
- $w' = 0.2$, $b' = 14$
- $w' = 7.2$, $b' = 14$
- $w' = 7.2$, $b' = 4$
- $w' = 1.2$, $b' = 14$
Rewrite the old fitted line in terms of $x'$. Is the result still the best line for the new data? Look at the slope formula: what do dividing every $x$ by 6 and adding 10 to every $y$ do to the deviations?
Dividing $x$ by 6 divides every $x - \bar x$ by 6, so $\text{Cov}$ is divided by 6 and $\text{Var}(x)$ by 36: the slope is multiplied by 6, $w' = 7.2$ (runs per over, not per ball).
Adding 10 to every $y$ leaves the deviations $y - \bar y$ unchanged (so the slope is unaffected) and raises $\bar y$ by 10. With $\bar x' = 25/6$: $b' = (34 + 10) - 7.2 \times \frac{25}{6} = 44 - 30 = 14$. Answer (B).
Equivalently, $1.2x + 4 + 10 = 1.2(6x') + 14 = 7.2x' + 14$, and since the converted data are just a relabelling, the best line is relabelled the same way.
(A) divides the slope by 6 instead of multiplying; (C) forgets the shift in $y$; (D) ignores the change of units.
For a linear regression with an intercept (design matrix $X$ with a column of ones), in which situations is $X\T X$ certainly singular, so that $(X\T X)^{-1}X\T\y$ cannot be used? (Select all that apply.)
- Two feature columns are identical (the same statistic entered twice).
- There are 5 features and only 4 training examples.
- One feature takes the same value in every example.
- The features are measured in very different units (balls and seconds).
$X\T X$ is invertible exactly when the columns of $X$ are linearly independent. For each option, find a column that is a combination of others, or show why none need be.
$X\T X$ is invertible exactly when $X$ has independent columns (rank equal to the number of columns; see Linear algebra 2.2).
(A) Two equal columns are dependent. (B) $X$ is $4\times6$, so its rank is at most 4, less than 6 columns. (C) A constant column is a multiple of the column of ones. All three make $X\T X$ singular.
(D) Units rescale columns but do not make them dependent, so $X\T X$ can still be invertible (rescaling can make it badly conditioned numerically, which is a different issue).
- Check whether the model has an intercept before choosing a formula.
- Centre the data ($x - \bar x$, $y - \bar y$) when computing a slope by hand.
- Sanity-check: with an intercept, residuals add to zero and the line passes through the means.
- Using $\sum xy/\sum x^2$ when the model has an intercept, or the centred formula for "$y = wx$".
- Measuring residuals perpendicular to the line; least squares uses vertical gaps.
- Forgetting the column of ones in $X$, which drops the intercept from the normal equations.
- Least squares picks the weights that minimise the sum of squared residuals Σ(yᵢ − ŷᵢ)².
- One feature: w = Σ(x − x̄)(y − ȳ)/Σ(x − x̄)², b = ȳ − w x̄; through the origin, w = Σxy/Σx².
- Many features: solve the normal equations XᵀX w = Xᵀy, so w = (XᵀX)⁻¹Xᵀy when the columns of X are independent.
Three points: $(0, 0)$, $(1, 1)$ and $(2, 5)$. Without computing, how steep is the least-squares line $\hat y = wx + b$?
For the model $y = wx$ (no intercept), least squares gives $w = $
The formula $\mathbf{w} = (X\T X)^{-1}X\T\y$ can be used exactly when
A classifier flags 40 players as at risk; 30 of them really were injured. Overall, 50 players were injured. Its precision and recall are
Fit $y = wx$ by least squares to $(1, 2)$ and $(2, 3)$. Then $w = $
Predicting tomorrow’s attendance at a DPL ground from past attendance records is
A least-squares line with intercept is fitted to data with $\bar x = 10$, $\bar y = 50$ and slope $w = 3$. The intercept is
Least squares makes the training SSE as small as possible. To report how well the line will predict next season, you should give