Skip to content

Lesson 1 of 6

What is a machine learning model?

You load a tiny dataset of study hours and exam scores into Python lists and define a model as a line with slope and intercept that predicts score from hours.

6 minMachine Learning

After this lesson you will be able to load a small dataset into Python lists and define a simple machine learning model as a line that predicts one value from another.

A tiny dataset

You will work with a dataset of study hours and exam scores for five students. Each student has one input value, the number of hours they studied, and one output value, the exam score they got. In machine learning, the input is often called a feature and the output is called a target.

Create a new file called data.py and put the dataset in it as two Python lists. The first list holds the study hours and the second holds the exam scores, in the same order.

# data.py
hours = [1, 2, 3, 4, 5]
scores = [50, 55, 65, 70, 80]

Run the file to check that it has no errors. It will not print anything, but it should finish without a message.

What is a model?

A model is a rule that uses an input to make a prediction. For this dataset, you want a rule that takes the number of hours studied and gives back a predicted exam score.

The simplest useful rule is a straight line. A line has two numbers that define it: a slope and an intercept. The slope says how much the predicted score changes when the study hours increase by one. The intercept says what the predicted score is when the study hours are zero.

In Python, you can write a function that represents a line. The function takes the input value and returns the prediction. The slope and intercept are given as arguments so you can try different lines.

Create a file called model.py with this function:

# model.py
def predict(hours, slope, intercept):
    return slope * hours + intercept

This is your first machine learning model. It is very simple, but it has the same parts as larger models: it takes an input, uses some numbers that can be adjusted, and gives a prediction.

Make a prediction

Now use the model to predict a score for a student who studied for 3 hours. You need to choose a slope and an intercept. For now, pick a slope of 10 and an intercept of 20. That means the model predicts a score of 20 plus 10 times the number of hours.

Open a Python shell in the same folder as your files and run these lines:

from model import predict
prediction = predict(3, 10, 20)
print(prediction)

The shell prints 50. The model says a student who studies for 3 hours will get a score of 50.

You can make predictions for any number of hours. Try a few different values to see how the line behaves.

Try different slopes and intercepts

The slope and intercept control the line. A larger slope makes the prediction go up faster as hours increase. A larger intercept makes the prediction higher for every input.

Use the same function with different numbers. For example, a slope of 5 and an intercept of 40 gives a flatter line that starts higher:

print(predict(3, 5, 40))

This prints 55. The same 3 hours now lead to a prediction of 55 instead of 50.

You can also use the model on the whole dataset. Loop over the hours list and print each prediction with the slope 10 and intercept 20:

from data import hours
for h in hours:
    print(h, predict(h, 10, 20))

You will see five pairs of numbers. The first number is the study hours and the second is the predicted score. Compare the predictions with the actual scores in data.py. The model is not perfect, but that is expected. In the next lesson you will measure how far off it is.

The model as a line

A line is fully described by its slope and intercept. In machine learning, these numbers are called the model parameters. The goal of fitting a model is to find parameter values that make the predictions as close as possible to the real data.

For now, you can choose any slope and intercept by hand. Later you will learn a method to find good values automatically.

Your two files, data.py and model.py, are the start of a small machine learning project. Keep them for the next lessons, where you will build on them.

Key takeaways

  • A machine learning model is a rule that takes an input and returns a prediction.
  • A line is a simple model with two parameters: slope and intercept.
  • The slope controls how much the prediction changes when the input changes by one.
  • The intercept is the prediction when the input is zero.
  • You can try different parameter values by hand, but later you will learn to find good values automatically.

Try it

Write a new Python script called try_model.py that imports the predict function and the hours list. In the script, choose your own slope and intercept values. Use a loop to print the predicted score for each number of hours in the dataset. Then run the script. Check that the output shows five predictions, one for each study hour. Change the slope to a larger number and run the script again. The predictions should all increase, and the difference between predictions for consecutive hours should be larger than before. A good result is a script that runs without errors and clearly shows how the predictions change when you change the 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.