Guessing from Examples
A rule you write comes from memory, while a rule learned from past examples comes from the data, and that learned rule is a model; each example is a row, the features are the columns it looks at, and the label is the column it learns to guess.
Novice · AI Basics · Python · Updated
After this lesson you can
- Explain the difference between a rule you write by hand and a rule a model learns from examples
- Point out the examples, the features and the label in a table of past data
Key takeaways
- A rule you write comes from what you remember. A rule learned from examples comes from what really happened, such as the average of every past Saturday.
- Machine learning is a computer finding a rule from examples. The rule it learns is called a model, and learning it is called training.
- Each example is one row. The features are the columns a model looks at, and the label is the column it learns to guess.
- A model's guess is not a promise. It knows only the examples it learned from, not why the world works the way it does.
Every evening at Sunrise Bakery, the owner decides how many loaves of bread to bake for tomorrow. Bake too many and the extras go stale; bake too few and customers leave with nothing. Guessing a number like this from what happened before is what every model in this course does, from a delivery-time estimate to a spam filter, so this lesson is the idea the whole course stands on.
The example for the whole chapter is Sunrise Bakery: 120 days of bread sales in bakery_days.csv, our own made-up data. It comes with Exercise: How Many Loaves?, beside the seed script that made it, so you can run this lesson's code in that folder.
A rule you write
The owner has a rule of thumb: "Saturdays are busy, so bake more." Written as Python, it's a function that gives back a number of loaves for each day of the week:
def owners_rule(weekday):
if weekday == "Saturday":
return 130
if weekday == "Sunday":
return 110
return 85
for weekday in ["Tuesday", "Saturday", "Sunday"]:
print(weekday, owners_rule(weekday))
Tuesday 85
Saturday 130
Sunday 110
A rule like this works, up to a point. But where did 130 come from? From the owner's memory of busy Saturdays, which nobody has checked. And every new situation needs another line: hot days, holidays, the week the street is closed for repairs. When the town changes, someone has to notice and rewrite the numbers.
A rule learned from examples
The bakery has written down every day's sales, so there's a better place to look than memory. The first three Saturdays in the records sold 143, 139 and 138 loaves:
saturdays = [143, 139, 138]
print("Average of the past Saturdays:", sum(saturdays) / len(saturdays))
Average of the past Saturdays: 140.0
The records say 140, not the 130 the owner remembered. Do the same for every day of the week, across all 120 days, and the data hands back a whole rule. This reads the file with Python's csv module, which gives each row as a dictionary:
import csv
with open("bakery_days.csv", newline="") as file:
days = list(csv.DictReader(file))
totals = {}
counts = {}
for day in days:
weekday = day["weekday"]
totals[weekday] = totals.get(weekday, 0) + int(day["loaves_sold"])
counts[weekday] = counts.get(weekday, 0) + 1
for weekday in totals:
print(weekday, round(totals[weekday] / counts[weekday], 1))
Monday 94.8
Tuesday 88.8
Wednesday 85.9
Thursday 89.6
Friday 87.5
Saturday 134.9
Sunday 122.2
Nobody typed those seven numbers in: the past days produced them. That is the whole idea of machine learning: a computer finds a rule from examples, instead of a person writing the rule by hand. The rule it finds is called a model, and finding it from the examples is called training. These weekday averages are a small model, trained on 120 days.
Examples, features and a label
Here are three of the bakery's 120 days, as the file has them:
| day | weekday | high_temp_f | is_holiday | loaves_sold |
|---|---|---|---|---|
| 6 | Saturday | 72 | 0 | 143 |
| 7 | Sunday | 68 | 0 | 117 |
| 8 | Monday | 75 | 1 | 101 |
Each row is one example: one day, with what was known about it and what happened. The columns a model looks at to make its guess are its features: here the weekday, the day's high temperature in °F, and whether it was a holiday (1 for yes, 0 for no). The column it learns to guess is its label: loaves_sold.
A feature has to be something you know when you make the guess. Tomorrow's weekday and the weather forecast are known tonight; tomorrow's loaves are the answer you're guessing, so they can only ever be the label. scikit-learn, the library this course uses from Module 2 on, calls an example a sample and the label the target; Features and Labels goes deeper into choosing features.
What a model is, and what it isn't
A model's guess is a best guess, not a promise. Saturdays averaged 134.9 loaves, but single Saturdays sold anywhere from 123 to 145. The model above also looks at one feature only: it ignores the weather and the holidays, which the file holds too. The models later in this course use several features at once.
And a model learned what happened on these 120 days, nothing more. It doesn't know why Saturdays are busy, and it has nothing to say about a day unlike any in the file, such as the day of the town's street fair. Is it any good? You can't tell yet: Training and Testing: Never Grade Your Own Homework shows how to find out honestly.
Do it: guess from similar past days
The owner wants tomorrow's guess to come from past days that were the same day of the week, the way the weekday averages above do it, but for one weekday at a time.
The inputs:
past_days: the days the bakery remembers, a list of dictionaries, each with a"weekday"and its"loaves_sold".weekday: tomorrow's day of the week, such as"Sunday".
For example, with the bakery's first three Sundays and some other days:
past_days = [
{"weekday": "Monday", "loaves_sold": 74},
{"weekday": "Sunday", "loaves_sold": 117},
{"weekday": "Saturday", "loaves_sold": 143},
{"weekday": "Sunday", "loaves_sold": 131},
{"weekday": "Sunday", "loaves_sold": 140},
]
The Sundays sold 117, 131 and 140: 388 loaves over 3 days, and 388 ÷ 3 = 129.333…, which rounds to 129.3. So similar_day_guess(past_days, "Sunday") returns 129.3.
Your job: write similar_day_guess(past_days, weekday).
- It returns the average
loaves_soldof the past days whose"weekday"isweekday, rounded to 1 decimal place withround(average, 1). - Days with any other weekday don't count.
- If no past day has that weekday, it returns
None.
Practiced live on the lesson page: write the Python right there and check it.
Check yourself
2 questions to try on the lesson page.
So what
A model is a rule learned from past examples instead of written from memory, and knowing which columns are the features and which is the label is the first step of every model you'll build.
Sources
- scikit-learn, Glossary of Common Terms and API Elements: a sample is one row of the data (also called an instance, data point or observation), features are the columns, and the target is what supervised learning learns to predict, also known as the label.
- scikit-learn 1.9.1, Getting Started: scikit-learn's models are called estimators, each is fitted to data with its
fitmethod, and fitting a model to some data doesn't mean it will predict well on data it hasn't seen. - Python documentation, csv: CSV File Reading and Writing, DictReader: each row is read as a dictionary whose keys are the file's first row, the column names.