Samples, Features, Labels and Feature Vectors
Every machine-learning paper, library and job description uses the same small set of words for the parts of a dataset, often with two or three synonyms for each. This lesson pins them down on one concrete dataset, so that "feature vector", "target" and "data point" mean exactly one thing for the rest of the track.
The anatomy of a dataset
| Term | Also called | Meaning | In the iris dataset |
|---|---|---|---|
| dataset | data set, corpus (text) | the whole collection used for a task | 150 flowers |
| sample | observation, example, instance, record, row, data point | one item | one flower |
| feature | attribute, variable, input, predictor, column | one measured property | petal length |
| feature vector | input vector, | all feature values of one sample, in a fixed order | |
| label | target, output, response, | what the model should predict | species |
| class | category | one possible value of a categorical label | setosa, versicolor, virginica |
Data point usually means one sample seen as a point in feature space, the picture you get when you plot samples by two of their features. A label exists only in supervised learning; in unsupervised learning (clustering, for example) the dataset is features only.
The notation
The track uses the standard notation of the Mathematics for ML course. With samples and features:
So is feature of sample : row , column . For classification, each is one of classes; for regression, is a real number. A model is a function with .
The same in code
scikit-learn follows the notation exactly: X is a 2-D array of shape (n_samples, n_features) and y a 1-D array of length n_samples.
from sklearn.datasets import load_iris
iris = load_iris()
X, y = iris.data, iris.target
n, d = X.shape
print("n samples:", n, "| d features:", d)
print("feature names:", iris.feature_names)
print("classes:", iris.target_names.tolist(), "coded as", sorted(set(y.tolist())))
i = 51
print(f"feature vector x_{i}:", X[i], f"| label y_{i}:", y[i], "=", iris.target_names[y[i]])
print("feature 'petal length' (column 2), first five samples:", X[:5, 2])n samples: 150 | d features: 4
feature names: ['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
classes: ['setosa', 'versicolor', 'virginica'] coded as [0, 1, 2]
feature vector x_51: [6.4 3.2 4.5 1.5] | label y_51: 1 = versicolor
feature 'petal length' (column 2), first five samples: [1.4 1.4 1.3 1.5 1.4]
With pandas, the same split into features and label is one line, and it is the moment to make sure the label column does not stay among the features:
import pandas as pd
from sklearn.datasets import load_iris
df = load_iris(as_frame=True).frame
print(df.columns.tolist())
X = df.drop(columns="target")
y = df["target"]
print("X:", X.shape, "| y:", y.shape)
print("label is not a feature:", "target" not in X.columns)
print(df.sample(3, random_state=0).to_string())['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)', 'target']
X: (150, 4) | y: (150,)
label is not a feature: True
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
114 5.8 2.8 5.1 2.4 2
62 6.0 2.2 4.0 1.0 1
33 5.5 4.2 1.4 0.2 0
Features in feature space, from scratch
A feature vector is literally a point. Distances between points are how many models (k-nearest neighbours, clustering) decide what is "similar". With plain NumPy, the nearest neighbour of a new flower:
import numpy as np
from sklearn.datasets import load_iris
iris = load_iris()
X, y = iris.data, iris.target
new_flower = np.array([6.1, 2.9, 4.9, 1.6])
dist = np.sqrt(((X - new_flower) ** 2).sum(axis=1)) # Euclidean distance to every sample
nearest = np.argsort(dist)[:3]
for i in nearest:
print(f"sample {i:3} distance {dist[i]:.3f} {iris.target_names[y[i]]}")sample 127 distance 0.224 virginica
sample 138 distance 0.265 virginica
sample 126 distance 0.265 virginica
The distance formula is , summed over the features. All three nearest flowers are virginica, so a nearest-neighbour model would call the new flower virginica. Every feature counts equally in the distance, which is why features measured on very different scales need scaling (Data Preparation).
Pitfalls
- The label among the features. Forgetting to drop the target column gives a perfect but useless model.
- Features that are not available at prediction time. "Days in hospital" cannot be a feature for a model that decides on admission.
- The unit of a sample. Is a sample a patient, a visit, or a lab test? If one patient has five visits and visits are samples, a random split puts the same patient in train and test. Decide what one row means before anything else.
- Mixed-up synonyms. "Target" in a marketing table may mean a sales goal, not the ML label. Read the data dictionary.
Practice
- A churn dataset has one row per customer with 12 columns:
customer_id, 10 behaviour columns andchurned. What are , and if it has 5,000 rows?
Answer
samples. customer_id is an identifier, not a feature, and churned is the label, so . is the churned column, a vector of 5,000 zeros and ones.
- In the nearest-neighbour example, which class would a 3-nearest-neighbour vote give the new flower?
Answer
Virginica: all three nearest samples (127, 138 and 126) are virginica, so the vote is 3 to 0. A k-NN classifier is exactly this vote.
Test yourself
In scikit-learn, what is the shape of X for 200 samples with 7 features?
- (7, 200)
- (200, 7)
- (200,)
- (1400,)
What is ?
- The label of sample
- Feature of sample
- The distance between samples and
- The number of classes
Which of these is a synonym of 'label'?
- Feature
- Target
- Sample
- Metadata
A model reaches 100% accuracy on the first try. What should you check first?
- Nothing, it is a great model
- Whether the label (or a copy of it) is still among the features
- Whether the dataset has too few columns
- The learning rate