train_test_split Done Right
Stratify, random_state, and ratios.
train_test_split Done Right is a free Data Science Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
One Helper, Two Sets
The function train_test_split takes your features and target and hands back train and test pieces in one tidy call.
from sklearn.model_selection import train_test_splitFour Things Come Back
It returns four objects in a fixed order: train features, test features, train target, test target. Unpack them carefully so each lands in the right name.
X_train, X_test, y_train, y_test = train_test_split(X, y)Set the Test Size
Use test_size to choose how much data to reserve. A value of 0.2 means 20% goes to the test set.
train_test_split(X, y, test_size=0.2)Shuffling by Default
By default the rows are shuffled before splitting. That mixing prevents any hidden order in your file from biasing either set.
Make It Reproducible
Pass random_state to lock the shuffle. The same number gives the same split every run, so your results are repeatable. 🔁
train_test_split(X, y, test_size=0.2, random_state=42)Why Reproducibility Matters
Without a fixed seed, every run reshuffles and your scores wobble. A locked random_state lets teammates reproduce your exact numbers.
The Imbalance Problem
If a class is rare, a plain random split might dump most of it into one set. Now train and test no longer reflect the same class balance.
Stratify to the Rescue
Pass stratify equal to your target so both sets keep the same class proportions. Essential for classification with uneven classes.
train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)Keep X and y Aligned
The split keeps each row's features and label together. Row 7's features always travel with row 7's label, never scrambled apart.
Choosing the Ratio
Common splits are 80/20 or 70/30. With lots of data you can spare a smaller test slice; with little data, give the test set a bit more.
Split Before You Touch
Run the split first, before scaling or filling values. Doing prep on the full set leaks test information back into training.
Quick Check
Your target classes are very imbalanced. Which argument helps?
Recap
Unpack four sets, set test_size, fix random_state for repeatability, and use stratify when classes are uneven. Split first, prep later. 🔁
Frequently asked questions
Is the “train_test_split Done Right” lesson free?
Yes — the full text of “train_test_split Done Right” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.
What will I learn in “train_test_split Done Right”?
Stratify, random_state, and ratios. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Data Science Academy?
No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “train_test_split Done Right” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Data Science Academy lesson?
Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why You Hold Out a Test Set
- train_test_split Done Right
- K-Fold Cross-Validation
- Stop Data Leakage Before It Starts