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.
More than one feature
Multiple linear regression predicts one dependent variable from several independent variables at once.
The key phrase is “holding the other features constant”. Each coefficient tells you what happens when only that one feature changes.
A fish model is weight = −500 + 30·length + 20·height. If length grows by 1 cm and height stays the same, predicted weight…
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:
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.
Tap features to add them to mpg ~ …
- ✓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
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.
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 valuesStep 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)))Why one-hot encode Species instead of turning it into codes 0–6?
Strength or weakness?
Sort each statement about multiple linear regression.
Defeat the Model Boss 🐲
Seven questions, three hearts.
You’re a Model Builder!
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.