TExcelTAD
Home › Data for ML: Types, Formats, Sources & Quality › Samples, Features, Labels and Feature Vectors

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

sepal lensepal widpetal lenpetal widspecies 6.43.24.51.5versicolor features: matrix X (n rows, d columns)label y sample ione feature (column j) x_i, y_i
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, xi\mathbf{x}_i all feature values of one sample, in a fixed order (6.4,3.2,4.5,1.5)(6.4, 3.2, 4.5, 1.5)
label target, output, response, yiy_i 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 nn samples and dd features:

X=(x1⊤x2⊤⋮xn⊤)∈Rn×d,xi=(xi1⋮xid)∈Rd,y=(y1⋮yn)X = \begin{pmatrix} \mathbf{x}_1^\top \\ \mathbf{x}_2^\top \\ \vdots \\ \mathbf{x}_n^\top \end{pmatrix} \in \mathbb{R}^{n \times d}, \qquad \mathbf{x}_i = \begin{pmatrix} x_{i1} \\ \vdots \\ x_{id} \end{pmatrix} \in \mathbb{R}^{d}, \qquad \mathbf{y} = \begin{pmatrix} y_1 \\ \vdots \\ y_n \end{pmatrix}

So xijx_{ij} is feature jj of sample ii: row ii, column jj. For classification, each yiy_i is one of KK classes; for regression, yiy_i is a real number. A model is a function ff with f(xi)≈yif(\mathbf{x}_i) \approx y_i.

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.

python · try it yourself
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:

python · try it yourself
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:

python · try it yourself
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 ∥xi−x∥=∑j(xij−xj)2\lVert \mathbf{x}_i - \mathbf{x} \rVert = \sqrt{\sum_j (x_{ij} - x_j)^2}, summed over the dd 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

  1. A churn dataset has one row per customer with 12 columns: customer_id, 10 behaviour columns and churned. What are nn, dd and y\mathbf{y} if it has 5,000 rows?
Answer

n=5000n = 5000 samples. customer_id is an identifier, not a feature, and churned is the label, so d=10d = 10. y\mathbf{y} is the churned column, a vector of 5,000 zeros and ones.

  1. 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?

  1. (7, 200)
  2. (200, 7)
  3. (200,)
  4. (1400,)

What is xijx_{ij}?

  1. The label of sample ii
  2. Feature jj of sample ii
  3. The distance between samples ii and jj
  4. The number of classes

Which of these is a synonym of 'label'?

  1. Feature
  2. Target
  3. Sample
  4. Metadata

A model reaches 100% accuracy on the first try. What should you check first?

  1. Nothing, it is a great model
  2. Whether the label (or a copy of it) is still among the features
  3. Whether the dataset has too few columns
  4. The learning rate