7 routes42 sections324 units

Unit 2.3 · about 3 h

7 / 324

Build a preprocessing pipeline and first model

A model is more than an estimator call. Real datasets require consistent transformations, and a pipeline makes the preprocessing/model contract explicit while reducing a major source of leakage.

  • implement
  • measure

You can now

  • Fit a DummyRegressor baseline on the training partition.
  • Build a ColumnTransformer for categorical and numeric features.
  • Chain preprocessing and an estimator in a scikit-learn Pipeline.
  • Fit the pipeline only on training data and generate test predictions.
  • Use only the calendar feature contract: season, mnth, hr, holiday, weekday and workingday; keep observed weather measurements outside the model pipeline.

Covers

  • Preprocessing pipelines and baseline estimators

Study, in this order

3 steps

  1. Intro to Machine Learning
    Kaggle Learn — Intro to Machine Learning

    Complete How Models Work, Your First Machine Learning Model and Model Validation; then inspect the random-forest lesson.

  2. Pipelines and composite estimators
    Pipelines and composite estimators

    Study Pipeline and ColumnTransformer and build one preprocessing-and-regression pipeline.

  3. Machine Learning Crash Course
    Google Machine Learning Crash Course

    Use the regression and numerical/categorical data modules as conceptual support for the feature transformations.

Focus on

  • DummyRegressor as a transparent baseline
  • ColumnTransformer for separate numeric and categorical columns
  • OneHotEncoder for categorical features
  • Pipeline fit/predict boundaries
  • Calendar-only feature contract based on information available at the start of the hour

Reproduce closed-book

Fit a mean DummyRegressor and a RandomForestRegressor pipeline on the same training partition using only season, mnth, hr, holiday, weekday and workingday. Keep transformations inside the pipeline and generate one prediction vector for the untouched test partition.

Traps

  • Do not fit preprocessing outside the pipeline on all rows.
  • Do not compare models evaluated on different splits.
  • Do not assume a more complex model is better without a held-out improvement.

Practice

Kaggle Intro to Machine Learning exercises

Build the first estimator in a fresh notebook, then adapt the pattern to the UCI bike-sharing feature contract.

Start with a simple estimator; don't tune hyperparameters yet.

Self-test

  1. What does ColumnTransformer provide in this project?

  2. Why is the baseline fitted on the same training partition as the model?

Completion criterion

Stop when

The baseline and calendar-only model pipeline fit without reading the test target, and the model produces one prediction per test observation.

Mark passed, then open 2.4 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • 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.

Also in this section

  • 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.
  • 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.
  • 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.
  • 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