Sub topic 3
Examples, features, and labels
This chapter assumes you can read basic Python, work with arrays or tables of numbers, and know what a mean and a variance are. It covers the vocabulary that every later chapter uses: examples, features, and labels.
The shape of a dataset
Most machine learning data is a table. Each row is one example (also called a sample or an instance). Each column is a feature, a measurable property of the example. In supervised problems, one extra column holds the label, the answer you want the model to predict (also called the target).
Take the iris dataset. Each example is one flower. The four features are sepal length, sepal width, petal length, and petal width, all in centimeters. The label is the species.
| Example | sepal length | sepal width | petal length | petal width | label (species) |
|---|---|---|---|---|---|
| 1 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 2 | 7.0 | 3.2 | 4.7 | 1.4 | versicolor |
| 3 | 6.3 | 3.3 | 6.0 | 2.5 | virginica |
By convention, the features form a matrix called X with one row per example and one column per feature. The labels form a vector called y with one entry per example. If you have n examples and d features, X has shape (n, d) and y has shape (n,).
Inspecting X and y in code
You can confirm these shapes directly.
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
print(X.shape) # (150, 4)
print(y.shape) # (150,)
print(X[0]) # features of the first example
print(y[0]) # label of the first exampleThe output shows 150 examples with 4 features each. The label 0 is the integer code for setosa. Libraries usually store class labels as integers, so keep a separate list of names if you need to display them.
Features must be numbers
Models do arithmetic, so features must be numeric. Real data often is not. Common conversions:
- A category such as "color" becomes several 0/1 columns (one-hot encoding).
- Text becomes counts or numeric vectors.
- Dates become parts such as day of week or days since a start date.
Choosing and building good features is called feature engineering. A model can only use information that is present in its features. If the property that drives the outcome is missing, no algorithm can recover it.
Labels define the task
The type of label decides the kind of problem. A label that is a category (a species, spam or not spam) makes the task classification. A label that is a number (a price, a temperature) makes it regression. Later chapters cover both.
If the data has no label column, you cannot do supervised learning on it. You can still look for structure in the features alone, which is the subject of unsupervised learning.
Common errors
- Leaking the label into the features. If a column is computed from the answer, such as "refund issued" when predicting "customer unhappy", the model looks excellent in testing and fails in real use.
- Mixing up rows and columns. Scikit-learn expects
(n_examples, n_features). A single example must be passed as a 2D array, for example[[5.0, 3.4, 1.5, 0.2]]. - Treating category codes as quantities. Encoding species as 0, 1, 2 does not mean species 2 is "twice" species 1. Use a classifier, not a regression, for such labels.
- Mismatched lengths.
Xandymust have the same number of rows, or training fails.
Exercise
A hospital table has 2,000 patients. Columns are age, resting heart rate, cholesterol, and "has heart disease" (yes/no). You want to predict heart disease. What are the examples, features, and labels, and what are the shapes of X and y?
Show answer
Each patient is an example. The features are age, resting heart rate, and cholesterol. The label is "has heart disease". X has shape (2000, 3) and y has shape (2000,). Because the label is a category, this is a classification task.