A Model That Guesses

A machine learning model is a rule a program learns from examples instead of one you write, and scikit-learn's LinearRegression learns one from a lemonade stand's summer with fit, then guesses the cups a day will sell with predict.

Apprentice · Python · AI Basics · Updated

By

After this lesson you can

  • Name a model's features and its label, the columns it looks at and the column it learns to guess
  • Train a LinearRegression model with fit, and use predict to guess the label for new examples

Key takeaways

  • A model is a rule learned from examples instead of written by hand. It makes guesses, not promises.
  • Features are the columns a model looks at, such as the temperature, and the label is the column it learns to guess, such as the cups sold.
  • scikit-learn takes the features as a table, days[["temp_f"]] with two pairs of brackets, and the label as one column, days["cups_sold"].
  • fit learns from the examples, and predict uses what was learned to guess the label for new examples.
  • A straight-line model learns a slope (cups per degree) and a starting point, and its guess is the starting point plus the slope times the temperature.

A grocery store orders tomorrow's bread, a delivery app says your pizza arrives in 25 minutes, a bank flags a card payment as odd. Nobody typed a rule for those numbers: each is a guess a program learned from past examples. That's machine learning, and in Python it starts with a few lines of scikit-learn, a widely used package for it.

The example is the Sunny Days Lemonade Stand: one summer of days, each with the afternoon temperature and the cups of lemonade sold.

You already guess from examples

Every morning Jada, who runs the stand, decides how many lemons to squeeze. Too few, and she's sold out by two o'clock; too many, and the rest go in the trash. The forecast says 90°F, and last summer's hot days were busy, so she squeezes more. That's a guess from examples.

Written down as a rule, it might be "over 85°F, make 100 cups; otherwise 60." It breaks quickly: an 84°F day and an 86°F day get very different answers, and the 85 and the 100 came from her memory, not her sales.

Jada wrote down all 92 days of the summer, though, and a program can work the rule out from them: how many more cups each extra degree tends to sell. A rule learned from examples is a model, and having a program learn it is machine learning. A model guesses; it never knows.

Each day is an example. The columns a model looks at are its features: here there's one, the temperature. The column it learns to guess is the label: the cups sold. scikit-learn's documentation calls the label the target; a statistics class calls it the dependent variable.

The stand's summer in a table

The days are in lemonade_days.csv, our own made-up numbers. A short script made them with a pattern built in (2 cups for each degree, minus 80, plus a random wobble), so you can see whether a model finds it; with real data, the pattern is what you don't know yet. The file comes with Exercise: Lemonade Stand Forecast, so you can run this lesson's code in that folder.

scikit-learn works with tables from pandas, a package that reads a CSV file into a table called a DataFrame:

import pandas as pd

days = pd.read_csv("lemonade_days.csv")
print(days.head())
   day  temp_f  cups_sold
0    1    70.1         52
1    2    85.8         87
2    3    79.9         78
3    4    76.6         67
4    5    76.1         77

head() shows the first five rows. The feature is temp_f and the label is cups_sold. The day number isn't a feature: it says which day it was, and the pattern Jada cares about is the weather.

fit: learning from the examples

By habit, the features are named X (a capital, because it's a table) and the label y. days[["temp_f"]], with two pairs of brackets, is a table with one column: scikit-learn wants the features as a table even when there's only one, because most models look at many columns. The label, days["cups_sold"], is a single column, with one pair. Give fit the temperature with one pair and it stops with a ValueError asking for "a 2-dimensional container".

import pandas as pd
from sklearn.linear_model import LinearRegression

days = pd.read_csv("lemonade_days.csv")
X = days[["temp_f"]]
y = days["cups_sold"]

model = LinearRegression()
model.fit(X, y)
print("Cups per degree:", round(model.coef_[0], 2))
print("Starting point:", round(model.intercept_, 2))
Cups per degree: 1.89
Starting point: -70.52

LinearRegression() makes a model that hasn't learned anything yet, and fit(X, y) trains it: it finds the straight line that runs closest to all 92 days, the one with the smallest total of squared misses (a textbook's ordinary least squares). coef_ (read "coef underscore", short for coefficient) holds one slope per feature, so [0] is the temperature's: about 1.89 more cups for each degree warmer. intercept_ is the starting point, where the line would be at 0°F. The underscore at the end marks a value the model learned in fit.

We planted 2 cups a degree, minus 80; from the wobbly days, the model found 1.89 and −70.52. Close, not exact, because the wobble hides the true rule, as it does in every real dataset.

The stand's 92 days as dots on a chart, temperature across the bottom and cups sold up the side, rising from the lower left to the upper right, with the model's straight line running through the middle of them; at 90°F the line reads about 99 cups

predict: guessing a day it hasn't seen

Tomorrow's forecast goes in a table with the same column name the model learned from, and predict guesses the label for each row:

import pandas as pd
from sklearn.linear_model import LinearRegression

days = pd.read_csv("lemonade_days.csv")
model = LinearRegression()
model.fit(days[["temp_f"]], days["cups_sold"])

forecast = pd.DataFrame({"temp_f": [75, 90]})
print(model.predict(forecast))
print(model.intercept_ + model.coef_[0] * 90)
[70.87062445 99.14912148]
99.14912148499397

The guesses come back as an array, NumPy's list of numbers: about 71 cups at 75°F and 99 at 90°F. The last line works out the 90°F guess by hand, the starting point plus the slope times the temperature, and gets the same number: that's all a straight-line model's predict does. The column's name matters: call it temp instead of temp_f and predict stops with a ValueError saying the feature names should match.

So Jada squeezes for about 99 cups. How far off might that be? A guess with no idea of its error is a risky one, and How to Tell When a Model Is Wrong finds out.

Check yourself

2 questions to try on the lesson page.

So what

A model is a rule learned from examples: you choose the features and the label, fit learns the rule, and predict turns it into guesses you can plan with, as long as you remember they're guesses.

Sources

Key terms

Related