Everyone Can Learn Program
中文 Start learning
Specialty · AILesson 1 of 6Free to read

Data, features and labels · AI Foundations

How does a program learn from examples instead of rules?

Course contents

Why 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²)roomsprice (k)
502150
803230
1204330

(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 yourself

Pull 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)
Output
[[50, 2], [80, 3], [120, 4], [65, 2]]
[150, 230, 330, 190]
Step by step
  1. L7Features: one list of inputs per house.
  2. L8Labels: the answer for each house, in the same order.
  3. 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 yourself

Keep 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)
Output
[1, 2, 3, 4, 5, 6, 7, 8]
[9, 10]
Step by step
  1. L280% of 10 rows is 8.8
  2. L3Slices: the first 8 for training, the rest for the final check.

Min-max scaling

Try it yourself

Every 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])
Output
[0.0, 0.43, 1.0, 0.21]
Step by step
  1. L2lo = 50 and hi = 120, so the range is 70.
  2. 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:])
  1. A
    ['d', 'e']
  2. B
    ['c', 'd', 'e']
  3. C
    ['a', 'b', 'c']
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?

  1. ATo check it works on new data, not just on examples it memorised
  2. BTo make training faster
  3. CBecause a model can only learn from half the data
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?

  1. AInformation from the test set leaks into training
  2. BNothing, the order doesn't matter
  3. CScaling only works after splitting because of memory
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.

  1. PredictScale three scores

    Type exactly what this prints.

  2. ModifyAll the same

    scale crashes when every value is the same: max - min is 0 and Python refuses to divide by zero. In that case return 0.0 for every value.

  3. WriteScale with the training numbers

    Write scale_with(train, values) that scales values using the min and max of **train**, not of values. Test values can fall outside 0…1, and that's fine. If train's max equals its min, return 0.0 for 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

TermChineseDefinition
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).