Course / Tutorials / Week 11 / Q-31
Quick tutorial Q-31 · Week 11 · about 15 min · Core
Scale two features, the right way and the wrong way
Predict running pace from step rate and step length with a nearest-neighbor model, and see what scaling does, and what fitting the scaler on the test runs too does, with only 30 runs.
- Ask
- Collect
- Check
- Interpret (this week)
- Coach
- Reassess
Download notebook ·Python file ·Read as Markdown ·Data folder ·Chapter 11 (sections 11.7.2)
Every cell runs in this page; the first run downloads Python (about 30 s). Nothing you load leaves your browser.
The decision
Before trusting a model's error on a small personal data set, check that the scaler learned nothing from the test runs. If it did, the error you report is better than the athlete will see.
The raw data
raw/sessions_fit.csv: 693 watch activity summaries (FIT session fields), June 2024 to November 2026, in the watch's own units (step length in millimeters, cadence in strides per minute for one foot); this tutorial uses the first 30 outdoor runs. Activity summaries from a COROS Training Hub archive of an adult male athlete in his mid-40s, published in the course case file (athletes/jh/coros-history/). GPS positions, per-second records and identifiers were removed before publication.
Try it
A nearest-neighbor model finds the past runs most like a new one by measuring a distance between them. If one feature is in steps per minute (about 165) and the other in meters (about 1.4), the distance is almost all step rate. Scaling puts both on one footing: subtract a mean and divide by a standard deviation, so each feature has mean 0 and spread 1.
The question here is whose mean and standard deviation. The data are the first 30 runs an adult male athlete in his mid-40s recorded on a new watch, in the summer of 2024. The model learns from the first 20 and is tested on the last 10, in time order, the way a personal model is used: on runs it has not seen yet (Chapter 11, splitting by day).
Open the file and convert the units
Type a prediction to enable Run.
Show the expected result without running
cadence_spm step_m pace_s_km mean 155.20 0.95 420.01 std 21.21 0.13 91.72 min 106.00 0.76 238.99 max 182.00 1.37 644.54
Step rate spreads by about 21 steps per minute and step length by about 0.13 m: a ratio of more than a hundred. Unscaled, a difference of one step per minute counts as much as a difference of a whole meter of step length. The low step rates (106 at the bottom) come from runs with walking in them, which also have the slowest paces.
The right way: fit the scaler on the training runs only
A pipeline joins the scaler and the model, so the scaler learns its mean and standard deviation inside fit, from the 20 training runs alone. The test runs are then scaled with those numbers, as a run next week would be.
Type a prediction to enable Run.
Show the expected result without running
no scaling mean error 36.0 s/km scaler fitted on training runs mean error 27.3 s/km
The wrong way: fit the scaler on all 30 runs first
The common shortcut scales the whole table and then splits it. The scaler has then seen the 10 test runs, which in real use would not exist yet. With only 30 runs, ten of them move the scaler's numbers a lot.
Type a prediction to enable Run.
Show the expected result without running
scaler mean, all runs: [155.2 0.95] SD [20.852 0.125] scaler mean, train only: [159.3 0.97] SD [19.217 0.144] leaky mean error 24.9 s/km

Scaling cut the error by about a quarter, from 36.0 to 27.3 s/km. The leaky scaler looks better again, at 24.9 s/km, but that gain is not real: it came from knowing the spread of runs that had not happened yet. With thousands of rows the two scalers would be almost the same (CS-13 shows that); with 30 they are not. The rule does not depend on size: anything that learns from data, a scaler included, is fitted on the training rows only.
For the athlete: "The pace model was checked only on runs it had never seen, so its error of about 27 seconds per kilometer is the one you can expect next week."
Action card
- Finding
- Scaling the two features cut the model's pace error by about a quarter; the leaky scaler made it look better still.
- Evidence
- Mean error on 10 later runs: 36.0 s/km unscaled, 27.3 s/km scaled on the training runs, 24.9 s/km scaled on all runs.
- How sure
- Low. Ten test runs from one athlete in one summer, some with walking in them.
- Action
- Report the 27.3 s/km, the number from a scaler that never saw the test runs.
- Test again
- Repeat with the next 30 runs and see whether the ranking holds.
Check yourself
Why is the leaky scaler's lower error not a real improvement?
Without scaling, which feature decides which past runs count as nearest?
Built 2026-12-02 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.