ML1: Thinking in machine learning

Lesson 3 of 5 · reading 15–25 minutes; practice and project time is on the module page

Data as arrays: X, y and one row per prediction

🎯You'll be able to

Turn a table into a feature matrix X and a target vector y, check their shapes and look at the target's balance.

🏠The everyday picture

A teacher's mark sheet has one row per student and one column per test; the final result is in the last column. A model needs data in the same shape: one row per example, one column per input, and the answer kept separately.

📘What it really is

📝A worked example

Input:

from dakdost.features import build_frame
X = build_frame(df)
y = df['late']
print(X.shape, y.shape)
print(y.value_counts())
print(X.head(3))

Expected output: (13410, 12) (13410,), then 0: 12280 and 1: 1130, then three rows. The first is a parcel from Jaipur (JAI) to a regional address, 287.1 km, 1.79 kg, standard service, a morning pickup, an address score of 0.95, a hub load of 27, booked at 7 a.m. on a Thursday (booked_weekday 3, counting Monday as 0). Measured on 2 November 2026 with pandas 3.1.0.

🌍Where it's used

📝Case scenario

A team's model trained without errors but its predictions made no sense. They had sorted X by date and y by shipment id, so each row's answer belonged to a different parcel. A check that X and y came from the same table, in one step, would have caught it.

🖱️Try it

Run the worked example, then find the share of late parcels for each destination zone with df.groupby('dest_zone')['late'].mean(). Success: local is about 5% and remote about 23%.

⚠️Common mistakes

🛠️For your project

Project 1 uses the same build_frame in training and in the API, so the model sees the same 12 columns in both places.

✅Quick check

What is the shape of X for 13,410 parcels and 12 features?

Show the answer

(13410, 12).

What does "imbalanced" mean here?

Show the answer

One answer (late) is much rarer than the other (on time).

← PreviousFraming the problem: target, inputs and the moment of prediction
ProLearners Assist