32. Features and Labels in Machine Learning
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.
Ravindra Bagale's Tip – मराठी
तुमच्या notebook च्या पहिल्या cell मध्ये X म्हणजे feature columns आणि y म्हणजे predict करायचा label column स्पष्टपणे ठरवा. फक्त df.iloc मधल्या column क्रमांकांवर अवलंबून राहू नका, कारण चुकीचा column निवडला जाणं खूप सोपं असतं. Column ची स्पष्ट नावं वापरा, म्हणजे notebook वाचणाऱ्या कोणालाही model मध्ये काय जातं ते लगेच कळेल.
Ravindra Bagale's Tip – हिंदी
अपने notebook के पहले cell में X यानी feature columns और y यानी predict करने वाला label column साफ़-साफ़ तय करो. सिर्फ़ df.iloc के column नंबरों पर भरोसा मत करो, क्योंकि गलत column चुन लेना बहुत आसान है. Columns के साफ़ नाम इस्तेमाल करो, ताकि notebook पढ़ने वाले हर किसी को पता चले कि model में क्या जा रहा है.