Stop 09 · Advanced Analytics & Predictive BasicsTopics · 2/8

Linear Regression for Analysts

Part of the Data Analyst Roadmap

In short

'How much does each extra rupee of ad spend bring in?' 'How much does price change with size?' Linear regression draws the best straight line through your data and puts a number on the relationship.

Extending Phase 4's regression basics into an actual predictive tool — fitting a line (or hyperplane) to predict a continuous outcome from one or more input variables.

The jaadu points at the fare meter of an auto-rickshaw with milestones behind.

Think of it like this

The auto-rickshaw meter

An auto fare starts with a minimum charge, then adds a fixed amount per kilometre. Plot fare against distance and you get a straight line. The starting charge is the intercept, the per-km rate is the slope. Regression finds those two numbers from real, slightly messy rides.

Watch it happen

Fares for rides of 1 to 5 km

52
0
63
1
82
2
93
3
111
4

1/5Five real rides of 1, 2, 3, 4 and 5 km, fares in rupees. Not perfectly even, real life never is.

The big ideas

What you need to know

Slope and intercept

Slope: change in y for one unit of x. Intercept: predicted y when x is zero.

R² shows fit

Share of the variation the line explains, from 0 to 1. High R² isn't proof of cause.

Multiple regression

Several inputs at once, like price and ad spend. Each coefficient is 'holding the others fixed'.

Correlation is not cause

Ice cream sales and drowning rise together because of summer. Regression alone can't separate them.

See it in code

Walk through the code

python
1import pandas as pd2from sklearn.linear_model import LinearRegression3 4rides = pd.DataFrame({"km": [1, 2, 3, 4, 5], "fare": [52, 63, 82, 93, 111]})5X, y = rides[["km"]], rides["fare"]6 7model = LinearRegression().fit(X, y)8print("intercept:", round(model.intercept_, 1))9print("per km:", round(model.coef_[0], 1))10print("R²:", round(model.score(X, y), 3))11 12print(model.predict(pd.DataFrame({"km": [7]})))

1/4X is a table of inputs (double brackets), y is the outcome column.

Fit a line to five rides and read off the base charge, per-km rate, fit and a prediction. Prints 35.8, 14.8, about 0.99 and about 139.4.

Save yourself the pain

Common mistakes

Oops

Saying 'ad spend causes sales' from a regression on past data.

Do this instead

Say they're associated. To claim cause, you need an experiment or careful design.

Oops

Predicting far outside the range you trained on.

Do this instead

Trust predictions mainly within the data's range. Patterns often bend outside it.

Your mission

Fit your first line

~25 min

Quick check

A model says sales = 2000 + 3.5 × ad_spend. What does 3.5 mean?

Go deeper

  1. 1Fit a simple linear regression in Python (or Excel) predicting one variable from another.
  2. 2Learn to sanity-check a regression's coefficients against real-world intuition.
  3. 3Understand the core assumptions (linearity, independence) well enough to know when they're violated.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge