Unit 2.2 · about 3 h
6 / 324Split data by time and prevent leakage
A random split can make a time-dependent prediction task look easier than it is. Leakage makes the evaluation score optimistic because information from the target or future has entered training.
- implement
- explain
- measure
You can now
- Sort observations by their timestamp before splitting.
- Create a chronological train/test holdout with 2011 for training and 2012 for final testing.
- Exclude target-derived fields and post-outcome identifiers from the feature matrix.
- Explain why preprocessing must be fitted on training data only.
- Exclude observed
weathersit,temp,atemp,humandwindspeedfrom model inputs because the project predicts at the start of the hour; retain them only for exploratory analysis and post-hoc error slices.
Covers
- Chronological splits and leakage control
- Prediction-time feature availability
Study, in this order
3 steps
- Common pitfalls and recommended practicesCommon pitfalls and recommended practices
Read inconsistent preprocessing and data leakage, including the wrong/right examples and the Pipeline recommendation.
- TimeSeriesSplitTimeSeriesSplit
Read why time-ordered data should not be evaluated by ordinary shuffled cross-validation.
- Bike Sharing DatasetBike Sharing Dataset
Use
dtedayonly to sort and define the 2011/2012 split. Excludeinstant,dteday,yr,casual,registeredandcntfrom model features.
Focus on
- Chronological train/test separation
- Target leakage and target-derived columns
- Preprocessing fit only on the training partition
- What the final test set is for and when not to inspect it
- Feature availability at prediction time: calendar fields versus observed weather measurements
Reproduce closed-book
dteday, create the 2011 training partition and 2012 final test partition, then print date bounds, row counts and feature columns. Assert that cnt, casual, registered, instant, dteday, yr, weathersit, temp, atemp, hum and windspeed are absent from X.Traps
- Do not shuffle a temporal holdout just to balance the dates.
- Do not use
casualorregistered: together they determinecntand would reveal the answer. - Do not fit imputation, encoding or scaling on the full dataset before splitting.
Practice
Reproduce one wrong/right preprocessing example and write a leakage checklist for the bike dataset.
Treat the checklist as a gate before fitting any model.
Self-test
-
Why must
casualandregisteredbe excluded when predictingcnt?The targetcntis the total count including both casual and registered users. These fields disclose its components and create direct target leakage. -
Why should the 2012 partition be left untouched until the model is selected?
Repeated decisions based on test results turn the test set into a tuning set and make the final performance estimate optimistic.
Completion criterion
Stop when
The split is chronological, the target-derived columns, identifiers, split-only fields and observed weather measurements are absent from X, and every learned preprocessing step is fitted only within the training partition.
Mark passed, then open 2.3 Only mark this when you can do it without notes.Leads to
- 2.3 Build a preprocessing pipeline and first model Follow the practical starter sequence; each unit uses the artifact from the previous step.
Comes after
- 2.1 Frame a prediction task and choose a baselineFollow the practical starter sequence; each unit uses the artifact from the previous step.
Resources for this unit
- Bike Sharing Dataset Use the hourly dataset (17,389 records) for a first regression project. Sort chronologically; use 2011 for training and 2012 as the untouched final test period.
cntis the target. Excludecasual,registered,cnt,instant,dtedayandyrfrom the model feature matrix; usedtedayonly to sort/split. - Common pitfalls and recommended practices Read the sections on inconsistent preprocessing and data leakage. Use the wrong/right examples to audit the project before trusting the reported test score.
- TimeSeriesSplit Read why ordinary cross-validation can train on future observations and evaluate on the past. The starter project uses one chronological year holdout; use TimeSeriesSplit when extending it to repeated time-aware validation.
Also in this section
- Introduction to Machine Learning Problem Framing Complete the short course on deciding whether ML fits the problem, framing the model output and choosing success metrics before touching the estimator.
- Kaggle Learn — Intro to Machine Learning Complete the lessons on how models work, data exploration, the first model, model validation, underfitting/overfitting and random forests. Reproduce the exercises in a fresh notebook.
- Google Machine Learning Crash Course Use the modules on regression, numerical and categorical data, datasets/generalization/overfitting, and classification metrics as a structured conceptual companion. The course is free and includes interactive exercises.
- Pipelines and composite estimators Study
PipelineandColumnTransformer. Keep preprocessing inside the pipeline so transformations are fitted only on the training partition and applied consistently at evaluation time. - Model evaluation: regression metrics Use the regression metric definitions for MAE, MSE/RMSE and R-squared. Explain the units and limitations of each metric instead of reporting a score without context.
- Machine Learning in Python with scikit-learn (MOOC) Use as the free, more thorough follow-on after this guided first model. Its static course and notebooks are openly available; the materials are published under CC BY.
- Model persistence Read the security and version-compatibility warnings before saving an estimator. Treat the training code, package versions, data split and metrics as the reproducible artifact, not just a serialized model file.