The Concept
The Model
Linear regression assumes a linear relationship between input ($x$) and output ($y$):
y = wx + b
w (weight/slope): how much $y$ changes when $x$ increases by 1b (bias/intercept): the value of $y$ when $x = 0$
For multiple inputs (features), this extends to:
y = w_1 x_1 + w_2 x_2 + \cdots + w_n x_n + b
Or in vector form: $y = w^T x + b$
The goal: find the values of $w$ and $b$ that make the predicted $y$ as close as possible to the actual $y$ across all training examples.
The Cost Function (Mean Squared Error)
How do you measure "as close as possible"? You need a single number that captures how wrong your predictions are. The most common choice is Mean Squared Error (MSE):
\text{MSE} = \frac{1}{n} \sum (y_{\text{predicted}} - y_{\text{actual}})^2
Why squared? Two reasons. First, it penalizes large errors more than small errors (an error of 10 is 100x worse than an error of 1, not 10x). Second, the squared function is smooth and differentiable everywhere, which makes optimization straightforward.
The cost function creates a surface. For a single weight $w$ and bias $b$, the MSE surface looks like a bowl (a convex paraboloid). The bottom of the bowl is where MSE is minimized. Training means finding that bottom.
Gradient Descent
Gradient descent finds the bottom of the bowl by taking steps downhill.
The gradients tell you two things: which direction to move each parameter, and how much to move.
For MSE with $\hat{y} = wx + b$:
\frac{\partial \text{MSE}}{\partial w} &= \frac{2}{n} \sum (\hat{y} - y) \, x \\
\frac{\partial \text{MSE}}{\partial b} &= \frac{2}{n} \sum (\hat{y} - y)
The update rule:
w &= w - \text{learning\_rate} \cdot \frac{\partial \text{MSE}}{\partial w} \\
b &= b - \text{learning\_rate} \cdot \frac{\partial \text{MSE}}{\partial b}
The learning rate controls step size. Too large: you overshoot the minimum and diverge. Too small: training takes forever. Typical starting values: 0.01, 0.001, or 0.0001.
The Normal Equation (Closed-Form Solution)
For linear regression specifically, there is a direct formula that gives the optimal weights without any iteration:
w = (X^T X)^{-1} X^T y
This inverts a matrix to solve for $w$ in one step. It works perfectly for small datasets. For large datasets (millions of rows or thousands of features), gradient descent is preferred because matrix inversion is $O(n^3)$ in the number of features.
Multiple Linear Regression
With multiple features, the model becomes:
y = w1*x1 + w2*x2 + ... + wn*xn + b
Everything works the same: MSE is the cost function, gradient descent updates all weights simultaneously. The only difference is that you are fitting a hyperplane instead of a line.
Feature scaling matters here. If one feature ranges from 0 to 1 and another ranges from 0 to 1,000,000, gradient descent will struggle because the cost surface becomes elongated. Standardize features (subtract mean, divide by standard deviation) before training.
Polynomial Regression
What if the relationship is not linear? You can still use linear regression by creating polynomial features:
y = w_1 x + w_2 x^2 + w_3 x^3 + b
This is still "linear" regression because the model is linear in the weights ($w_1, w_2, w_3$). You are just using nonlinear features of $x$.
Higher-degree polynomials can fit more complex curves but risk overfitting. A degree-10 polynomial will pass through every point in a 10-point dataset but predict poorly on new data.
R-Squared Score
MSE tells you how wrong you are, but the number depends on the scale of $y$. R-squared ($R^2$) gives a scale-independent measure:
\begin{aligned}
R^2 &= 1 - \frac{\text{sum of squared residuals}}{\text{sum of squared deviations from mean}} \\
&= 1 - \frac{SS_{\text{res}}}{SS_{\text{tot}}}
\end{aligned}
- $R^2 = 1.0$: perfect predictions
- $R^2 = 0.0$: the model is no better than predicting the mean every time
- $R^2 < 0.0$: the model is worse than predicting the mean
Regularization Preview (Ridge Regression)
When you have many features, the model can overfit by assigning large weights. Ridge regression (L2 regularization) adds a penalty:
\text{Cost} = \text{MSE} + \lambda \sum w_i^2
The penalty term discourages large weights. The hyperparameter $\lambda$ controls the tradeoff: higher $\lambda$ means smaller weights and more regularization. This is covered in depth in a later lesson. For now, know that it exists and why it helps.