Skip to main content
LearnMLAcademy
Intermediate10 min read

Linear Regression

Derive linear regression with Ordinary Least Squares and gradient descent, with Python code, architecture diagrams, worked examples, and interview quiz.

What you will master in this lesson
Intuitive core mechanisms & practical real-world intuition
Step-by-step mathematical breakdown & visual diagrams
Runnable Python implementation & practical trade-offs

Linear Regression

Linear Regression helps us understand relationships between variables and predict continuous numerical values such as prices, sales, temperatures, scores, and demand forecasts.

What is Linear Regression?

Linear Regression is a supervised machine learning algorithm used to predict continuous values by modeling an approximately straight-line relationship between input variables and a numerical target.

Linear Regression in Simple Words

We show the model some examples where the answer is already known. It looks for a straight-line trend and then uses that trend to predict a new number.

1

Past Examples

Hours + marks

2

Find a Trend

Draw a best-fit line

3

New Input

A student studies 5 hours

4

Prediction

Predict the marks

Study HoursExam Score
140
250
360
470

"Imagine observing a pattern: As study hours increase, exam scores increase approximately in a straight-line manner. Linear regression learns this relationship and predicts future values."

Example: Study Hours vs Score

  • 1 Hour ➔ 40 Score
  • 2 Hours ➔ 50 Score
  • 3 Hours ➔ 60 Score
  • 4 Hours ➔ 70 Score

Visual Understanding of Linear Regression

The main objective is to find the best-fit line, minimize prediction errors, and predict future continuous values. Below is a visual representation of the best-fit Regression line based on study hours versus exam score.

Interactive: Be the Algorithm

Drag the blue handles to find the line that best fits the data points. Then, challenge the machine to see its mathematically perfect prediction.

Your Error (MSE)

157.8

Machine Error (MSE)

Hidden
X: Independent VariableY: Dependent Variable

Machine Learning Workflow

The standard machine learning workflow for Linear Regression involves:

  1. Collect Dataset
  2. Clean the Dataset
  3. Feature Engineering
  4. Train Linear Model
  5. Evaluate Performance
  6. Predict Future Values

Important Terminologies

TermMeaning
Independent Variable (X)Input feature used to make predictions.
Dependent Variable (Y)Output prediction / value.
SlopeChange in Y with respect to X. It controls how steeply the line rises or falls.
InterceptValue of Y when X is 0. Controls the starting point.
Residual/ErrorDifference between the actual value and the predicted value.

Best-Fit Line in Linear Regression

In simple linear regression, the best-fit line is the straight line that minimizes a chosen error measure—commonly the sum of squared residuals—between the observed values and the line. This line is used to estimate or predict values based on existing data.

The purpose of the best-fit line is to create a mathematical relationship between variables so that future outcomes can be predicted more easily.

Understanding the Variables

In linear regression:

  • X represents the independent variable (input or predictor).
  • Y represents the dependent variable (output or target).

The regression model estimates how Y tends to change as X changes. A fitted relationship does not by itself prove that X causes Y.

  • b (Intercept): Represents the predicted value of Y when X is equal to 0.
  • m (Slope): Indicates how much predicted Y changes when X increases by one unit.

Linear regression can work with:

  • A single input feature (simple linear regression)
  • Multiple input features (multiple linear regression)

"Among different regression techniques, linear regression is one of the simplest and most commonly used methods."

Equation of the Best-Fit Line

For simple linear regression, the equation of the line is:

y = mx + b

Where:

  • y = predicted output value
  • x = input value
  • m = slope of the line
  • b = intercept of the line

The main objective of linear regression is to determine the best values for the slope and intercept so that the line stays as close as possible to the actual data points.

Equation Explorer

See how changing the slope (m) and intercept (b) affects the line.

Y = 1.0X + 20
1.0

Controls the steepness and direction of the line.

20

Controls where the line crosses the Y axis (when X is 0).

Y Axis (Output)
X Axis (Input)
b = 20Rise = 1.0Run = 1

How the Best-Fit Line is Calculated

Linear regression uses a technique called the Least Squares Method to identify the most suitable line. The model calculates the difference between the actual observed values, and the predicted values generated by the line. These differences are known as residuals or errors.

Residual Calculation

Residual = yᵢ - ŷᵢ

Where:

  • yᵢ = actual observed value
  • ŷᵢ = predicted value from the regression line

Residual in One Tiny Example

Actual score

70

Predicted score

66

Residual

70 - 66 = 4

A positive residual means the actual value is above the prediction; a negative residual means it is below the prediction.

To find the best-fit line, the algorithm minimizes the total squared error:

Σ(yᵢ - ŷᵢ)²

Squaring the errors ensures that:

  • Negative and positive errors do not cancel each other out
  • Larger errors receive greater penalty

"The line with the smallest total squared error is considered the optimal regression line."

Meaning of Slope and Intercept

Slope (m)

The slope explains the rate of change between the variables. For example, if the slope is 5, it means that whenever X increases by 1 unit, Y increases by 5 units.

  • A positive slope indicates an increasing relationship
  • A negative slope indicates a decreasing relationship

Intercept (b)

The intercept represents the predicted value of Y when X equals zero. Graphically, it is the exact point where the regression line crosses the Y-axis.

Why the Best-Fit Line is Important

The best-fit line helps in:

  • Predicting future values
  • Understanding relationships between variables
  • Identifying trends in data
  • Supporting business and scientific decision-making

Linear regression is widely used in:

  • Sales forecasting
  • Stock market analysis
  • Weather prediction
  • Healthcare analytics
  • Machine learning applications

Limitations of the Best-Fit Line

1. Assumes a Linear Relationship

Linear regression performs well only when the relationship between variables follows a straight-line pattern. Complex non-linear relationships may require advanced models.

2. Sensitive to Outliers

Extreme values or outliers can heavily influence the regression line, causing inaccurate predictions.

3. Limited for Complex Problems

Real-world datasets often contain multiple hidden patterns that simple linear regression may not capture effectively.

Real-World Worked Example: House Size vs Price

Let us work through a concrete example to see exactly how the best-fit line is calculated and used for prediction.

Sample Dataset — 5 Houses
HouseSize X (sq ft)Price Y (₹ Lakhs)
House A800₹32L
House B1000₹40L
House C1200₹46L
House D1500₹60L
House E1800₹72L
House Size (sq ft)Price (₹ Lakhs)8001000120015001800020406080Best-fitline(800,32L)(1000,40L)(1200,46L)(1500,60L)(1800,72L)
Figure — Scatter plot with best-fit regression line through 5 house data points

Step-by-Step: Finding the Best-Fit Line

1

Step 1 — Calculate means

Mean of X: (800+1000+1200+1500+1800)/5 = 1260 sq ft

Mean of Y: (32+40+46+60+72)/5 = 50 lakhs

2

Step 2 — Calculate slope (m)

m = Σ(xᵢ - x̄)(yᵢ - ȳ) / Σ(xᵢ - x̄)² = 25400 / 632000

m ≈ 0.04019 lakh per sq ft

3

Step 3 — Calculate intercept (b)

b = ȳ - m × x̄

b = 50 - (0.04019 × 1260) ≈ -0.64

4

Step 4 — Final equation

ŷ = 0.04019x - 0.64

This line gives the least-squares prediction for this five-house sample.

Making a Prediction

A new house has 1400 sq ft. What is the predicted price?

ŷ = 0.04019 × 1400 − 0.64

ŷ ≈ 56.27 − 0.64

ŷ ≈ ₹55.63 Lakhs ← Predicted Price


Types of Linear Regression

Linear Regression

  • Simple Linear Regression: Uses exactly ONE independent variable to predict the outcome.
  • Multiple Linear Regression: Uses MULTIPLE independent variables working together to predict the outcome.

1. Simple Linear Regression

Simple Linear Regression uses one independent variable to predict one dependent variable. Think of it as drawing the best possible straight line through a cloud of data points on a 2D chart.

Mathematical Formula

y = mx + b
  • y = predicted output (what we want to find)
  • x = input feature (what we know)
  • m = slope — how steeply Y changes as X increases
  • b = intercept — the value of Y when X = 0

Real-World Example: Study Hours vs Exam Score

Dataset
Hours (X)Score (Y)
1 hrs20%
2 hrs35%
3 hrs45%
4 hrs55%
5 hrs65%
6 hrs75%
7 hrs85%
Study HoursScore %y=10x+10
Figure — y = 10x + 10

Prediction: How many marks will a student score studying 8 hours?

y = 10x + 10

y = 10(8) + 10

y = 80 + 10

y = 90% ← Predicted Score

2. Multiple Linear Regression

Multiple Linear Regression uses two or more independent variables simultaneously to predict the output. Instead of a 2D line, it fits a hyperplane through multi-dimensional data.

Mathematical Formula

y = b₀ + b₁X₁ + b₂X₂ + ... + bₙXₙ

Where each b is a coefficient learned from data and each X is a feature.

Real-World Example: Predicting House Price (₹ Lakhs)

Learned Model Equation
Price = 2.5 + (0.04 × Area) + (8 × Bedrooms) + (5 × Bathrooms) − (0.5 × Age)
Training Data (sample)
Area (sq ft)BedroomsBathroomsAge (yrs)Price (₹L)
100021561.0
150032395.0
2000431129.0
800211050.5

Prediction: Area=1200, Bedrooms=3, Bathrooms=2, Age=4 years

Price = 2.5 + (0.04 × 1200) + (8 × 3) + (5 × 2) − (0.5 × 4)

Price = 2.5 + 48 + 24 + 10 − 2

Price = ₹82.5 Lakhs ← Predicted Price

In this illustrative equation, each coefficient shows the model's predicted change in price when that feature changes by one unit while the other listed features are held constant.


Assumptions of Linear Regression

Linear Regression can still produce predictions when these conditions are imperfect, but checking them helps us decide whether the fitted line and its coefficients are trustworthy.

CheckSimple Meaning
LinearityA straight-line relationship is a reasonable approximation.
Independent errorsOne residual should not systematically tell us the next residual.
Roughly constant error spreadResiduals should not strongly fan out as predictions increase.
Residual normalityMost important for classical confidence intervals and significance tests; it is not required just to draw a least-squares line.
Low severe multicollinearityIn multiple regression, nearly duplicate predictors can make individual coefficient estimates unstable.

Cost Function & Gradient Descent

Here we explore the key concepts and techniques involved in this topic.

Cost Function (MSE)

The cost function measures how well or poorly our model is performing. Mean Squared Error (MSE) measures prediction error by averaging the squared differences.

Mathematical Formula (MSE)

MSE = (1/n) * Σ (Actual - Predicted)²

Worked-Out Example

Step 1: Errors are -2, 2, and -3.

Step 2: Square them: 4, 4, and 9.

Step 3: Add squared errors: 4 + 4 + 9 = 17.

Step 4: Divide by 3 observations: 17 / 3 = 5.67.

MSE = 5.67

Gradient Descent Optimization

Gradient Descent is one way to minimize a cost function: it repeatedly adjusts parameters in the direction that reduces error. It is very useful for understanding optimization and for many large-scale models.

Important implementation note

Scikit-learn's standard LinearRegression fits ordinary least squares by minimizing residual sum of squares; it is not a gradient-descent estimator. We keep Gradient Descent here because it explains the general idea of minimizing regression error.

Error Minimization Curve

Learning Rate (α) Implications:

  • Small Learning Rate: Slow learning, takes a long time to reach minimum.
  • Large Learning Rate: Might overshoot the minimum completely and fail to converge.
  • Suitable Learning Rate: Makes useful progress without repeatedly overshooting the minimum.

Evaluation Metrics for Linear Regression

Beyond MSE, we use these real-world metrics to evaluate the performance.

1. Mean Absolute Error (MAE)

MAE = (1/n) * Σ |y - y_pred|

Example: Actual=[10, 20], Pred=[12, 18].

|10 - 12| = 2, |20 - 18| = 2. MAE = (2 + 2) / 2 = 2

2. Root Mean Squared Error (RMSE)

RMSE = √MSE

If MSE = 9, RMSE = √9 = 3.

3. R-Squared Score (R²)

R² = 1 - (SS_res / SS_tot)

R² compares the model with a simple baseline that always predicts the mean target. A score of 1 is perfect, around 0 is similar to that mean baseline, and R² can be negative when the model performs worse than the baseline.

Suppose SS_res = 2 and SS_tot = 10

R² = 1 - (2 / 10)

R² = 1 - 0.2

R² = 0.8


Python Implementation of Linear Regression

Below is the complete implementation pipeline using Numpy, Matplotlib, and Scikit-Learn, along with an explanation for each step.

python

Interactive: Train Linear Regression in Python

Adjust the underlying slope and noise level below, then click 'Run Code' to execute ordinary least squares regression.

Live Hyperparameters (Tweak & Re-run)
3.5
1.55.5

The real relationship slope used to generate data.

20
540

Standard deviation of random noise around the regression line.

1import numpy as np
2from sklearn.linear_model import LinearRegression
3from sklearn.metrics import r2_score, mean_squared_error
4
5# Step 1 — Generate Dataset
6np.random.seed(42)
7true_slope = 3.5
8noise_level = 20
9X = np.random.rand(50, 1) * 100
10Y = true_slope * X + np.random.randn(50, 1) * noise_level
11
12# Step 2 — Train Model (Ordinary Least Squares)
13model = LinearRegression()
14model.fit(X, Y)
15
16# Step 3 — Predictions & Coefficients
17Y_pred = model.predict(X)
18learned_slope = round(float(model.coef_[0, 0]), 2)
19learned_intercept = round(float(model.intercept_[0]), 2)
20r2 = round(r2_score(Y, Y_pred), 3)
21mse = round(mean_squared_error(Y, Y_pred), 1)
22
23print(f"Target True Slope: {true_slope}")
24print(f"Noise Std Dev: ±{noise_level}")
25print(f"Fitted Slope (m): {learned_slope}")
26print(f"Fitted Intercept (b): {learned_intercept}")
27print(f"R² Score (Goodness of Fit): {r2}")
28print(f"Mean Squared Error (MSE): {mse}")
Click "Run Code" above to execute this script in the sandbox and inspect live outputs.

Line-by-Line Explanation:

  • import ...: We import numpy for mathematical arrays, matplotlib.pyplot for plotting charts, and LinearRegression from Scikit-Learn to build the model.
  • np.random.seed(42): Ensures our random dataset is exactly the same every time we run the code.
  • X = ... and Y = ...: We generate 50 random samples. X is between 0 and 100. Y is created using a linear relationship (3.5 * X) with some added random noise.
  • model.fit(X, Y): This is the training step. Scikit-learn fits the ordinary least-squares line that minimizes residual sum of squares.
  • Y_pred = ...: Once trained, we feed X back into the model to predict what it thinks Y should be, creating our best-fit line.
  • plt.scatter... plt.plot...: Projects the original noisy data points (scatter) and overlays the predicted line (plot) visually.

Expected numeric output for the fixed random seed

Slope: 3.46
Intercept: 1.93

The underlying data was generated with a slope near 3.5, so the fitted slope being close to 3.5 is a useful sanity check.

Illustrative Visual Output:


Real-World Applications

  • Finance: Stock Prediction, Risk Analysis, Revenue Forecasting.
  • Healthcare: Disease indicator estimation, Medical Cost Prediction, Patient duration monitoring.
  • Retail: Product Sales Forecasting, Demand Prediction, Customer Analytics tracking.
  • Real Estate: House Price Prediction, Rental Valuation Estimation.
  • Manufacturing: Quality tracking, Failure rate estimation, Production demand.

Advantages & Limitations

Advantages

  • Simple: Very easy to understand mathematically.
  • Fast: Often computationally lightweight compared with many more complex models.
  • Interpretable: Coefficients directly explain relationships.
  • Useful Baseline: Provides a strong, interpretable starting point for many regression problems.

Limitations

  • Linear Form: A straight-line model may underfit strongly curved relationships unless useful transformations or features are added.
  • Outlier Sensitivity: Squared-error fitting can be strongly influenced by extreme observations.
  • Multicollinearity: Highly correlated predictors can make individual coefficients unstable and harder to interpret.
  • Underfitting: Can be too naive for highly complex phenomena.

Understanding Overfitting vs Underfitting

Linear Regression is mostly prone to Underfitting if the true data has complex curvature.

  • UNDERFIT Model is too simple. The line ignores distinct complex patterns.
  • GOOD FIT Model captures the main trend without chasing every fluctuation.
  • OVERFIT Model is too complex, attempting to hit every single point (jagged line).

Comparison with Other Algorithms

AlgorithmComplexityInterpretabilitySpeed
Linear RegressionLowHighFast
Decision TreeMediumMediumMedium
Random ForestHighLowMedium
Neural NetworksVery HighVery LowSlow

Common Questions About Linear Regression

Does the best-fit line have to pass through every point?

No. Real data contains noise. The goal is to minimize overall error, not to touch every observation.

Can Linear Regression predict categories such as Pass or Fail?

It is designed for numerical targets. For a category such as Pass/Fail, a classification method such as Logistic Regression is usually more appropriate.

Does a large positive slope prove that X causes Y?

No. Regression describes an association in the fitted data. Causal conclusions require additional study design and assumptions.

Continue Learning

Now that the line, residuals, and error are clear, continue with the topics that explain optimization and regularization.


Final Summary

Linear Regression is an important algorithm in Machine Learning because it introduces the core concepts of predictive modeling, optimization, and statistical learning. It is simple, fast, interpretable, and widely used in real-world applications.

Linear Regression is a strong starting choice when an approximately linear relationship is plausible, interpretability matters, and you want a simple baseline before trying more complex models.

Most Important Insight to Remember:

"Linear Regression is valuable because one simple line connects data, prediction, error, optimization, coefficients, and model evaluation in a form that is easy to inspect."

Apply this concept in a real hands-on project

You have studied the idea. Now use it in working Python code, see the calculation and output, and explain what changed. Each project includes step-by-step instructions, full source and tests.

Practice challenge: predict an outcome first, run the code, and explain the difference.

Track Your Progress0% curriculum done

Finished "Linear Regression"?

Mark this lesson as completed to update your progress bar and track your milestones across the full curriculum.

Overall Progress0 / 167 lessons

Was this tutorial helpful?

Your feedback helps us continuously refine and improve our open-access tutorials.

Summary / Key Takeaways

  • Linear regression predicts with an intercept plus weighted input features.
  • A coefficient describes an expected target change with other included features held fixed.
  • Least squares minimizes squared residuals.
  • Residuals help assess linearity, variance, and influential observations.
  • Correlated inputs can destabilize coefficients even when predictions remain useful.
  • Association in a coefficient is not proof of causation.

Knowledge Check

Progress: 0/4

Test your active recall. Choose one answer for each question, then check it to reveal the step-by-step reasoning.

Question 1 of 4
A residual plot shows a funnel shape — residuals are small for low fitted values and large for high ones. What problem does this indicate?
Question 2 of 4
A coefficient of 4.2 for years_experience in a salary model means precisely:
Question 3 of 4
Two features correlate at 0.96. Training gives total_sq_ft coefficient=-127 and living_area=+219. How do you interpret this?
Question 4 of 4
Your linear regression produces negative predictions for very old houses. What is most likely happening?

Knowledge Check Review & Explanations

Review the questions, verified correct answers, and conceptual explanations for this lesson.

1A residual plot shows a funnel shape — residuals are small for low fitted values and large for high ones. What problem does this indicate?▼

Correct Answer: Heteroscedasticity — error variance is not constant. Linear regression assumes homoscedasticity. Try log-transforming the target variable to stabilise variance

Homoscedasticity (constant error variance) is a key assumption. A funnel-shaped residual plot violates it. Log-transforming the target (predicting log price instead of price) often fixes this for monetary quantities.

2A coefficient of 4.2 for years_experience in a salary model means precisely:▼

Correct Answer: Holding all other features constant, each additional year of experience is associated with a predicted salary increase of 4.2 (in salary units)

The 'holding all other features constant' part is critical and often forgotten. Linear regression coefficients have this specific ceteris paribus interpretation.

3Two features correlate at 0.96. Training gives total_sq_ft coefficient=-127 and living_area=+219. How do you interpret this?▼

Correct Answer: Multicollinearity has made individual coefficients unreliable and uninterpretable. A negative coefficient for total size makes no physical sense. The model may still predict well, but coefficients cannot be used for interpretation

When features are highly collinear, the model can achieve the same prediction with many different coefficient combinations. The specific values become highly sensitive to noise and lose interpretability.

4Your linear regression produces negative predictions for very old houses. What is most likely happening?▼

Correct Answer: Linear regression extrapolates beyond the training distribution. If old houses were rare in training, the model extends its learned trend beyond reasonable limits. Tree-based models are less susceptible because they cannot extrapolate beyond training ranges

Linear regression is an extrapolating model — it has no built-in constraints on output range. Tree-based models return the mean of the nearest training leaf, so they cannot extrapolate beyond training data ranges.

Keep moving through the path

Continue Learning

Free Download: ML Interview Cheatsheet

100 essential Machine Learning interview questions and answers covering foundations, algorithms, evaluation, deep learning, Python, system design and interview strategy. Enter your email to download it instantly.

Optional Learning Resources

These resources may complement the lesson. They are optional and are not required to use ML Academy.

As an Amazon Associate I earn from qualifying purchases. Other links are identified separately when they are not affiliate links.

Next