Chapter 4

Build a First Machine Learning Pipeline

Presentation & objectives

This session focuses on building your first complete Machine Learning pipeline: understanding a real-world dataset, training a baseline model, evaluating it honestly across two cities, and turning working notebook code into a small, tested Python package. You will work with real PM2.5 air quality measurements from African cities, combining satellite data with ground-level pollution readings.

Knowledge Check

Answer these questions once you have finished this course module.

--- primary_color: steelblue secondary_color: lightgray text_color: black shuffle_questions: false shuffle_answers: true --- # Why is training on one city and testing on another more informative than a random train/test split here? 1. [ ] It reduces the number of rows needed for training 2. [x] It checks whether the model generalizes to a location it has never seen, instead of memorizing city-specific patterns 3. [ ] It automatically improves model accuracy 4. [ ] It removes the need for missing-value handling # Why should missing values be filled within each city separately rather than globally? 1. [ ] Filling globally is faster to compute 2. [x] Filling globally could use one city's measurements to fill another city's gaps, which is not physically meaningful 3. [ ] Pandas does not support filling by group 4. [ ] It only matters for categorical columns # Why do site_latitude and site_longitude cause a linear model to fail badly when generalizing to a new city? 1. [ ] They are not numeric columns 2. [x] Within one city they vary very little, so the model fits an oversized coefficient that explodes on a city with very different coordinates 3. [ ] They are always missing 4. [ ] They are perfectly correlated with the target # What is the main limitation of notebooks that motivates extracting code into Python modules? 1. [ ] Notebooks cannot use pandas or scikit-learn 2. [x] Notebook cells depend on execution order and hidden state, making the same logic hard to reuse and verify 3. [ ] Notebooks cannot produce plots 4. [ ] Notebooks require a paid license # After extracting pipeline code into functions, what should you check in the notebook? 1. [ ] That the notebook no longer runs 2. [x] That importing and calling the new functions reproduces the exact same metrics as before 3. [ ] That the functions are renamed to match the notebook variable names 4. [ ] That the original cells are kept alongside the new imports # What role do the provided pytest tests play in this course? 1. [ ] They generate a cryptographic proof of completion 2. [x] They give concrete feedback on which behavior does not match yet 3. [ ] They automatically fix incorrect code 4. [ ] They replace the need for a Git commit history