🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #93

Linear Regression Cost Function

Linear regression needs a precise, numeric definition of "best fit" before it can search for it — the cost function (Mean Squared Error) is exactly that definition, turning "how good is this line?" into a single number to minimize.

Formula

\[ J(b_0, b_1) = \text{MSE} = \frac{1}{n}\sum_{i=1}^{n}(y_i - \hat{y}_i)^2 \]

\(y_i\) is the actual value, \(\hat{y}_i = b_0 + b_1x_i\) is the model's prediction, and \(n\) is the number of data points. \(J(b_0,b_1)\) is written as a function of the coefficients specifically because it's exactly those two numbers — not the data — that training searches over to minimize this cost.

Why Squared Error, Not Just Error?

  • Positive and negative errors don't cancel out — squaring makes every term positive, same reasoning as variance's squared deviations
  • Large errors are penalized disproportionately more — an error of 10 contributes 100 to the sum, while an error of 2 contributes only 4; this pushes the model to avoid any single badly-wrong prediction
  • It's smooth and differentiable everywhere — required for gradient descent to compute a usable gradient at every point

Numerical Example

Using the residuals already computed in Simple Linear Regression: \(0.2, 0.6, -1.0, -0.6, 0.8\).

\[ \text{MSE} = \frac{(0.2)^2+(0.6)^2+(-1.0)^2+(-0.6)^2+(0.8)^2}{5} = \frac{0.04+0.36+1.0+0.36+0.64}{5} = \frac{2.4}{5} = 0.48 \]
from sklearn.metrics import mean_squared_error

y_actual = [52, 58, 62, 68, 75]
y_pred =   [51.8, 57.4, 63.0, 68.6, 74.2]

print(mean_squared_error(y_actual, y_pred))   # 0.48

The Cost Surface — Why It's Shaped Like a Bowl

Cost surface J(b0, b1) minimum (b0=46.2, b1=5.6) b0 axis → b1 axis

Because MSE is a sum of squared terms, the cost surface for linear regression is always a smooth, convex bowl — with exactly one global minimum, no other local minima to get stuck in.

This convexity is a genuinely important, practical guarantee: unlike deep neural networks (whose cost surfaces have many local minima), linear regression's cost surface is provably bowl-shaped — gradient descent (or the closed-form Normal Equation) is guaranteed to find the single global best-fit line, not just a locally good one.

MSE vs MAE — Why This Specific Choice

MSE (used here)MAE
Formula\(\frac{1}{n}\sum(y_i-\hat{y}_i)^2\)\(\frac{1}{n}\sum|y_i-\hat{y}_i|\)
Outlier sensitivityHigh — squaring amplifies large errorsLower — errors scale linearly
Differentiable everywhere?Yes, smoothlyNot at zero (a sharp corner)
Standard choice for training linear regression?YesLess common, though usable

Common Mistakes

  • Confusing the cost function (computed on the training set, used to fit the model) with an evaluation metric reported on a held-out test set — they can use the identical formula but serve different purposes.
  • Assuming a lower training MSE always means a better model — an unreasonably low training MSE relative to validation MSE is a sign of overfitting.

Interview Relevance

Q: "Why is linear regression's cost function guaranteed to have a single global minimum?" Because MSE, as a function of the coefficients, is convex (bowl-shaped) — a mathematical property of sums of squared linear terms — so there are no other local minima for gradient descent or the Normal Equation to get stuck in.

Practice Question

Given actual values \([10, 20, 30]\) and predictions \([12, 18, 33]\), compute the MSE by hand.

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Linear Regression Cost Function – FAQs

Quick answers about learning Linear Regression Cost Function in Machine Learning.

This free note from CodingNow 2.0 explains Linear Regression Cost Function in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including Linear Regression Cost Function, is 100% free with no signup required.
With focused practice, most students grasp Linear Regression Cost Function in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now