Stop 05 · Machine Learning FundamentalsTopics · 2/12

Linear Regression

Part of the AI Engineer Roadmap

In short

Linear regression is the first model to try for predicting a number. It's fast, easy to explain, and a strong baseline that fancier models must beat to earn their place.

The simplest predictive model — fitting a straight line (or hyperplane) to predict a continuous value — and still the right first baseline for any regression problem.

The mascot looks at a round dial electricity meter on a wall next to an air conditioner.

Think of it like this

Guessing the electricity bill

Your bill goes up with AC hours, the number of people at home and the size of the flat. Linear regression learns how many rupees each one adds, plus a fixed base charge, from past months' bills.

Watch it happen

Building a bill predictor

Data
Split
Fit
Coefficients
Evaluate

1/5Past months: AC hours, people, flat size and the bill in rupees.

The big ideas

What you need to know

Prediction is a weighted sum

bill = base + w1 × AC hours + w2 × people + w3 × size. Each weight is readable.

Coefficients explain

A weight is the change in the prediction for one more unit of that feature, others held fixed.

MAE and R²

MAE is the average miss in real units, like rupees. R² is the share of variation explained.

Regularised versions

Ridge and Lasso shrink weights to reduce overfitting. Lasso can push useless features to zero.

See it in code

Walk through the code

python
1import pandas as pd2from sklearn.model_selection import train_test_split3from sklearn.linear_model import LinearRegression4from sklearn.metrics import mean_absolute_error, r2_score5 6df = pd.read_csv("electricity.csv")   # ac_hours, people, sqft, bill7X, y = df[["ac_hours", "people", "sqft"]], df["bill"]8X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.2, random_state=0)9 10model = LinearRegression().fit(X_tr, y_tr)11print(dict(zip(X.columns, model.coef_.round(1))), round(model.intercept_, 1))12 13pred = model.predict(X_te)14print(mean_absolute_error(y_te, pred), r2_score(y_te, pred))

1/3Pick three features and the target, then hold out 20% for testing.

Fit, read the weights, then judge on unseen months. MAE in rupees is easy to explain to anyone.

Save yourself the pain

Common mistakes

Oops

Comparing raw coefficients of features on different scales.

Do this instead

Standardise features first if you want to compare which one matters more.

Oops

Using linear regression on a clearly curved relationship as-is.

Do this instead

Plot residuals. If they curve, add features like squares or logs, or try a tree model.

Your mission

Predict a real number

~30 min

Quick check

A coefficient for 'people' is 350. What does it mean?

Go deeper

  1. 1Implement linear regression from scratch using gradient descent.
  2. 2Then fit the same problem with scikit-learn's LinearRegression to compare.
  3. 3Learn to interpret coefficients and R² for a fitted model.

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