Ravindra BagaleCourses & study guides मराठी Track your progress

32. Features and Labels in Machine Learning

Data Science

32.1 Features (X) and Labels (y) in Plain Words

Why. sklearn code writes X and y. If those letters feel scary, stop here and make them boring.

What.

  • Features (X) — input columns the model is allowed to look at (items, distance, rain…).
  • Label (y) — the answer column you want to guess (was_late, amount, churn…).

Memory trick: X = eXplanatory inputs. y = the why we care about (the target).

How.

import pandas as pd

df = pd.DataFrame({
 "items": [2, 5, 3],
 "distance_km": [1.2, 4.0, 2.5],
 "was_late": [0, 1, 0],
})
X = df[["items", "distance_km"]] # features
y = df["was_late"] # label
print("X columns:", list(X.columns))
print("y name:", y.name)

Ravindra Bagale's Tip

In the first cell of your notebook, clearly set X as the feature columns and y as the label column you want to predict. Do not depend only on column numbers with df.iloc, because it is very easy to pick the wrong column. Use clear column names, so anyone who reads your notebook knows what goes into the model.