7 routes42 sections324 units

Unit 2.2 · about 3 h

6 / 324

Split 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, hum and windspeed from 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

  1. Common pitfalls and recommended practices
    Common pitfalls and recommended practices

    Read inconsistent preprocessing and data leakage, including the wrong/right examples and the Pipeline recommendation.

  2. TimeSeriesSplit
    TimeSeriesSplit

    Read why time-ordered data should not be evaluated by ordinary shuffled cross-validation.

  3. Bike Sharing Dataset
    Bike Sharing Dataset

    Use dteday only to sort and define the 2011/2012 split. Exclude instant, dteday, yr, casual, registered and cnt from 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

Sort the dataset by 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 casual or registered: together they determine cnt and would reveal the answer.
  • Do not fit imputation, encoding or scaling on the full dataset before splitting.

Practice

scikit-learn common pitfalls

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

  1. Why must casual and registered be excluded when predicting cnt?

  2. Why should the 2012 partition be left untouched until the model is selected?

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

Comes after

Resources for this unit

  • Bike Sharing Dataset
    UCI Machine Learning Repositoryrequireddataset
    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. cnt is the target. Exclude casual, registered, cnt, instant, dteday and yr from the model feature matrix; use dteday only to sort/split.
  • Common pitfalls and recommended practices
    scikit-learnrequireddocumentation
    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
    scikit-learnreferencedocumentation
    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
    Google for Developersprimarycourse
    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
    Kaggle Learnprimarycourse
    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
    Google for Developersrequiredcourse
    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
    scikit-learnrequireddocumentation
    Study Pipeline and ColumnTransformer. Keep preprocessing inside the pipeline so transformations are fitted only on the training partition and applied consistently at evaluation time.
  • Model evaluation: regression metrics
    scikit-learnreferencedocumentation
    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)
    Inriasupportcourse
    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
    scikit-learnreferencedocumentation
    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.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels