1. Home
  2. Explore
  3. Computer Science
  4. Linear Regression Introduction

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Linear Regression Introduction

  • 24Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Supervised Learning Fundamentals

What is supervised learning?

24 questions

  1. 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)(x, y)(x,y) and learns a mapping from inputs to outputs.

    Explanation

    In supervised learning, the training data includes both the input features xxx and the target labels yyy, and the goal is to learn a function hhh that predicts yyy from xxx.

  2. 2

    What is the difference between a regression problem and a classification problem?

    Model answer

    In regression, the output yyy is a continuous value, while in classification, the output yyy 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. 3

    Write the hypothesis for linear regression with nnn features, using the notation where x0=1x_0 = 1x0​=1.

    Model answer

    The hypothesis is hθ(x)=θ0x0+θ1x1+⋯+θnxn=∑j=0nθjxjh_\theta(x) = \theta_0 x_0 + \theta_1 x_1 + \dots + \theta_n x_n = \sum_{j=0}^n \theta_j x_jhθ​(x)=θ0​x0​+θ1​x1​+⋯+θn​xn​=∑j=0n​θj​xj​, where x0=1x_0 = 1x0​=1.

    Explanation

    This compact notation treats the bias term θ0\theta_0θ0​ as the coefficient for a dummy feature x0=1x_0=1x0​=1, allowing the hypothesis to be written as a linear combination of parameters and features.

  4. 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\theta_0θ0​ (the intercept), so it does not pass through the origin unless θ0=0\theta_0=0θ0​=0.

    Explanation

    A linear function must satisfy f(ax+by)=af(x)+bf(y)f(ax + by) = a f(x) + b f(y)f(ax+by)=af(x)+bf(y), but the presence of θ0\theta_0θ0​ violates this unless θ0=0\theta_0=0θ0​=0. However, in machine learning it is still commonly called linear regression.

  5. 5

    Define the notation mmm, nnn, x(i)x^{(i)}x(i), and y(i)y^{(i)}y(i) as used in linear regression.

    Model answer

    mmm is the number of training examples, nnn is the number of features, x(i)x^{(i)}x(i) is the input feature vector of the iii-th training example, and y(i)y^{(i)}y(i) is the target output for that example.

    Explanation

    The superscript (i)(i)(i) indexes the training examples, e.g., x(1)x^{(1)}x(1) is the first training example. The input vector x(i)x^{(i)}x(i) has nnn components, plus the dummy x0=1x_0=1x0​=1.

  6. 6

    Write the cost function J(θ)J(\theta)J(θ) for linear regression (ordinary least squares).

    Model answer

    J(θ)=12m∑i=1m(hθ(x(i))−y(i))2J(\theta) = \frac{1}{2m} \sum_{i=1}^{m} (h_\theta(x^{(i)}) - y^{(i)})^2J(θ)=2m1​∑i=1m​(hθ​(x(i))−y(i))2

    Explanation

    The cost function measures the average squared difference between predicted values hθ(x(i))h_\theta(x^{(i)})hθ​(x(i)) and actual values y(i)y^{(i)}y(i). The factor 1/21/21/2 is included for mathematical convenience when taking derivatives.

  7. 7

    Why is the factor 12\frac{1}{2}21​ included in the linear regression cost function?

    Model answer

    It simplifies the derivative calculation because the 222 from the derivative of the square cancels with the 1/21/21/2, leaving no constant factor in the gradient.

    Explanation

    When taking the partial derivative of J(θ)J(\theta)J(θ) with respect to θj\theta_jθj​, the 1/21/21/2 cancels the 222 that comes from differentiating (hθ−y)2(h_\theta - y)^2(hθ​−y)2, making the gradient expression cleaner.

  8. 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 yyy is Gaussian distributed with mean hθ(x)h_\theta(x)hθ​(x). This will be explained in the context of GLMs.

  9. 9

    Write the update rule for gradient descent for a single parameter θj\theta_jθj​ in linear regression.

    Model answer

    θj:=θj−α∂∂θjJ(θ)\theta_j := \theta_j - \alpha \frac{\partial}{\partial \theta_j} J(\theta)θj​:=θj​−α∂θj​∂​J(θ), where α\alphaα 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. 10

    Derive the partial derivative ∂∂θjJ(θ)\frac{\partial}{\partial \theta_j} J(\theta)∂θj​∂​J(θ) for linear regression assuming a single training example (x,y)(x, y)(x,y).

    Model answer

    ∂∂θjJ(θ)=(hθ(x)−y) xj\frac{\partial}{\partial \theta_j} J(\theta) = (h_\theta(x) - y) \, x_j∂θj​∂​J(θ)=(hθ​(x)−y)xj​

    Explanation

    Starting from J(θ)=12(hθ(x)−y)2J(\theta) = \frac12 (h_\theta(x) - y)^2J(θ)=21​(hθ​(x)−y)2, differentiate using the chain rule: ∂J∂θj=(hθ−y)⋅∂hθ∂θj\frac{\partial J}{\partial \theta_j} = (h_\theta - y) \cdot \frac{\partial h_\theta}{\partial \theta_j}∂θj​∂J​=(hθ​−y)⋅∂θj​∂hθ​​. Since hθ=∑kθkxkh_\theta = \sum_k \theta_k x_khθ​=∑k​θk​xk​, ∂hθ∂θj=xj\frac{\partial h_\theta}{\partial \theta_j} = x_j∂θj​∂hθ​​=xj​.

  11. 11

    What is the effect of a very large learning rate α\alphaα 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 α\alphaα 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. 12

    How is the learning rate α\alphaα 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. 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 mmm training examples. For large mmm, this is computationally expensive, making the algorithm impractical for big data.

  14. 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. 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. 16

    How can you monitor convergence of gradient descent?

    Model answer

    Plot the cost function J(θ)J(\theta)J(θ) as a function of the number of iterations; if it stops decreasing significantly, the algorithm has converged.

    Explanation

    Monitoring J(θ)J(\theta)J(θ) over time shows whether the learning algorithm is reducing the error. If J(θ)J(\theta)J(θ) plateaus, it indicates convergence. This is especially useful for stochastic gradient descent.

  17. 17

    What property does the cost function J(θ)J(\theta)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(θ)J(\theta)J(θ) is a sum of squared terms, it is a quadratic function in θ\thetaθ. For ordinary least squares, this quadratic is convex, so any local minimum is also the global minimum.

  18. 18

    Write the normal equations for linear regression and give the closed-form solution for θ\thetaθ.

    Model answer

    The normal equations are XTXθ=XTyX^T X \theta = X^T yXTXθ=XTy, and the solution is θ=(XTX)−1XTy\theta = (X^T X)^{-1} X^T yθ=(XTX)−1XTy.

    Explanation

    By setting the derivative of J(θ)J(\theta)J(θ) to zero, we obtain XTXθ=XTyX^T X \theta = X^T yXTXθ=XTy. Assuming XTXX^T XXTX is invertible, solving gives the optimal θ\thetaθ in one step without iteration.

  19. 19

    What is the design matrix XXX in linear regression?

    Model answer

    The design matrix XXX is an m×(n+1)m \times (n+1)m×(n+1) matrix whose iii-th row is the transpose of the feature vector (x(i))T(x^{(i)})^T(x(i))T, including x0=1x_0=1x0​=1.

    Explanation

    Each row corresponds to a training example. The columns correspond to features (including the dummy x0x_0x0​). This matrix allows efficient vectorized computation of predictions as XθX\thetaXθ.

  20. 20

    Express the cost function J(θ)J(\theta)J(θ) in matrix-vector form using the design matrix XXX and target vector yyy.

    Model answer

    J(θ)=12(Xθ−y)T(Xθ−y)J(\theta) = \frac{1}{2} (X\theta - y)^T (X\theta - y)J(θ)=21​(Xθ−y)T(Xθ−y)

    Explanation

    The vector Xθ−yX\theta - yXθ−y contains all errors. Its squared norm is the sum of squared errors, matching the scalar form 12∑i=1m(hθ(x(i))−y(i))2\frac12 \sum_{i=1}^m (h_\theta(x^{(i)}) - y^{(i)})^221​∑i=1m​(hθ​(x(i))−y(i))2.

  21. 21

    What does it mean if XTXX^T XXTX 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 XTXX^T XXTX 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. 22

    Define the trace of a square matrix AAA and state one of its properties used in deriving the normal equations.

    Model answer

    The trace of AAA, denoted tr⁡(A)\operatorname{tr}(A)tr(A), is the sum of its diagonal entries. A key property is tr⁡(ABC)=tr⁡(CAB)\operatorname{tr}(ABC) = \operatorname{tr}(CAB)tr(ABC)=tr(CAB) (cyclic permutation).

    Explanation

    Trace properties allow manipulation of matrix expressions. For example, tr⁡(AB)=tr⁡(BA)\operatorname{tr}(AB) = \operatorname{tr}(BA)tr(AB)=tr(BA) is used when differentiating scalar functions of matrices.

  23. 23

    What is the derivative of the trace tr⁡(AB)\operatorname{tr}(AB)tr(AB) with respect to AAA, assuming BBB is constant?

    Model answer

    ∂∂Atr⁡(AB)=BT\frac{\partial}{\partial A} \operatorname{tr}(AB) = B^T∂A∂​tr(AB)=BT

    Explanation

    This is a standard matrix derivative result used in the derivation of the normal equations when expanding J(θ)J(\theta)J(θ) and differentiating.

  24. 24

    Outline the key steps in deriving the normal equations using matrix calculus.

    Model answer
    1. Express J(θ)=12(Xθ−y)T(Xθ−y)J(\theta) = \frac12 (X\theta - y)^T (X\theta - y)J(θ)=21​(Xθ−y)T(Xθ−y). 2) Expand to 12(θTXTXθ−2yTXθ+yTy)\frac12 (\theta^T X^T X \theta - 2y^T X \theta + y^T y)21​(θTXTXθ−2yTXθ+yTy). 3) Take derivative with respect to θ\thetaθ: ∂J∂θ=XTXθ−XTy\frac{\partial J}{\partial \theta} = X^T X \theta - X^T y∂θ∂J​=XTXθ−XTy. 4) Set derivative to zero and solve: XTXθ=XTy⇒θ=(XTX)−1XTyX^T X \theta = X^T y \Rightarrow \theta = (X^T X)^{-1} X^T yXTXθ=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.

1 of 24
Question 1 of 24

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent