Last updated

Preparing Data: Features and Labels

Definition: Before training, raw data must be shaped into features (X) — a list of inputs for each example — and labels (y) — the answer for each example.

Example — turn records into X and y

Say we want to predict a house price from its size and number of rooms:

data = [
    {"size": 50,  "rooms": 1, "price": 100},
    {"size": 80,  "rooms": 2, "price": 150},
    {"size": 120, "rooms": 3, "price": 220},
]

X = [[d["size"], d["rooms"]] for d in data]   # features
y = [d["price"] for d in data]                # label

print("Features (X):", X)
print("Labels (y):  ", y)

The shape that models expect

Almost every scikit-learn model wants X as a list of rows (one row of features per example) and y as a flat list of answers. Getting your data into this shape is most of the work in a real ML project.

💡 Tip: text categories (like "red"/"blue") must be turned into numbers before training — this step is called encoding.

Try it Yourself
Output

          
Ad · responsive