Course / Tutorials / Week 11 / Q-32
Quick tutorial Q-32 · Week 11 · about 15 min · Core
Split by athlete, not by row
Score one nearest-neighbor model of peak foot force twice, with shuffled steps and with whole runners held out by fixed folds, and see where the difference comes from.
- Ask
- Collect
- Check
- Interpret (this week)
- Coach
- Reassess
Download notebook ·Python file ·Read as Markdown ·Data folder ·Chapter 11 (sections 11.7.1, 11.7.2)
Every cell runs in this page; the first run downloads Python (about 30 s). Nothing you load leaves your browser. Helpful first: Q-31 Scale two features, the right way and the wrong way.
The decision
Before a model's score goes into a report for a coach, decide what it will be used on. For a runner the model has never seen, only a split that keeps each runner on one side gives the honest score.
The raw data
raw/biomechanics.txt: one row per left-foot step on an instrumented treadmill, nine gait measures and the peak force (2,625 rows). From the Data-Driven Engineering case study Biomechanic Regression; data courtesy of Iain Hunter, BYU Biomechanics.
Try it
The file holds about 2,600 left-foot steps from an instrumented treadmill. Each runner contributes many steps, and neighboring steps of one runner are nearly copies of each other. A model tested on steps from runners it has already seen will look far better than it will be for a new runner. The fix is to split by athlete: every step of a runner goes to the same side.
Open the file and label the runners
The file names no runner. Each runner ran a ladder of rising speeds, so a speed the ladder has already used starts a new runner (the same inference as CS-13).
Type a prediction to enable Run.
Show the expected result without running
2568 steps, 102 trials, 17 runners median steps per runner: 125
Two splits, one model
The model is five nearest neighbors on the nine gait measures, scaled inside a pipeline. The row split shuffles the steps into five folds. The athlete split makes the fold from the runner number: runner 1, 6, 11 and 16 in one fold, and so on (runner % 5). Fixed folds give the same answer in every version of scikit-learn; GroupKFold does the same job, but how it assigns runners to folds has changed between versions.
Type a prediction to enable Run.
Show the expected result without running
split by row R² 0.93 split by runner R² 0.31 runners in fold 1: [1, 6, 11, 16]
Where the difference comes from
Compare the error for each runner under the two splits.
Show the expected result without running
by row 0.06 by runner 0.22 runners whose error grew more than threefold: 10

With a row split, every runner's own steps sat in the training folds, a few rows away, and the nearest neighbors found them. Held out, the model has to predict a runner it never saw, which is the job a coach would give it. Report the second number.
For the athlete: "A model like this one would get your peak force wrong by about a fifth of your body weight on an average step, so we use it to rank gait measures, not to tell you your force."
Action card
- Finding
- The same model scores R² 0.93 on shuffled steps and 0.31 on runners it has not seen.
- Evidence
- With runners held out by fixed folds, the mean error per step rose from 0.06 to 0.22 body weights.
- How sure
- Moderate. 17 runners inferred from speed ladders on one treadmill.
- Action
- Report the score with runners held out, and say how the folds were made.
- Test again
- Recheck with another assignment of runners to folds (for example
(runner + 1) % 5).
Check yourself
Why does
runner % 5make a fair split?For which use is a split by row acceptable?
Acknowledgements
The data set and the original case study are from the Data-Driven Engineering course, with data courtesy of Iain Hunter, BYU Biomechanics.
References
- Data-Driven Engineering: Biomechanic Regression.
- Machine Learning for Engineers: Split Data: Train, Validate, Test.
Built 2026-11-30 with Python packages numpy 2.5.5, pandas 3.1.1, matplotlib 3.11.2, sklearn 1.9.1, scipy 1.18.1. In the browser the same code runs on Pyodide, so the last digit can differ.