Skip to content

Lesson 2 of 6

Measure how wrong a model is

You write a Python function that computes mean squared error (MSE) for any slope and intercept, and use it to compare a few hand-picked lines against the data.

4 minMachine Learning

After this lesson, you will be able to write a Python function that measures how wrong a line is for a dataset, and use that measure to compare different lines.

What is error?

A model makes predictions. The difference between a prediction and the true value is the error for that point. For one student, if the model predicts a score of 60 and the true score is 65, the error is 5.

You have a dataset of study hours and exam scores in data.py from lesson 1. You also have a predict function in model.py. To measure how good a line is, you need to look at the errors for all points, not just one.

Mean squared error

A common way to combine errors into one number is mean squared error, or MSE. For each data point, you take the error, square it, and then take the average of those squared errors.

Squaring does two things. It makes all errors positive, so a negative error does not cancel a positive one. It also makes large errors count more than small ones. A model with one big mistake is worse than a model with many small mistakes.

Here is a function that computes MSE for a given slope and intercept:

# mse.py
def mean_squared_error(hours, scores, slope, intercept):
    total = 0
    for i in range(len(hours)):
        predicted = slope * hours[i] + intercept
        error = scores[i] - predicted
        total += error ** 2
    return total / len(hours)

The function loops over the data. For each point, it predicts the score using the slope and intercept, finds the error, squares it, and adds to a total. At the end, it divides by the number of points to get the mean.

You can use this function from another file. Create a new file called compare_lines.py:

# compare_lines.py
from data import hours, scores
from mse import mean_squared_error

mse1 = mean_squared_error(hours, scores, 10, 20)
print('MSE for slope 10, intercept 20:', mse1)

mse2 = mean_squared_error(hours, scores, 5, 40)
print('MSE for slope 5, intercept 40:', mse2)

Run this script from the command line. The output shows the MSE for two different lines. A lower MSE means the line is closer to the data. You can compare the numbers to see which line is better.

Why square the errors?

You might wonder why not just take the average of the errors without squaring. If you do that, positive and negative errors can cancel. For example, if one error is 10 and another is -10, the average is 0, which suggests the model is perfect when it is not.

Squaring also changes the scale. The MSE is in squared units. If scores are in points, MSE is in points squared. That is fine for comparing models, but it is not directly interpretable as an error in points. Later you may see root mean squared error, which is the square root of MSE and is in the original units.

Comparing lines

You can use the MSE function to compare any number of lines. For example, you might try a slope of 8 with intercept 30. Add this to compare_lines.py:

mse3 = mean_squared_error(hours, scores, 8, 30)
print('MSE for slope 8, intercept 30:', mse3)

Run the script again. Now you have three MSE values. The line with the lowest MSE is the best among the ones you tried. In the next lesson, you will learn how to find the line with the lowest possible MSE without trying many lines by hand.

Summary

You now have a way to measure how wrong a model is. The MSE function takes a slope and intercept, and returns a single number that summarises the errors. You can use it to compare different lines and pick the better one.

Key takeaways

  • Error is the difference between a prediction and the true value.
  • Mean squared error (MSE) squares each error, then averages them.
  • Squaring makes errors positive and penalises large errors more.
  • Lower MSE means the line fits the data better.
  • You can compare different lines by computing their MSE on the same data.

Try it

Write a new Python script called my_mse.py. In it, import the hours and scores lists from data.py and the mean_squared_error function from mse.py. Choose three different pairs of slope and intercept values. For each pair, compute the MSE and print the slope, intercept, and MSE on one line. Run the script. Check that the output shows three lines, each with a slope, intercept, and MSE. Then, change one of the slopes to a value much larger than the others and run again. The MSE for that line should be much larger than before. A good result is a script that runs without errors and clearly shows the MSE for each line, with the largest MSE for the line with the extreme slope.

Official documentation

Ready to use this in a job?

Rolebound applies to matching roles for you.

Start now

Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.

    Cookies on Rolebound

    We use necessary cookies to sign you in and keep the site working — those cannot be turned off. We would also like to set analytics and advertising cookies so we can see which pages help people get interviews and which adverts brought them here. Nothing optional is set unless you say yes, and you can change your mind at any time from “Cookie settings” in the footer. Read our Privacy Policy.

    Measure how wrong a model is | Machine Learning Foundations: Fit a Line to Data and Test It | Rolebound