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

Linear Regression

Notation and Problem Setup

Supervised Learning Recap

In supervised learning, the algorithm is given a training set of input-output pairs (x,y)(x, y)(x,y). The goal is to learn a hypothesis function hhh that maps inputs xxx to outputs yyy, generalizing to new, unseen inputs. In a regression problem, the output yyy is a continuous value (e.g., house price). This contrasts with classification, where yyy is discrete (e.g., category).

The simplest regression algorithm is linear regression, which models hhh as a linear (affine) function of the inputs.

Hypothesis Representation

For a single input feature xxx (e.g., house size), the hypothesis is:

hθ(x)=θ0+θ1xh_\theta(x) = \theta_0 + \theta_1 xhθ​(x)=θ0​+θ1​x

where θ0\theta_0θ0​ is the intercept and θ1\theta_1θ1​ is the slope. The parameters θ=[θ0,θ1]T\theta = [\theta_0, \theta_1]^Tθ=[θ0​,θ1​]T are chosen to fit the data.

For multiple features (e.g., size and number of bedrooms), let x1,x2,…,xnx_1, x_2, \dots, x_nx1​,x2​,…,xn​ be the input features. The hypothesis becomes:

hθ(x)=θ0+θ1x1+θ2x2+⋯+θnxnh_\theta(x) = \theta_0 + \theta_1 x_1 + \theta_2 x_2 + \dots + \theta_n x_nhθ​(x)=θ0​+θ1​x1​+θ2​x2​+⋯+θn​xn​

To simplify notation, define an extra dummy feature x0=1x_0 = 1x0​=1. Then:

hθ(x)=∑j=0nθjxjh_\theta(x) = \sum_{j=0}^n \theta_j x_jhθ​(x)=∑j=0n​θj​xj​

Here θ\thetaθ and xxx are (n+1)(n+1)(n+1)-dimensional vectors: θ=[θ0,θ1,…,θn]T\theta = [\theta_0, \theta_1, \dots, \theta_n]^Tθ=[θ0​,θ1​,…,θn​]T, x=[x0,x1,…,xn]Tx = [x_0, x_1, \dots, x_n]^Tx=[x0​,x1​,…,xn​]T.

Notation

  • mmm: number of training examples.

  • nnn: number of features (excluding x0x_0x0​).

  • x(i)x^{(i)}x(i): input features of the iii-th training example.

  • y(i)y^{(i)}y(i): output (target) value of the iii-th training example.

  • θ\thetaθ: parameters (weights) of the model.

  • hθ(x)h_\theta(x)hθ​(x): hypothesis (predicted output) for input xxx.

Key terms: Parameters θ\thetaθ are what the learning algorithm tunes. The hypothesis hθ(x)h_\theta(x)hθ​(x) is the function that maps features to predictions.


Cost Function

The goal is to choose θ\thetaθ so that hθ(x)h_\theta(x)hθ​(x) is close to yyy for all training examples. The cost function (also called the loss function) measures the error. For linear regression, we use the mean squared error (MSE) with a factor of 12\frac{1}{2}21​ for convenience:

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

This is also known as ordinary least squares. The 12\frac{1}{2}21​ simplifies derivatives later. Minimizing J(θ)J(\theta)J(θ) yields the optimal θ\thetaθ.

Why squared error? It corresponds to assuming Gaussian noise in the data, as will be justified in the context of generalized linear models.


Gradient Descent

Gradient descent is an iterative optimization algorithm for finding θ\thetaθ that minimizes J(θ)J(\theta)J(θ). The idea: start with an initial guess for θ\thetaθ (e.g., all zeros) and repeatedly update θ\thetaθ in the direction that reduces J(θ)J(\theta)J(θ) the fastest.

Update Rule (for one training example)

The partial derivative of J(θ)J(\theta)J(θ) with respect to θj\theta_jθj​ (using a single example) is:

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

This follows from the chain rule. The gradient descent update for each θj\theta_jθj​ is:

θ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 (step size). For the full training set, the derivative is averaged over all examples.

Batch Gradient Descent

In batch gradient descent, each update uses all mmm training examples. Repeat until convergence:

θj:=θj−α1m∑i=1m(hθ(x(i))−y(i))xj(i)for all j\theta_j := \theta_j - \alpha \frac{1}{m} \sum_{i=1}^m \left( h_\theta(x^{(i)}) - y^{(i)} \right) x_j^{(i)} \quad \text{for all } jθj​:=θj​−αm1​∑i=1m​(hθ​(x(i))−y(i))xj(i)​for all j

This algorithm looks at the entire "batch" of data before making a single step. It converges to the global minimum (for linear regression, J(θ)J(\theta)J(θ) is convex and has no local optima). However, when mmm is very large (e.g., millions), each iteration is slow.

Stochastic Gradient Descent

Stochastic gradient descent (SGD) updates θ\thetaθ using only one training example at a time. For each iii from 1 to mmm:

θj:=θj−α(hθ(x(i))−y(i))xj(i)for all j\theta_j := \theta_j - \alpha \left( h_\theta(x^{(i)}) - y^{(i)} \right) x_j^{(i)} \quad \text{for all } jθj​:=θj​−α(hθ​(x(i))−y(i))xj(i)​for all j

SGD takes a noisy, random path toward the optimum. It never converges exactly but oscillates around the minimum. For large datasets, SGD is much faster because a single pass through the data already provides many updates. In practice, the learning rate α\alphaα is often decreased over time to reduce oscillations.

Mini-batch gradient descent, a compromise, uses a small batch of bbb examples (e.g., b=100b=100b=100) at each step.

Learning Rate Selection

  • The learning rate α\alphaα controls step size.

  • Too large: may overshoot and cause J(θ)J(\theta)J(θ) to increase.

  • Too small: slow convergence.

  • Common practice: try values on an exponential scale (e.g., 0.01, 0.03, 0.1, 0.3) and pick the one that minimizes J(θ)J(\theta)J(θ) fastest. Feature scaling (scaling inputs to [−1,1][-1,1][−1,1]) helps make learning rate selection more robust.


Normal Equations

For linear regression, there exists a closed-form solution that gives the optimal θ\thetaθ directly, without iteration. This is derived by setting the derivative of J(θ)J(\theta)J(θ) to zero.

Matrix Notation for the Cost Function

Define the design matrix XXX of size m×(n+1)m \times (n+1)m×(n+1) where row iii contains the features of the iii-th example:

X=[—(x(1))T——(x(2))T—⋮—(x(m))T—]X = \begin{bmatrix} — (x^{(1)})^T — \\ — (x^{(2)})^T — \\ \vdots \\ — (x^{(m)})^T — \end{bmatrix}X=​—(x(1))T——(x(2))T—⋮—(x(m))T—​​

Let y=[y(1),y(2),…,y(m)]T\mathbf{y} = [y^{(1)}, y^{(2)}, \dots, y^{(m)}]^Ty=[y(1),y(2),…,y(m)]T be the vector of target values. Then:

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

Matrix Derivatives

To differentiate J(θ)J(\theta)J(θ) with respect to θ\thetaθ, we use matrix calculus. Key definitions:

  • For a function f:Rp×q→Rf: \mathbb{R}^{p \times q} \to \mathbb{R}f:Rp×q→R, the derivative ∇Af(A)\nabla_A f(A)∇A​f(A) is a matrix of the same size, with entries ∂f∂Aij\frac{\partial f}{\partial A_{ij}}∂Aij​∂f​.

  • Trace operator: For a square matrix AAA, tr⁡(A)=∑iAii\operatorname{tr}(A) = \sum_i A_{ii}tr(A)=∑i​Aii​.

  • Useful identities:

    • ∇Atr⁡(AB)=BT\nabla_A \operatorname{tr}(AB) = B^T∇A​tr(AB)=BT

    • tr⁡(ABC)=tr⁡(CAB)=tr⁡(BCA)\operatorname{tr}(ABC) = \operatorname{tr}(CAB) = \operatorname{tr}(BCA)tr(ABC)=tr(CAB)=tr(BCA) (cyclic property)

    • ∇Atr⁡(AATC)=CA+CTA\nabla_A \operatorname{tr}(A A^T C) = C A + C^T A∇A​tr(AATC)=CA+CTA

Derivation of Normal Equations

Expanding J(θ)J(\theta)J(θ):

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

Taking the gradient with respect to θ\thetaθ and setting to zero:

∇θJ(θ)=XTXθ−XTy=0\nabla_\theta J(\theta) = X^T X \theta - X^T \mathbf{y} = 0∇θ​J(θ)=XTXθ−XTy=0

This yields the normal equations:

XTXθ=XTyX^T X \theta = X^T \mathbf{y}XTXθ=XTy

Solving for θ\thetaθ gives:

θ=(XTX)−1XTy\theta = (X^T X)^{-1} X^T \mathbf{y}θ=(XTX)−1XTy

This is the optimal solution in one step (provided XTXX^T XXTX is invertible). If XTXX^T XXTX is singular, it usually indicates redundant (linearly dependent) features. In that case, one can use the pseudo-inverse or remove redundant features.

Key advantage: No need to choose a learning rate or iterate; one matrix computation yields the global minimum.


Gradient descent (especially stochastic) and the normal equations are foundational techniques for fitting linear models. Linear regression is the simplest supervised learning algorithm, but the concepts of hypothesis, cost function, and optimization carry over to more advanced algorithms discussed later.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent