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

31. Train Test Split in Machine Learning

31.5 Practice, Tip and Close

Why. Split until it feels boring — then you are ready for models.

How.

Chapter practice lab

  1. Build 40 fictional delivery rows (cities in Maharashtra, items, distance, was_late).
  2. Split 80/20 with random_state=42 and stratify=y.
  3. Print late-rate in train and test.
  4. Confirm no shared order_id between train and test.
import pandas as pd
from sklearn.model_selection import train_test_split

# Expand to 40 rows in your notebook
df = pd.DataFrame({
 "order_id": [f"BLK-{i:03d}" for i in range(1, 11)],
 "items": [2, 3, 5, 1, 8, 4, 2, 6, 3, 7],
 "distance_km": [1, 2, 4, 1, 7, 3, 2, 5, 2, 6],
 "was_late": [0, 0, 1, 0, 1, 0, 0, 1, 0, 1],
})
X = df[["items", "distance_km"]]
y = df["was_late"]
ids = df["order_id"]
X_tr, X_te, y_tr, y_te, id_tr, id_te = train_test_split(
 X, y, ids, test_size=0.2, random_state=42, stratify=y
)
print(set(id_tr).isdisjoint(set(id_te)))
print(y_tr.mean(), y_te.mean())

Practice task

Write one WhatsApp line to a teammate: “Split 80/20, random_state 42, stratify on was_late, train n=__, test n=__.”

Thodkyaat sangaycha tar (quick recap)

  • Hold test data back like a final exam
  • train_test_split is the standard tool
  • random_state for repeatability; stratify for class balance
  • No split → fake confidence

Samajla ka? Aata pudhe jaauya features aani labels — X ani y clear karuya.