Sub topic 1
Features and labels: what the model sees and what it predicts
Prerequisites
This chapter assumes you know basic Python, including classes and methods. It also assumes you know what a NumPy array is and what supervised learning means: learning a mapping from inputs to known answers. Familiarity with pandas DataFrames helps for the later examples.
The two roles in a dataset
Every supervised learning problem splits the data into two parts.
- Features are the inputs the model sees. Each feature is one measurable property of a sample, such as the petal length of a flower.
- A label is the answer the model learns to predict. It is also called the target.
A sample is one row of the data, for example one flower. In scikit-learn, features are named X and labels are named y. The capital letter marks X as a 2D table. The lowercase letter marks y as a 1D list.
The model never sees the label of a sample when it predicts for that sample. It sees only the features and returns a guess for the label.
A first look at real data
The Iris dataset ships with scikit-learn. Each sample is one iris flower. It has four measurements and one species label.
from sklearn.datasets import load_iris
X, y = load_iris(return_X_y=True)
print(X.shape) # (150, 4)
print(y.shape) # (150,)X has 150 rows (samples) and 4 columns (features). y has 150 entries, one label per sample. Row i of X always matches entry i of y. If the two fall out of step, the model learns from wrong answers and nothing warns you.
Choosing X and y from a table
Real data usually arrives as a pandas DataFrame with every column together. You decide which column is the label. Every column you want the model to use becomes a feature.
import pandas as pd
df = pd.DataFrame({
"age": [22, 35, 47, 52],
"city": ["Paris", "Rome", "Paris", "Oslo"],
"bought": [0, 1, 1, 0],
})
X = df[["age", "city"]]
y = df["bought"]
print(X.shape, y.shape) # (4, 2) (4,)Note the double brackets in df[["age", "city"]]. They select several columns and return a DataFrame, which is 2D. Single brackets in df["bought"] select one column and return a Series, which is 1D. This matches the shapes scikit-learn expects.
Common errors
- Passing a 1D array as
X. scikit-learn raises an error that asks you to reshape your data. For a single feature, useX.reshape(-1, 1). - Mixing up the order of arguments. Estimators expect
Xfirst andysecond, as infit(X, y). - Using an identifier column, such as a customer ID, as a feature. It carries no real signal and can mislead the model.
- Having different numbers of rows in
Xandy. This raises aValueErrorabout inconsistent sample counts.
Exercise
A DataFrame df has columns "height", "weight", and "isathlete". You want to predict "isathlete" from the other two columns. Write the two lines that create X and y, and state the shape of each if df has 200 rows.
Show answer
X = df[["height", "weight"]] and y = df["is_athlete"]. X has shape (200, 2) and y has shape (200,).
Exercise
Why does df["weight"] fail as a value for X, while df[["weight"]] works?
Show answer
Single brackets return a 1D Series, but X must be 2D with rows as samples and columns as features. Double brackets return a one-column DataFrame, which is 2D.