Lesson 23 · Probability and Statistics in python
Simple Linear Regression in Python: Model Fitting & Interpretation Guide
In this lesson, you will learn how to fit a straight line to data, predict values, and interpret results. We will use the classic 'mpg' dataset, which shows…
- CourseProbability and Statistics in python
- Lesson23 of 35
- Video12 min
- FormatJupyter notebook · 12 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbWelcome to Simple Linear Regression in Python#
In this lesson, you will learn how to fit a straight line to data, predict values, and interpret results.
We will use the classic 'mpg' dataset, which shows car gas mileage and engine size.
Let us get started!
# Data setup
import warnings; warnings.filterwarnings("ignore")
import pandas as pd
import seaborn as sns
df = sns.load_dataset("mpg")
print("Dataset shape:", df.shape)
df.head()
What is Linear Regression?#
Linear regression finds the best straight line that fits the data.
It helps us predict values. For example: How does car weight affect miles per gallon?
# Pick two columns: weight and mpg
df = df[["weight", "mpg"]].dropna()
print("Clean shape:", df.shape)
df.head()
Visualizing the Relationship#
Let us look at how car weight and mpg are related on a scatter plot.
import matplotlib.pyplot as plt
plt.figure(figsize=(8,5))
plt.scatter(df["weight"], df["mpg"], alpha=0.7)
plt.xlabel("Car Weight")
plt.ylabel("Miles Per Gallon (mpg)")
plt.title("Car Weight vs. MPG")
plt.show()
The Linear Model#
We want to fit a line: mpg = slope * weight + intercept.
This line will show the average relationship between car weight and gas mileage.
# Fit the linear model
from sklearn.linear_model import LinearRegression
X = df[["weight"]]
y = df["mpg"]
model = LinearRegression()
model.fit(X, y)
slope = model.coef_[0]
intercept = model.intercept_
print("Slope:", slope)
print("Intercept:", intercept)
# Draw the regression line on the scatter plot
plt.figure(figsize=(8,5))
plt.scatter(df["weight"], df["mpg"], alpha=0.5)
plt.plot(df["weight"], model.predict(X), color="red", label="Regression Line")
plt.xlabel("Car Weight")
plt.ylabel("MPG")
plt.title("Regression Line: Weight vs MPG")
plt.legend()
plt.show()
What do Slope and Intercept Mean?#
- The slope shows change in mpg for every extra pound of car weight.
- The intercept is the predicted mpg if weight is zero.
If the slope is negative, more weight means worse mileage.
# Predict mpg for a new car weight
test_weight = 2500
pred_mpg = model.predict([[test_weight]])[0]
print("Predicted mpg for 2500 lbs:", pred_mpg)
# Try your own value!
your_weight = float(input("Enter a car weight in pounds (for example, 3400): "))
your_pred = model.predict([[your_weight]])[0]
print("Predicted mpg:", your_pred)
# How well does our model fit?
r2 = model.score(X, y)
print("R-squared:", r2)
Interpreting the Results#
A high R-squared means car weight explains much of the difference in mpg.
But remember:
- Linear regression assumes a straight line fits well.
- Other factors can affect mpg too (like horsepower, engine type).
# Check for linearity with a residual plot
import numpy as np
residuals = y - model.predict(X)
plt.scatter(df["weight"], residuals, alpha=0.6)
plt.axhline(0, color="red", linestyle="--")
plt.xlabel("Car Weight")
plt.ylabel("Residual (Actual - Predicted)")
plt.title("Residual Plot")
plt.show()
# Try adding another variable: horsepower
X2 = df[["weight", "mpg"]].dropna()
if "horsepower" in sns.load_dataset("mpg").columns:
df2_full = sns.load_dataset("mpg")
df2 = df2_full[["weight", "horsepower", "mpg"]].dropna()
X2 = df2[["weight", "horsepower"]]
y2 = df2["mpg"]
model2 = LinearRegression()
model2.fit(X2, y2)
r2_2 = model2.score(X2, y2)
print("Linear regression with weight and horsepower. R-squared:", r2_2)
else:
print("No horsepower column in dataset.")
Best Practices with Linear Regression#
- Always check for missing data and fix before analysis.
- Look at scatter and residual plots for patterns.
- Try different variables to see which combinations are best.
- Remember that a straight line is just a simple model, not always perfect.
# Troubleshooting
print("Check your data for missing or weird values.")
if df.isnull().sum().sum() > 0:
print("Warning: Missing values found.")
if df.duplicated().sum() > 0:
print("Warning: Duplicate rows found.")
# Challenge! Predict mpg for three new car weights.
for i in range(3):
w = float(input(f"Test weight {i+1}: "))
print(f"Predicted mpg for {w} lbs: {model.predict([[w]])[0]:.2f}")
Recap#
- You learned how to fit a line to car data and predict miles per gallon.
- You saw what the slope and intercept mean.
- You tried making your own predictions.
- You checked model quality with R-squared.
Practice with your own variables and see what changes!
Thanks for Learning! Subscribe for More#
If you found this helpful, like the video and subscribe for more Python and statistics lessons.
Happy coding!
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



