Machine Learning, FoundationsLesson 35 of 92

Module 4 · Generalisation, and the discipline of held-out data

Splits that respect the structure of the data

In this lesson

  • 9 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 4 of 10 in this chapter

This chapter’s goalDesign a validation scheme that matches the structure of the data (grouped, stratified or time-ordered as required) run a hyperparameter search inside it without contaminating the estimate, and state from a learning curve whether the next improvement should come from more data, better features or a different model.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch1:49Not available
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 8Free · no account needed
Lesson steps

Random is a choice, not a default

train_test_split shuffles rows and cuts. That is correct only when rows are independent, and in real data they usually are not. Three structures break it, and each has a matching splitter.

Grouped data: split the entity, not the row

Whenever several rows belong to the same underlying thing (patient, customer, device, document, household) those rows share information. A random split puts some of a patient’s visits in training and the rest in test, and the model can identify the patient rather than the condition.

Python
from sklearn.model_selection import GroupKFold, StratifiedGroupKFold
cv = StratifiedGroupKFold(n_splits=5)
for tr, te in cv.split(X, y, groups=df["patient_id"]):
    ...

StratifiedGroupKFold keeps whole groups together and balances the classes, which is what you almost always want and which did not exist in scikit-learn until relatively recently.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Every lesson here is open. Your progress is recorded once you pass the How a Language Model Actually Works exam.

Stuck on something?Ask Funke, your AI tutor

How to cite: Addaly (2026) ‘Splits that respect the structure of the data’, in Machine Learning, Foundations. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/machine-learning-foundations/splits-that-respect-structure (Accessed: 29 November 2026).