Mathew K Analytics

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…

⬇ Download notebookOpen in Colab ↗

What you'll learn

Data

No separate download needed — the notebook creates or downloads everything it uses.

📓 Full notebook

Download .ipynb

Welcome 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()
Dataset shape: (398, 9)
mpg cylinders displacement horsepower weight acceleration model_year origin name
0 18.0 8 307.0 130.0 3504 12.0 70 usa chevrolet chevelle malibu
1 15.0 8 350.0 165.0 3693 11.5 70 usa buick skylark 320
2 18.0 8 318.0 150.0 3436 11.0 70 usa plymouth satellite
3 16.0 8 304.0 150.0 3433 12.0 70 usa amc rebel sst
4 17.0 8 302.0 140.0 3449 10.5 70 usa ford torino

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()
Clean shape: (398, 2)
weight mpg
0 3504 18.0
1 3693 15.0
2 3436 18.0
3 3433 16.0
4 3449 17.0

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()
No description has been provided for this image

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)
Slope: -0.007676610063926465
Intercept: 46.31736442026563
# 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()
No description has been provided for this image

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)
Predicted mpg for 2500 lbs: 27.12583926044947
# 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)
Predicted mpg: 21.752212215700943
# How well does our model fit?
r2 = model.score(X, y)
print("R-squared:", r2)
R-squared: 0.6917929800341573

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()
No description has been provided for this image
# 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.")
    
Linear regression with weight and horsepower. R-squared: 0.7063752737298348

Best Practices with Linear Regression#

  1. Always check for missing data and fix before analysis.
  2. Look at scatter and residual plots for patterns.
  3. Try different variables to see which combinations are best.
  4. 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.")
    
Check your data for missing or weird values.
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}")
    
Predicted mpg for 2000.0 lbs: 30.96
Predicted mpg for 3000.0 lbs: 23.29
Predicted mpg for 4000.0 lbs: 15.61

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.