Foundations

Features, Labels and Examples: The Vocabulary of a Learning Problem

Learn what examples, features, labels and targets mean, how they map to arrays and shapes, and the traps hidden in each choice.

Key takeaways

  • An example is one unit of prediction, and you should be able to say in one sentence what a single row of your table represents.
  • A feature is legitimate only if its value will be known, in the same form, at the moment the prediction is made.
  • Labels are measurements with their own errors and definitions, so find out who or what produced them.
  • In the usual array convention, the input matrix has shape (n, d) for n examples and d features, and the target vector has length n.
  • Different fields use different names for the same things, which a short translation table resolves.

An example is one case your model learns from or makes a prediction about. Its features are the pieces of information you are allowed to use, and its label is the answer you want the model to produce. Most of supervised learning is finding a function that takes features in and gives something close to the label out.

The words sound obvious, yet many failed projects trace back to a vague choice about one of them. Deciding exactly what a row stands for, what goes into it and what counts as the right answer is design work, and it happens before any algorithm runs.

Start with the table

Picture a spreadsheet for predicting apartment rents. Each row is a listing. The columns hold floor area, number of bedrooms, district and building age. One more column holds the monthly rent.

Floor area (m²) Bedrooms District Age (years) Rent
45 1 north 30 900
70 2 center 12 1,650
95 3 south 5 1,800

These figures are invented for illustration. The first four columns are features, the last is the label, and each row is an example. In math notation, one example is a pair (x, y): x is the list of feature values and y is the label. A model produces ŷ, read "y hat", its prediction of y.

From a raw case to a training pair
1 Pick the unit Decide what one row stands for 2 Gather features Collect inputs known at prediction time 3 Attach the label Record the outcome you want predicted 4 Encode Turn every value into numbers Pick the unit Decide what one row stands for 1 Gather features Collect inputs known at prediction time 2 Attach the label Record the outcome you want predicted 3 Encode Turn every value into numbers 4

What counts as one example

The unit of prediction is the thing you score once. For rents it is a listing. For a medical model it might be a patient, a hospital visit or a single scan, and these lead to quite different datasets.

The choice has consequences. If one patient contributes twenty scans, those rows are related, so scattering them across training and test data lets the model recognize the patient instead of the condition. Group-aware splitting keeps all rows from one source on the same side.

Time often belongs in the definition too. "A customer" is vague. "A customer as of the first day of a month" is an example you can build features for, and it makes clear which facts lie in the future.

Kinds of features

Features arrive in several types, and each needs its own handling before a model can use it.

  • Numeric features are quantities, such as area or temperature. Many methods work better when numeric columns are on comparable scales.
  • Categorical features name a group, such as district. They need encoding, for instance a separate 0 or 1 column per category, which is called one-hot encoding.
  • Ordinal features are categories with a natural order, such as small, medium and large.
  • Time values usually become derived quantities, such as day of week or days since the last purchase.
  • Text, images and audio are high-dimensional raw inputs. Modern models often learn their own internal features from them.

An engineered feature is one you compute yourself, such as price per square meter or the ratio of debt to income. A learned representation is a set of numbers a model constructs on the way to its answer. Both serve the same purpose: presenting the input in a form where the pattern is easier to express.

Be careful with that price per square meter example. If price is what you are predicting, a feature computed from it contains the answer and cannot be used.

Labels, targets and where they come from

The label, also called the target, defines the task. Its type decides much of what follows.

Label type Task Example question
A number Regression What rent would this listing get?
One of two classes Binary classification Is this transaction fraudulent?
One of several classes Multiclass classification Which of five species is this?
Any number of tags Multilabel classification Which topics does this article cover?
A relative order Ranking Which results should appear first?

Labels come from people who annotate cases, from later events (did the customer cancel?), from instruments, or from rules applied to logs. Each source makes mistakes. Annotators disagree on borderline cases, and a recorded "no fraud" may only mean that nobody checked. Treat the label as a measurement of the thing you care about, which can differ from the thing itself.

Some methods need no labels at all. Unsupervised learning looks for structure in the features, such as clusters. Self-supervised learning manufactures labels from the data, for example by hiding a word and asking the model to predict it. You can read more about making labels from the data itself.

The same ideas as arrays

Libraries expect features as a two-dimensional array, conventionally called X, with one row per example and one column per feature. The targets form a vector y with one entry per row.

import numpy as np

# columns: floor area, bedrooms, age
X = np.array([[45, 1, 30],
              [70, 2, 12],
              [95, 3, 5]], dtype=float)
y = np.array([900, 1650, 1800], dtype=float)

print(X.shape)   # (3, 3): n = 3 examples, d = 3 features
print(y.shape)   # (3,)
print(X[1])      # all features of the second example
print(X[:, 0])   # floor area across all examples

Here n = 3 and d = 3, which happen to match. In real data they rarely do, so check which axis is which. District is missing from this array because it is text; one-hot encoding the three districts would add three columns and make d equal to 6.

Each row can also be read as a point in a d-dimensional space, which is the starting idea of the geometry behind a feature table. Distance, direction and boundaries between classes all get their meaning there.

Image and text data add axes. A batch of color images is commonly a four-dimensional array covering example, height, width and channel, though the order of axes differs between libraries.

The availability test

For every candidate feature, ask: will I know this value, computed this way, at the instant I need the prediction? If a model predicts at application time whether a loan will be repaid, then "number of missed payments on this loan" fails the test completely.

Subtler failures are common. A field gets overwritten when an account closes, so historical rows show its final value. A summary statistic was computed over the whole year, including months after the prediction date. An identifier number was assigned in an order that happens to track the outcome. All of these are forms of target leakage, and they produce impressive test scores that vanish in use.

Identifiers deserve special suspicion. A customer ID carries no general pattern, but a flexible model can use it to memorize individuals.

Write the definitions down

Before training, record a short specification that another person could use to rebuild your table:

  1. Unit: what one row represents, and at what moment it is observed.
  2. Population: which cases are included, and which are filtered out and why.
  3. Features: each column's meaning, unit, source and the time at which it becomes known.
  4. Label: the exact rule that assigns it, including how long you wait for the outcome.
  5. Missingness: what an empty value means in each column.

The last item is easy to skip. An empty "previous purchases" field might mean zero, might mean unknown, or might mean the customer predates the tracking system. These call for different handling, and filling all of them with 0 erases the difference.

The label rule deserves the same precision. "Churned" could mean a cancellation, a month without activity or a failed payment that was never fixed. Each definition selects a different set of positive examples, so two teams using the same word can train models that disagree for good reason. When the definition changes, treat the result as a new dataset with its own version, and do not compare scores across the two.

One thing, many names

Machine learning borrowed from statistics, signal processing and computer science, and kept several vocabularies.

This article Other names you will meet
Example Instance, sample, observation, record, row
Feature Predictor, covariate, attribute, input variable, column
Label Target, response, outcome, dependent variable
Parameters Weights, coefficients

"Sample" is the awkward one: statisticians often use it for a whole collection of observations, while many programmers use it for a single row. Read the context.

Common questions

Can a column be both a feature and a label?

In different problems, yes. Income can be an input when predicting spending and the target in another model. Within one problem and one prediction, the target must not appear among the inputs, directly or through a calculation.

How many features should I have?

There is no fixed number. Useful features lower error, while irrelevant ones add noise and cost, especially when examples are few. Start with the inputs you can justify, then measure on held-out data whether additions help.

Written for 4MachineLearning with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 42 articles in Foundations →

Latest news on this topic