Linear Regression Introduction
- 24Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is supervised learning?
24 questions
- 1
What is supervised learning?
Model answer
Supervised learning is a machine learning paradigm where the algorithm is given a dataset of input-output pairs (x,y) and learns a mapping from inputs to outputs.
Explanation
In supervised learning, the training data includes both the input features x and the target labels y, and the goal is to learn a function h that predicts y from x.
- 2
What is the difference between a regression problem and a classification problem?
Model answer
In regression, the output y is a continuous value, while in classification, the output y is discrete (e.g., a class label).
Explanation
For example, predicting house prices is regression because price is continuous, while predicting whether an email is spam or not is classification because the output is a discrete category.
- 3
Write the hypothesis for linear regression with n features, using the notation where x0=1.
Model answer
The hypothesis is hθ(x)=θ0x0+θ1x1+⋯+θnxn=∑j=0nθjxj, where x0=1.
Explanation
This compact notation treats the bias term θ0 as the coefficient for a dummy feature x0=1, allowing the hypothesis to be written as a linear combination of parameters and features.
- 4
Why is the hypothesis in linear regression technically an affine function rather than a linear function?
Model answer
It is affine because it includes a constant term θ0 (the intercept), so it does not pass through the origin unless θ0=0.
Explanation
A linear function must satisfy f(ax+by)=af(x)+bf(y), but the presence of θ0 violates this unless θ0=0. However, in machine learning it is still commonly called linear regression.
- 5
Define the notation m, n, x(i), and y(i) as used in linear regression.
Model answer
m is the number of training examples, n is the number of features, x(i) is the input feature vector of the i-th training example, and y(i) is the target output for that example.
Explanation
The superscript (i) indexes the training examples, e.g., x(1) is the first training example. The input vector x(i) has n components, plus the dummy x0=1.
- 6
Write the cost function J(θ) for linear regression (ordinary least squares).
Model answer
J(θ)=2m1∑i=1m(hθ(x(i))−y(i))2
Explanation
The cost function measures the average squared difference between predicted values hθ(x(i)) and actual values y(i). The factor 1/2 is included for mathematical convenience when taking derivatives.
- 7
Why is the factor 21 included in the linear regression cost function?
Model answer
It simplifies the derivative calculation because the 2 from the derivative of the square cancels with the 1/2, leaving no constant factor in the gradient.
Explanation
When taking the partial derivative of J(θ) with respect to θj, the 1/2 cancels the 2 that comes from differentiating (hθ−y)2, making the gradient expression cleaner.
- 8
Why is squared error used in the cost function rather than absolute error or a higher power?
Model answer
The choice corresponds to a Gaussian noise model in the data, which will be justified when studying generalized linear models (GLMs).
Explanation
Squared error leads to the ordinary least squares solution, which is the maximum likelihood estimate under the assumption that the target variable y is Gaussian distributed with mean hθ(x). This will be explained in the context of GLMs.
- 9
Write the update rule for gradient descent for a single parameter θj in linear regression.
Model answer
θj:=θj−α∂θj∂J(θ), where α is the learning rate.
Explanation
The update subtracts the gradient times the learning rate from the current parameter value. The gradient points in the direction of steepest ascent, so subtraction moves downhill toward a minimum.
- 10
Derive the partial derivative ∂θj∂J(θ) for linear regression assuming a single training example (x,y).
Model answer
∂θj∂J(θ)=(hθ(x)−y)xj
Explanation
Starting from J(θ)=21(hθ(x)−y)2, differentiate using the chain rule: ∂θj∂J=(hθ−y)⋅∂θj∂hθ. Since hθ=∑kθkxk, ∂θj∂hθ=xj.
- 11
What is the effect of a very large learning rate α in gradient descent?
Model answer
A very large learning rate can cause the algorithm to overshoot the minimum, potentially diverging and causing the cost function to increase.
Explanation
If α is too big, the parameter updates are too large and may jump over the minimum, leading to oscillations or divergence. The cost function may not decrease monotonically.
- 12
How is the learning rate α typically chosen in practice?
Model answer
It is chosen empirically by trying a few values on an exponential scale (e.g., 0.01, 0.02, 0.04, 0.08) and selecting one that minimizes the cost function efficiently.
Explanation
Common practice is to start with a small value like 0.01 and adjust by factors of 2 or 3. If the cost function increases, the learning rate is too high; if convergence is slow, it may be too low.
- 13
What is batch gradient descent and what is its main disadvantage?
Model answer
Batch gradient descent computes the gradient using the entire training set before each parameter update. Its main disadvantage is that for large datasets (millions of examples), each update is very slow because it requires scanning all examples.
Explanation
In batch gradient descent, the update rule sums over all m training examples. For large m, this is computationally expensive, making the algorithm impractical for big data.
- 14
Describe stochastic gradient descent (SGD) and its key advantage over batch gradient descent.
Model answer
SGD updates parameters using the gradient computed from a single training example at a time. Its key advantage is that it can make progress much faster on large datasets without scanning the entire set for each update.
Explanation
SGD loops through training examples one by one, updating parameters after each example. This allows quick iterations, but the path to the minimum is noisy and oscillatory.
- 15
Why does stochastic gradient descent typically not converge exactly to the global minimum, and how can this be mitigated?
Model answer
SGD does not converge exactly because it uses only one example per update, causing oscillations. This can be mitigated by gradually decreasing the learning rate over time.
Explanation
Reducing the learning rate shrinks the step size, so the oscillations become smaller and the parameters settle closer to the minimum. However, SGD rarely reaches the exact optimum for non-convex problems.
- 16
How can you monitor convergence of gradient descent?
Model answer
Plot the cost function J(θ) as a function of the number of iterations; if it stops decreasing significantly, the algorithm has converged.
Explanation
Monitoring J(θ) over time shows whether the learning algorithm is reducing the error. If J(θ) plateaus, it indicates convergence. This is especially useful for stochastic gradient descent.
- 17
What property does the cost function J(θ) for linear regression have regarding local optima?
Model answer
It is a convex quadratic function (a bowl shape) with a single global minimum and no other local optima.
Explanation
Since J(θ) is a sum of squared terms, it is a quadratic function in θ. For ordinary least squares, this quadratic is convex, so any local minimum is also the global minimum.
- 18
Write the normal equations for linear regression and give the closed-form solution for θ.
Model answer
The normal equations are XTXθ=XTy, and the solution is θ=(XTX)−1XTy.
Explanation
By setting the derivative of J(θ) to zero, we obtain XTXθ=XTy. Assuming XTX is invertible, solving gives the optimal θ in one step without iteration.
- 19
What is the design matrix X in linear regression?
Model answer
The design matrix X is an m×(n+1) matrix whose i-th row is the transpose of the feature vector (x(i))T, including x0=1.
Explanation
Each row corresponds to a training example. The columns correspond to features (including the dummy x0). This matrix allows efficient vectorized computation of predictions as Xθ.
- 20
Express the cost function J(θ) in matrix-vector form using the design matrix X and target vector y.
Model answer
J(θ)=21(Xθ−y)T(Xθ−y)
Explanation
The vector Xθ−y contains all errors. Its squared norm is the sum of squared errors, matching the scalar form 21∑i=1m(hθ(x(i))−y(i))2.
- 21
What does it mean if XTX is non-invertible, and how can the issue be addressed?
Model answer
It indicates linearly dependent features (redundant features). The issue can be addressed by using the pseudo-inverse or removing redundant features.
Explanation
Linear dependence causes XTX to be singular. The pseudo-inverse provides a solution, but it is better to identify and remove duplicate features to obtain a unique, interpretable model.
- 22
Define the trace of a square matrix A and state one of its properties used in deriving the normal equations.
Model answer
The trace of A, denoted tr(A), is the sum of its diagonal entries. A key property is tr(ABC)=tr(CAB) (cyclic permutation).
Explanation
Trace properties allow manipulation of matrix expressions. For example, tr(AB)=tr(BA) is used when differentiating scalar functions of matrices.
- 23
What is the derivative of the trace tr(AB) with respect to A, assuming B is constant?
Model answer
∂A∂tr(AB)=BT
Explanation
This is a standard matrix derivative result used in the derivation of the normal equations when expanding J(θ) and differentiating.
- 24
Outline the key steps in deriving the normal equations using matrix calculus.
Model answer
- Express J(θ)=21(Xθ−y)T(Xθ−y). 2) Expand to 21(θTXTXθ−2yTXθ+yTy). 3) Take derivative with respect to θ: ∂θ∂J=XTXθ−XTy. 4) Set derivative to zero and solve: XTXθ=XTy⇒θ=(XTX)−1XTy.
Explanation
The derivation uses matrix identities for derivatives of quadratic forms. The result provides a closed-form solution for linear regression that directly gives the optimal parameters.

Ask anything
We've read this whole study set, so questions about it get the best answers.