31. Machine Learning मध्ये Train Test Split
31.5 Practice, Tip आणि Close
का. Split कंटाळवाणे वाटेपर्यंत करा — मग models साठी तयार.
कसे.
Chapter practice lab
- 40 काल्पनिक delivery rows तयार करा (Maharashtra cities, items, distance, was_late).
random_state=42आणिstratify=yसह 80/20 split करा.- Train आणि test मधील late-rate print करा.
- Train आणि test मध्ये shared order_id नाही हे तपासा.
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
Teammate ला एक WhatsApp ओळ लिहा: “Split 80/20, random_state 42, stratify on was_late, train n=__, test n=__.”
थोडक्यात सांगायचं तर (quick recap)
- Test data final exam सारखे मागे ठेवा
- train_test_split हे standard tool आहे
- पुन्हा मिळण्यासाठी random_state; class balance साठी stratify
- Split नाही → खोटा आत्मविश्वास
समजलं का? आता पुढे जाऊया features आणि labels — X आणि y स्पष्ट करूया.