Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Machine Learning Foundations — Alpha School

Lesson 2: Data, Features and Labels

If machine learning is learning from examples, then data is the textbook — and like any textbook, its quality decides how much can be learned. A model can only learn what its data shows it. Give it clear, honest, well-chosen examples and it can learn something useful. Give it messy, biased or misleading examples and it will learn the mess, confidently. That is why people who work with machine learning spend far more time preparing data than building models, and why this lesson comes before any algorithm.

Every example a model learns from is described by features: the individual measurements or facts that describe it. Imagine you want to predict the price of a house. Each house in your data might have features such as its size in square metres, the number of bedrooms, its age, and how far it is from the nearest station. The model looks at how those features relate to the price. Features are the model's eyes: it cannot see anything about a house that is not written down as a feature, so choosing the right features is one of the most important decisions in any project.

Labels: the answers the model studies

In supervised learning, each example also carries a label: the correct answer the model is trying to learn to predict. For the house-price example, the label is the price the house actually sold for. For a spam filter, the label is whether each email really was spam. For a system that recognises handwritten digits, the label is the digit each image shows. Labels are usually supplied by people — someone had to mark those emails, or price those houses — and that makes them expensive and sometimes wrong. A model trained on wrong labels learns wrong answers, so checking labels matters as much as collecting them.

Features come in different kinds, and the kind decides how a model can use them. Some features are numbers, such as age or temperature, and they can be compared and added directly. Others are categories, such as a town name or a colour, which have no natural order: you cannot say that red is greater than blue. Categories usually have to be converted into numbers before a model can use them, for example by giving each category its own yes-or-no column. Text, images and sound are features too, but they need extra steps before they become numbers a model can work with — a photo, for instance, becomes a grid of brightness values, one per pixel.

Numbers that are not measurements

Not every number in a dataset is a measurement. A postcode, a customer number or a product code may look like a number, but adding two postcodes together means nothing, and a model that treats them as measurements will learn nonsense. Such numbers are really labels in disguise, and they should be handled as categories. Dates are another trap: the number 20240115 looks like a measurement but is really a calendar date, and a model will do better with the year, the month or the day of the week pulled out separately. The skill here is to look at each column and ask what kind of thing it really is — a measurement, a category, a date, or an identifier that should not be used at all. Getting that question right, column by column, is unglamorous work, but it is the foundation every later step rests on, and it is where experienced practitioners spend much of their time.

A good dataset is large enough to show the real pattern, varied enough to cover the cases the model will meet, and honest about its gaps. A dataset of house prices from one wealthy neighbourhood will not teach a model about the whole city. A dataset of faces that mostly shows one skin tone will not teach a model to recognise everyone fairly — a problem you will meet again in Lesson 17. Before building anything, always ask three questions about your data: where did it come from, who or what is missing from it, and could any of its labels be wrong? The answers shape everything that follows.

Key Terms

Data
The examples a model learns from, usually organised as rows of examples and columns of measurements.
Feature
One measurement or fact describing an example, such as a house's size or an email's sender.
Label
The correct answer attached to an example in supervised learning, such as the real selling price of a house.
Supervised learning
Learning from examples that carry labels, so the model can check its guesses against the right answers.
Numeric feature
A feature measured as a number, such as age, temperature or price.
Categorical feature
A feature that takes one of a set of named groups, such as a town name or a colour, with no natural order.
Dataset
A collection of examples used to train or test a model.

Discussion Questions

  1. You want to predict whether students will pass an exam. What features would you collect, and which of them might be unfair or misleading to use?
  2. Labels are usually made by people. Where could a label be wrong, and what would a model learn from wrong labels?
  3. Why is a dataset that is large but all from one place sometimes worse than a smaller, more varied one?

Practice Questions

  1. What is a feature? Give an example from house-price prediction.
  2. What is a label, and why does supervised learning need labels?
  3. Why can't a model use a category like a town name directly as a number?
  4. A photo is a feature for an image model. How does it become numbers?
  5. Name two questions you should ask about any dataset before training a model.
Show answers
  1. A feature is one measurement describing an example, such as the number of bedrooms. Features are the facts the model can see; it cannot use anything that is not written down as a feature.
  2. A label is the correct answer for an example, and the model learns by comparing its guesses with it. Without labels the model has no way to know whether its predictions are right or wrong.
  3. Because categories have no natural order or size. You cannot say one town is greater than another, so categories are usually turned into separate yes-or-no columns.
  4. It becomes a grid of brightness values, one number per pixel. Each pixel's brightness is a number, so the whole image is a long list of numbers the model can work with.
  5. Where did it come from, and who or what is missing from it? The source tells you how trustworthy it is, and missing groups tell you whose cases the model may handle badly.
Keep reading to complete this lesson