Velamuvi AIAcademy

Lesson 5 of 13 · How Models Learn

Features and Labels

The building blocks of an ML dataset: examples, features and labels, numerical and categorical features, one-hot encoding and choosing good features, in Python.

  • 5 min read
  • Beginner
  • Reviewed
In this lesson you will learn
  • Identify the examples, features and label in a dataset
  • Tell numerical and categorical features apart
  • Turn categories into numbers with one-hot encoding
  • Judge whether a feature is likely to help a model

Rows and columns

Most machine learning data looks like a table. Each row is one example; each column describes something about it.

size_m2bedroomsareaprice
702city310,000
1204suburbs395,000
551city255,000

Here, size_m2, bedrooms and area are features; price is the label. By convention, the features are called X and the labels y.

Numbers and categories

Models work with numbers, so categories must be converted. You can't just number them (city = 1, suburbs = 2, village = 3), because that would tell the model a village is “three times” a city. The standard fix is one-hot encoding: one column per category, with 1 for the house's category and 0 for the rest:

Try it yourselfPython
houses = [
    {"size_m2": 70,  "bedrooms": 2, "area": "city",    "price": 310_000},
    {"size_m2": 120, "bedrooms": 4, "area": "suburbs", "price": 395_000},
    {"size_m2": 55,  "bedrooms": 1, "area": "city",    "price": 255_000},
    {"size_m2": 95,  "bedrooms": 3, "area": "village", "price": 240_000},
]

# Models need numbers. Turn the category "area" into one 0/1 column per value (one-hot encoding).
areas = sorted({h["area"] for h in houses})            # ['city', 'suburbs', 'village']

X, y = [], []
for h in houses:
    row = [h["size_m2"], h["bedrooms"]] + [1 if h["area"] == a else 0 for a in areas]
    X.append(row)               # features: what the model sees
    y.append(h["price"])        # label: what it must predict

print("Columns:", ["size_m2", "bedrooms"] + [f"area_{a}" for a in areas])
for row, label in zip(X, y):
    print(row, "->", label)

Choosing good features

A feature helps if it carries information about the label. Ask:

  • Is it related to the label? Size affects price; the seller's shoe size doesn't.
  • Will it be available when predicting? If you predict a house's price before it's sold, you can't use “number of viewings in the first week”.
  • Does it leak the answer? A feature that's secretly computed from the label makes the model look perfect in testing and useless in real life.

Common mistakes

Practice exercise

Turn a category into numbers a model can use, with one-hot encoding.

Your solutionPython
colours = ["red", "green", "blue"]
cars = ["blue", "red", "blue"]

# One-hot encode each car's colour: one 0/1 value per colour, in the order of `colours`.
# Print one list per car, like: [0, 0, 1]
Knowledge check

In a dataset for predicting whether an email is spam, what is the label?

Summary

  • A dataset has examples (rows), features (inputs, X) and a label (the answer, y).
  • Features are numerical or categorical; categories become numbers with one-hot encoding.
  • Good features relate to the label and are available at prediction time.
  • Watch for data leakage: features that secretly contain the answer.

Next: why you must keep some data aside for testing.

Finished reading? Tick it off to track your progress.