Skip to content

Lesson 6 of 6

Put it together: a small prediction tool

You combine the previous steps into one Python script that takes hours studied as input, predicts a score, and reports the test error.

6 minMachine Learning

After this lesson, you will be able to build a small Python tool that fits a line to your study data, asks for a number of study hours, predicts an exam score, and reports the error on unseen data.

Put the pieces together

You have written several scripts in the earlier lessons. Each one does one job: loading data, splitting it, fitting a line, and measuring error. Now you will combine them into one script that a user can run from the command line.

Create a new file called predict_tool.py. Start by importing the functions you already have:

from data import hours, scores
from split_data import split_data
from least_squares import least_squares
from mse import mean_squared_error

The script will do these steps in order: split the data, fit a line on the training part, compute the test error, ask the user for a number of hours, and print the predicted score and the test error.

Split and fit

Use a test size of 0.4, as in the earlier lessons. This means 40 percent of the data is held out for testing, and the rest is used to fit the line.

train_hours, train_scores, test_hours, test_scores = split_data(hours, scores, 0.4)
slope, intercept = least_squares(train_hours, train_scores)

The split_data function shuffles the data randomly, so each run will give a slightly different split. That is fine for a small tool.

Compute the test error

Now compute the mean squared error on the test set using the line fitted on the training set. This is the error you should report to the user, because it shows how well the model works on data it has not seen.

mse_test = mean_squared_error(test_hours, test_scores, slope, intercept)

You can also compute the root mean squared error, which is in the same units as the scores and is easier to interpret. Import the math module at the top of the file, then compute:

import math
rmse_test = math.sqrt(mse_test)

Ask for input and predict

The script should ask the user for a number of study hours. Use the input function, which returns a string. Convert it to a float so you can use it in the prediction.

hours_input = float(input('Enter hours studied: '))
predicted_score = slope * hours_input + intercept

The prediction is just the line equation: slope times hours plus intercept. You could also use the predict function from model.py, but writing the formula directly is clear here.

Print the result

Finally, print the predicted score and the test error. Round the numbers to two decimal places so the output is easy to read.

print(f'Predicted score for {hours_input} hours: {predicted_score:.2f}')
print(f'Test RMSE: {rmse_test:.2f}')

The full script looks like this:

# predict_tool.py
import math
from data import hours, scores
from split_data import split_data
from least_squares import least_squares
from mse import mean_squared_error

train_hours, train_scores, test_hours, test_scores = split_data(hours, scores, 0.4)
slope, intercept = least_squares(train_hours, train_scores)
mse_test = mean_squared_error(test_hours, test_scores, slope, intercept)
rmse_test = math.sqrt(mse_test)

hours_input = float(input('Enter hours studied: '))
predicted_score = slope * hours_input + intercept

print(f'Predicted score for {hours_input} hours: {predicted_score:.2f}')
print(f'Test RMSE: {rmse_test:.2f}')

Run the script from the command line. It will ask for hours studied. Type a number, such as 3.5, and press Enter. The script prints the predicted score and the test RMSE. The numbers will change each run because the split is random.

Why this works

The script uses the same steps you have learned: split the data fairly, fit the line only on training data, and evaluate on test data. The RMSE tells you the typical size of the prediction error on unseen data. For example, an RMSE of 10 means the predictions are typically off by about 10 points. The predicted score is the best guess from the line, but the RMSE reminds you that the guess is not exact.

This small tool is a complete machine learning workflow: load data, split, fit, evaluate, and predict. You can reuse this pattern for other datasets and other models.

Key takeaways

  • A prediction tool combines data loading, splitting, fitting, evaluation, and user input in one script.
  • Always fit the model on training data and report the error on test data.
  • RMSE is in the same units as the target, so it is easier to interpret than MSE.
  • The predicted value is a point estimate; the RMSE shows the typical error around it.

Try it

Write a new script called predict_tool_extended.py. Copy the code from predict_tool.py, but change the test size to 0.3. After printing the predicted score and test RMSE, also print the slope and intercept of the fitted line. Run the script three times with the same input, such as 4 hours. Check that the slope and intercept change slightly between runs because of the random split, and that the test RMSE is a positive number. A good result is a script that runs without errors and clearly shows the prediction, test RMSE, slope, and intercept for each run.

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.