Predicting from one variable is good. Predicting from two or more is how real-world ML actually works.
Save
Complete lesson & earn 250 PX
EXERCISE
1Predicting CO2 emissions from engine size alone is okay — but adding car weight makes the prediction dramatically better.
Save
EXERCISE
2The coefficient tells you exactly how much each variable pushes the prediction up or down — it is the "weight" of each input.
Save
EXERCISE
3From loading a CSV to making predictions — here is how multiple regression works end-to-end in a real project.
Save
Single-variable is like judging a restaurant by its menu alone. Multiple regression also considers reviews, location, and price — the more relevant information you include, the better your .
Single vs Multiple regression:
| Type | Variables | Example |
|---|---|---|
| Simple | 1 independent → 1 dependent | Engine size → CO2 |
| Multiple | 2+ independent → 1 dependent | Engine size + Weight → CO2 |
import pandas as pd
from sklearn import linear_model
df = pd.read_csv("cars.csv")
X = df[["Weight", "Volume"]]
y = df["CO2"]
regr = linear_model.LinearRegression()
regr.fit(X, y)
# Predict CO2 for a car: weight=2300kg, volume=1300cm³
predicted = regr.predict([[2300, 1300]])
print(f"Predicted CO2: {round(predicted[0], 2)} g/km")
# Output: Predicted CO2: 107.21 g/km
> 💡 Key Insight: In the real world, almost nothing is caused by a single factor. House prices depend on size AND location AND age AND condition. Multiple regression lets you model this reality. The jump from single to multiple regression is the jump from toy examples to real ML.
Think of coefficients as volume knobs on a mixing board. Each knob controls how much one input (guitar, vocals, drums) contributes to the final sound (CO2 prediction). Turn up weight → CO2 rises. Turn up volume → CO2 rises.
Reading the coefficients:
import pandas as pd
from sklearn import linear_model
df = pd.read_csv("cars.csv")
X = df[["Weight", "Volume"]]
y = df["CO2"]
regr = linear_model.LinearRegression()
regr.fit(X, y)
print(f"Coefficients: {regr.coef_}")
# Output: Coefficients: [0.00755095 0.00780526]
print(f"Intercept: {round(regr.intercept_, 4)}")
# Output: Intercept: 79.6947
What the numbers mean:
| Variable | Coefficient | Meaning |
|---|---|---|
| Weight | 0.00755 | +1 kg → CO2 goes up by 0.00755 g/km |
| Volume | 0.00781 | +1 cm³ → CO2 goes up by 0.00781 g/km |
Verifying with a prediction:
import pandas as pd
from sklearn import linear_model
df = pd.read_csv("cars.csv")
X = df[["Weight", "Volume"]]
y = df["CO2"]
regr = linear_model.LinearRegression()
regr.fit(X, y)
# Original: weight=2300, volume=1300 → 107.21
print(round(regr.predict([[2300, 1300]])[0], 2))
# Output: 107.21
# Add 1000kg: weight=3300, volume=1300
print(round(regr.predict([[3300, 1300]])[0], 2))
# Output: 114.76
# Difference: 114.76 - 107.21 = 7.55 = 1000 × 0.00755 ✅
> 💡 Key Insight: Coefficients only tell a fair story when variables are on similar scales. If weight is in kg (range 800–1800) and volume is in cm³ (range 900–2500), the raw coefficients are not directly comparable. To compare importance, you need to scale your first — which is exactly what the next lesson covers.
In production, the data does not come in neat Python lists — it comes in CSV files, databases, and APIs. Here is the real-world workflow using Pandas and scikit-learn.
Step-by-step:
import pandas as pd
from sklearn import linear_model
# Step 1: Load data
df = pd.read_csv("cars.csv")
print(df.head(3))
# Output:
# Car Model Volume Weight CO2
# 0 Toyota Aygo 1000 790 99
# 1 Mitsubishi Space 1200 1160 95
# 2 Skoda Citigo 1000 929 95
# Step 2: Separate features (X) and target (y)
X = df[["Weight", "Volume"]]
y = df["CO2"]
print(f"Features shape: {X.shape}")
# Output: Features shape: (36, 2)
# Step 3: Create and train the model
regr = linear_model.LinearRegression()
regr.fit(X, y)
# Step 4: Inspect the model
print(f"Coefficients: {[round(c, 5) for c in regr.coef_]}")
# Output: Coefficients: [0.00755, 0.00781]
print(f"Intercept: {round(regr.intercept_, 2)}")
# Output: Intercept: 79.69
# Step 5: Predict
new_car = [[2300, 1300]]
print(f"Predicted CO2: {round(regr.predict(new_car)[0], 2)} g/km")
# Output: Predicted CO2: 107.21 g/km
The naming convention:
| Variable | Convention | Why |
|---|---|---|
| Features | Capital X | It is a matrix (rows × columns) |
| Target | Lowercase y | It is a single vector |
> 💡 Key Insight: regr.fit(X, y) is the most important line. It trains the model — meaning it calculates the coefficients and intercept that minimise prediction error across all 36 cars. After .fit(), the model is ready to predict. Before .fit(), it knows nothing.
Multiple regression uses two or more independent variables to predict an outcome. Scikit-learn's LinearRegression handles the math.