Codiqo

Understanding AI · lesson 10 of 60 · about 6 minutes

Lines Through Points

A straight line can sum up a pattern in just two numbers. See how the simplest machine learning works.

What you'll learn

  • Describe a line with a slope and a starting value
  • Work out a line's prediction for a given input
  • Explain why a line is a model

Two numbers sum it up

In the ice cream data, every 5 degrees warmer meant 20 more sales: 4 more per degree. We can write that as a line:

sales = 4 x temperature - 30

The 4 is the slope (how fast it rises) and the -30 is the starting value. Two numbers, and the whole pattern is captured.

A model

This line is a model: a simple summary of the data that can make predictions. Training means finding the best numbers for the model. Here, a computer would try slopes and starting values until the line fits the points as closely as possible.

The line as a Python function. `def` makes a function; `return` gives back its answer.

def predict_sales(temperature):
    return 4 * temperature - 30

print(predict_sales(20))
print(predict_sales(30))

Output

50
90

Tip

Check it against the real data: at 20 degrees they sold 50, at 30 degrees they sold 90. The line fits.

Now try it yourself

This lesson is free in Codiqo, with 6 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.