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

Simple Linear Regression

Simple linear regression fits a straight line through data with exactly one feature — the cleanest place to see, by hand, exactly how a regression model's coefficients get computed.

Formula for the Coefficients

\[ b_1 = \frac{\sum(x_i-\bar{x})(y_i-\bar{y})}{\sum(x_i-\bar{x})^2}, \qquad b_0 = \bar{y} - b_1\bar{x} \]

This should look familiar — the numerator of \(b_1\) is the same sum used in covariance, and the denominator is the same sum used in variance. In fact, \(b_1 = \text{Cov}(X,Y) / \text{Var}(X)\) exactly — simple linear regression's slope is literally a ratio of covariance to variance.

Step-by-Step Numerical Example

Five students: hours studied \(x = [1,2,3,4,5]\), exam marks \(y=[52,58,62,68,75]\).

StepCalculation
1. Means\(\bar{x}=3\), \(\bar{y}=63\)
2. Deviations \((x_i-\bar{x})\)\(-2,-1,0,1,2\)
3. Deviations \((y_i-\bar{y})\)\(-11,-5,-1,5,12\)
4. Products, summed\(22+5+0+5+24 = 56\)
5. Squared X deviations, summed\(4+1+0+1+4 = 10\)
6. Slope\(b_1 = 56/10 = 5.6\)
7. Intercept\(b_0 = 63 - 5.6(3) = 46.2\)

Final equation: \(\hat{y} = 46.2 + 5.6x\) — matching exactly what scikit-learn returned in the Linear Regression hub note.

Checking the Predictions and Residuals

xActual yPredicted ŷ = 46.2 + 5.6xResidual (y − ŷ)
15251.80.2
25857.40.6
36263.0−1.0
46868.6−0.6
57574.20.8

Notice the residuals sum to (approximately) zero — this isn't a coincidence, it's a mathematical guarantee of the least-squares fitting method used to derive \(b_0\) and \(b_1\).

Python — From Scratch and scikit-learn

# From scratch, following the formula exactly
x = [1, 2, 3, 4, 5]
y = [52, 58, 62, 68, 75]
n = len(x)

x_mean = sum(x) / n
y_mean = sum(y) / n

numerator = sum((x[i] - x_mean) * (y[i] - y_mean) for i in range(n))
denominator = sum((x[i] - x_mean) ** 2 for i in range(n))

b1 = numerator / denominator
b0 = y_mean - b1 * x_mean
print(b0, b1)   # 46.2  5.6

# scikit-learn -- same result, far less code
from sklearn.linear_model import LinearRegression
import numpy as np

model = LinearRegression().fit(np.array(x).reshape(-1, 1), y)
print(model.intercept_, model.coef_)   # 46.2  [5.6]

Practical Use Cases

  • Any two-variable relationship — advertising spend vs sales, temperature vs ice cream sales, experience vs salary
  • A fast, interpretable first check before reaching for a more complex model

Common Mistakes

  • Forgetting that \(b_1\) is only meaningful for the range of \(x\) actually observed in the data — extrapolating far beyond the training range is unreliable.
  • Interpreting \(b_0\) (the intercept) literally when \(x=0\) is outside any realistic or observed range (e.g. "0 hours studied" might genuinely make sense, but "0 square feet" for a house price model wouldn't).

Interview Relevance

Q: "What's the relationship between the regression slope and covariance/variance?" The slope \(b_1\) equals \(\text{Cov}(X,Y)/\text{Var}(X)\) exactly — simple linear regression's coefficient is a direct ratio of how X and Y vary together to how much X varies on its own.

Practice Question

For \(x=[2,4,6,8]\), \(y=[3,7,5,10]\), compute \(\bar{x}\), \(\bar{y}\), and use the formula above to find \(b_1\) and \(b_0\) 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

Simple Linear Regression – FAQs

Quick answers about learning Simple Linear Regression in Machine Learning.

This free note from CodingNow 2.0 explains Simple Linear Regression 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 Simple Linear Regression, is 100% free with no signup required.
With focused practice, most students grasp Simple Linear Regression 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