Data, features and labels · AI Foundations
How does a program learn from examples instead of rules?
Course contentsWhy you need it
Some problems are hard to write rules for: is this email spam, what will this flat sell for, which photo shows a cat? Machine learning finds the pattern in examples instead. Before any model, though, comes the data, and most real problems with models start as problems with the data: the wrong columns, numbers on very different scales, or a test that secretly peeks at the answers.
The intuition
A dataset is a table. Each row is one example; the columns you know in advance are the features, and the column you want to predict is the label:
| size (m²) | rooms | price (k) |
|---|---|---|
| 50 | 2 | 150 |
| 80 | 3 | 230 |
| 120 | 4 | 330 |
(The numbers in this course are made up for practice.)
Training means finding a function from features to label that fits the examples. But fitting old examples is easy: a lookup table does it perfectly. What matters is how the model does on rows it has never seen. So before training you set some rows aside as the test set and only look at them at the very end.
Features often live on different scales: size goes up to hundreds, rooms only to a handful. Many methods compare numbers directly, so the big column would drown out the small one. Min-max scaling maps each feature to 0…1:
scaled = (value - min) / (max - min)Take min and max from the training rows only, then apply the same numbers to the test rows. Otherwise information about the test set leaks into training.
Definition
Feature
An input column the model uses, such as size or number of rooms.
Label
The column the model learns to predict, such as the price.
Test set
Rows kept aside during training and used only to check the finished model on new data.
Min-max scaling
Mapping a feature to the range 0…1 with (value − min) / (max − min).
Examples
Features and labels
Try it yourselfPull the inputs and the answer out of each row.
houses = [
{"size": 50, "rooms": 2, "price": 150},
{"size": 80, "rooms": 3, "price": 230},
{"size": 120, "rooms": 4, "price": 330},
{"size": 65, "rooms": 2, "price": 190},
]
X = [[h["size"], h["rooms"]] for h in houses]
y = [h["price"] for h in houses]
print(X)
print(y)[[50, 2], [80, 3], [120, 4], [65, 2]] [150, 230, 330, 190]
- L7Features: one list of inputs per house.
- L8Labels: the answer for each house, in the same order.
- L9Row i of X belongs with item i of y.
[[50, 2], [80, 3], [120, 4], [65, 2]]
Hold out a test set
Try it yourselfKeep the last 20% aside. Real code shuffles the rows first.
rows = list(range(1, 11))
cut = int(len(rows) * 0.8)
train, test = rows[:cut], rows[cut:]
print(train)
print(test)[1, 2, 3, 4, 5, 6, 7, 8] [9, 10]
- L280% of 10 rows is 8.
8 - L3Slices: the first 8 for training, the rest for the final check.
Min-max scaling
Try it yourselfEvery size ends up between 0 and 1.
sizes = [50, 80, 120, 65]
lo, hi = min(sizes), max(sizes)
scaled = [(s - lo) / (hi - lo) for s in sizes]
print([round(v, 2) for v in scaled])[0.0, 0.43, 1.0, 0.21]
- L2lo = 50 and hi = 120, so the range is 70.
- L3The smallest becomes 0, the largest 1; 80 is (80 − 50) / 70 ≈ 0.43.
Common mistakes
Testing on the training rows
A model that memorised the training rows scores perfectly on them and tells you nothing. Judge it only on rows it never saw.
Scaling before splitting
If min and max come from all rows, the test rows have influenced training. Split first, then fit the scaling on the training rows.
A feature that gives the answer away
A column such as "refund issued" when predicting "will this order be returned" is only known afterwards. The model looks brilliant in testing and fails in real use.
Quick quiz
1. What does this print?
rows = ["a", "b", "c", "d", "e"]
cut = int(len(rows) * 0.6)
print(rows[cut:])Show the answer
Answer: A
60% of 5 is 3, so rows[3:] is the last two.
2. Why keep a test set the model never sees during training?
Show the answer
Answer: A
Only unseen rows show how the model will do in real use.
3. You scale with the min and max of the whole dataset, then split. What's wrong?
Show the answer
Answer: A
The test rows helped choose the scaling, so the final check is no longer fair.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictScale three scores
Type exactly what this prints.
- ModifyAll the same
scalecrashes when every value is the same:max - minis 0 and Python refuses to divide by zero. In that case return0.0for every value. - WriteScale with the training numbers
Write
scale_with(train, values)that scalesvaluesusing the min and max of **train**, not ofvalues. Test values can fall outside 0…1, and that's fine. Iftrain's max equals its min, return0.0for every value.scale_with([10, 20, 30], [15, 30, 40]) # [0.25, 1.0, 1.5]
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| Feature | 特征 | An input column the model uses. |
| Label | 标签 | The value the model learns to predict. |
| Test set | 测试集 | Rows kept aside to check the model on unseen data. |
| Min-max scaling | 最小-最大缩放 | Mapping values to 0…1 with (v − min) / (max − min). |