Lesson 12: Scikit-learn Hands-on Machine Learning

AI and Machine Learning Course from Scratch ~8 min read 60 min hands-on

Introduction

Scikit-learn is easy to learn in fragments: one notebook on preprocessing, another on pipelines, another on saving models. Each exercise works in isolation. None of them answer the question backend engineers actually face: how do estimators, transformers, validators, and persistence live together behind one API, one evaluation contract, and one product surface you could ship?

week_12_aiml_integrated_project closes that gap. It is a self-contained capstone that unifies six sklearn topics into one repository:

  • An installable Python package (week12_python) holding domain logic for ecosystem demos, tabular preprocessing, pipeline composition, feature engineering, model persistence, and end-to-end tabular workflows

  • A composition layer (core/) that picks canonical implementations for production pathsβ€”preprocessing, persistence, validation, and feature primitives

  • A product workflow (product/titanic_pipeline.py) that runs a full Titanic survival train-and-predict path on top of those choices

  • A FastAPI backend exposing two API surfaces: a lab path for per-topic exploration and a product path for composed ML workflows

  • An optional React frontendβ€”Scikit-learn Studioβ€”for live telemetry, training actions, registry inspection, and module health checks

The goal is system-first learning. ColumnTransformer is not just a classroom exerciseβ€”it is the canonical preprocessor inside a persisted sklearn Pipeline. Model bundles are not ad-hoc picklesβ€”they are joblib artifacts with JSON metadata sidecars, listed through a registry API.

For engineers, the payoff is traceability. Titanic training is not a one-off scriptβ€”it is POST /api/v1/ml/titanic/train with validation, cross-validation metrics, a model_key, request IDs, and timing metadata recorded in runtime telemetry.


Core Components

The stack is layered. Each topic below is a distinct building block; together they form one coherent tabular ML system.

Scikit-learn ecosystem demo

Demonstrates the sklearn mental model: custom transformers, Pipeline, cross-validation, and regression metrics on synthetic movie-rating data.

MovieRecommendationDataset generates tabular ratings with user and movie metadata. UserMovieFeatureTransformer implements BaseEstimator / TransformerMixin to create interaction and temporal features. SklearnEcosystemPipeline chains feature creation, scaling, and RandomForestRegressor training with optional CV.

The week12_apis module exposes api_d71_train, returning dataset summary, feature count, RMSE/MAE/RΒ² metrics, and optional CV RMSEβ€”all as plain dicts.

Tabular preprocessing

DataPreprocessor builds a ColumnTransformer with numeric imputation plus scaling (standard, minmax, or robust) and categorical one-hot encoding with handle_unknown='ignore'. MissingDataHandler analyzes missing-value patterns before transformation.

api_d72_fit_transform accepts tabular records plus column lists and returns a missing-value report, transformed shape, and feature names. This module is the source implementation behind core/preprocessing.py, which supplies the canonical preprocessor for the product Titanic workflow.

Pipeline composition

api_d73_column_transformer_demo generates deterministic synthetic demographic data, fits the shared DataPreprocessor, and returns original vs transformed shapes plus positive-class rate. It teaches ColumnTransformer wiring without requiring client-supplied records.

Feature engineering primitives

PolynomialFeatureCreator, FeatureBinner, FeatureSelector, and FeatureEngineeringPipeline cover polynomial expansion, binning, and selection utilities. api_d74_poly_features accepts a numeric matrix and returns expanded shape plus optional feature names.

core/features.py surfaces these primitives for optional use in composed workflows; the Titanic product path uses the canonical preprocessor rather than polynomial expansion.

Model persistence and registry

Keep going with the full lesson

The rest of this lesson β€” plus its implementation guide, runnable scripts and source code β€” is included with the course or an All Access plan. Create a free account to start the free lessons in every course.

Already a member? Sign in