Focused practice · 10 min read

Python Linear Regression Exercises: Fit, Predict, Evaluate

These exercises keep linear regression small enough to inspect by hand. You will fit a line, measure its error against a baseline, and decide when a prediction is outside the evidence.

Exercise: turn a line into a prediction

Write the prediction rule first. The slope controls change per input unit, and the intercept sets the starting height. Test one value before fitting anything.

def predict(x, slope, intercept):
    return slope * x + intercept

print(predict(4, 1.5, 2.0))

A rule you can test

Exercise: fit with centered sums

Center x and y around their means, multiply the paired deviations, and divide by the squared deviations of x. This is the least-squares slope for one input.

xs = [1, 2, 3]
ys = [3, 5, 7]
mean_x = sum(xs) / len(xs)
mean_y = sum(ys) / len(ys)
slope = sum((x - mean_x) * (y - mean_y) for x, y in zip(xs, ys)) / sum((x - mean_x) ** 2 for x in xs)
intercept = mean_y - slope * mean_x
print(slope, intercept)

Fitted by hand

Exercise: compare error with a baseline

A model should beat a simple baseline. Predict the training mean for every input, compute its MSE, then compare the fitted line's MSE. If the line is not better, the extra complexity is not earning its place.

xs = [1, 2, 3]
ys = [2, 4, 5]
mean_y = sum(ys) / len(ys)
baseline_mse = sum((y - mean_y) ** 2 for y in ys) / len(ys)
slope = 1.5
intercept = 2 / 3
model_mse = sum((y - (slope * x + intercept)) ** 2 for x, y in zip(xs, ys)) / len(xs)
print(round(baseline_mse, 2), round(model_mse, 2))

Beat the mean

Exercise: keep prediction inside the evidence

A guard is part of the model's interface. Return a clear result when the input is outside the training range instead of silently extrapolating.

def safe_predict(x, xs, slope, intercept):
    if not min(xs) <= x <= max(xs):
        return None
    return slope * x + intercept

print(safe_predict(3, [1, 2, 3], 2.0, 0.0))
print(safe_predict(4, [1, 2, 3], 2.0, 0.0))

Outside the range, return nothing

Exercise: report the right unit

MSE is in squared output units. Root mean squared error returns to the original unit and is easier to explain. Compute both so the relationship is visible.

import math

mse = 4.0
print(math.sqrt(mse))

Back to the original unit

Your practice checklist

Common questions

Is linear regression always a straight line?

Simple linear regression with one input is a line. Multiple regression fits a plane or hyperplane, but the same least-squares idea applies.

Why is my slope negative?

The fitted relationship can be negative: as x increases, the predicted y decreases. The sign is part of the result, not an error.

Can I compare MSE between two datasets?

Only when the output is measured in the same unit and evaluated on comparable data. Otherwise compare RMSE or a relative metric.

Practice fitting a line

12 questions on slopes, intercepts, predictions, MSE, baselines, and extrapolation.

Open practice

Keep reading: