Interactive Python · Machine learning

Multiple Linear Regression, the game

Predict with many features at once. Build your own model from real car data, see why adjusted R² matters, learn feature selection and code it in Python.

~15 minIntermediatePython · sklearn · statsmodels6 missions350 XP
Mission 1+25 XP✓ Complete

More than one feature

Multiple linear regression predicts one dependent variable from several independent variables at once.

y = β₀ + β₁x₁ + β₂x₂ + … + βₙxₙ + εEach βᵢ is the effect of xᵢ while all the other features stay the same

The key phrase is “holding the other features constant”. Each coefficient tells you what happens when only that one feature changes.

🧠Quick check
+25 XP

A fish model is weight = −500 + 30·length + 20·height. If length grows by 1 cm and height stays the same, predicted weight…

Mission 2+80 XP✓ Complete

Build your model

Every feature you add makes R² (the share of variance explained) go up or stay the same, even if the feature is useless. Adjusted R² fixes that by penalizing extra features:

Adjusted R² = 1 − (1 − R²) · (n − 1) / (n − k − 1)n = rows, k = number of features

Below are the real values for every possible model predicting a car’s fuel efficiency (mpg) on R’s built-in mtcars data (32 cars, 10 features). The original article uses a fish-weight dataset; the idea is identical.

🏗️Model builder
+80 XP

Tap features to add them to mpg ~ …

–features used
–R²
–Adjusted R²
R²
Adjusted R²
  • ✓Find the best single feature: try wt (weight) alone+20 XP
  • ✓Switch on all 10 features and compare R² with adjusted R²+20 XP
  • ✓Reach adjusted R² ≥ 0.83 (hint: 3 features can do it)+40 XP
Mission 3+40 XP✓ Complete

Pick features like a pro

With hundreds of features you need a strategy:

  • Backward elimination: start with every feature and repeatedly remove the least significant one (highest p-value above 0.05). Like an entrance exam where the weakest candidates leave.
  • Forward selection: start with no features and add the most useful one at a time. Like picking a cricket team one player at a time.
  • Stepwise selection: add features like forward selection, but re-check after each step and remove any that are no longer significant.
⚡Which method?
1 / 4
correct on the first try
Mission 4+30 XP✓ Complete

Code it in Python

The article’s workflow on the Kaggle Fish Market dataset (159 fish, 7 columns), with two fixes: species is one-hot encoded (category codes would wrongly imply an order), and the metric functions are imported.

Step 1Load and check the data

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score, mean_squared_error

data = pd.read_csv("Fish.csv")
print(data.shape, data.isnull().sum().sum())   # (159, 7) and no missing values

Step 2Encode, then split

# One-hot encode the species column (no fake ordering)
data = pd.get_dummies(data, columns=["Species"], drop_first=True)

X = data.drop(columns="Weight")
y = data["Weight"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

Step 3Fit and evaluate

model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

print("R²:", r2_score(y_test, y_pred))
print("RMSE:", np.sqrt(mean_squared_error(y_test, y_pred)))
🧠Quick check
+30 XP

Why one-hot encode Species instead of turning it into codes 0–6?

Mission 5+70 XP✓ Complete

Strength or weakness?

Sort each statement about multiple linear regression.

⚡Sort the cards
1 / 7
correct on the first try
Final boss+105 XP✓ Defeated

Defeat the Model Boss 🐲

Seven questions, three hearts.

Question 1 / 7
❤️❤️❤️

💥 The boss winsReview the missions above, then try again.

You’re a Model Builder!

Share on LinkedIn
What’s next

Keep levelling up

You learned the multiple regression equation, how to read its coefficients, why R² always rises with more features while adjusted R² doesn’t, three feature selection methods, and the full Python workflow.

Lv 1 Rookie0 / 350 XP