← Master Index
Vol. 03 Module 3.3 Lecture

Scikit-learn

AI & Data Libraries

How This Lesson Fits the Module

With NumPy arrays and Pandas tables prepared, you need a library that packages preprocessing, model training, and evaluation into reproducible pipelines. Scikit-learn is the standard for classical ML on tabular data—and the conceptual template that deep-learning engineers still follow (fit/transform, train/val split, metrics).

Many production systems combine sklearn preprocessing with PyTorch or TensorFlow models. Learn sklearn first; it teaches the engineering discipline of ML.

Learning Objectives

By the end of this lesson, students should be able to:

  • Build end-to-end ML pipelines with Pipeline and ColumnTransformer.
  • Train and evaluate classifiers and regressors with consistent APIs.
  • Perform cross-validation and hyperparameter search responsibly.
  • Select appropriate metrics for classification and regression tasks.
  • Explain when sklearn is sufficient versus when deep learning is warranted.
  • Serialize trained models with joblib for deployment.

What Scikit-learn Is—and When to Use It

Scikit-learn provides consistent fit / predict / transform interfaces for preprocessing, feature extraction, and supervised/unsupervised models. It runs on CPU with NumPy arrays and excels on structured, tabular problems up to millions of rows.

Use Scikit-learn when…Reach for deep learning when…
Tabular data with hand-crafted featuresRaw images, audio, or long text sequences
Dataset fits in memory and needs fast baselinesYou need representation learning at scale
Interpretability and simple deployment matterState-of-the-art accuracy on unstructured data is required
You need robust preprocessing pipelinesYou need GPU training and autograd

The Estimator API

Every sklearn object shares the same contract: fit(X, y) learns from data; predict(X) or transform(X) applies what was learned. This uniformity lets you swap algorithms without rewriting glue code.

from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression from sklearn.metrics import classification_report X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, stratify=y, random_state=42 ) scaler = StandardScaler() X_train_s = scaler.fit_transform(X_train) # fit on train only X_test_s = scaler.transform(X_test) # apply train stats clf = LogisticRegression(max_iter=1000) clf.fit(X_train_s, y_train) y_pred = clf.predict(X_test_s) print(classification_report(y_test, y_pred))

Pipelines Prevent Leakage

Wrap preprocessing and modeling in a single Pipeline so cross-validation applies transformations correctly on each fold.

from sklearn.pipeline import Pipeline from sklearn.ensemble import RandomForestClassifier pipe = Pipeline([ ("scaler", StandardScaler()), ("model", RandomForestClassifier(n_estimators=200, random_state=42)), ]) pipe.fit(X_train, y_train) print(pipe.score(X_test, y_test))
Definition — Baseline First

A baseline model is the simplest reasonable approach—often logistic regression or a shallow tree ensemble. Always establish a sklearn baseline before investing in deep learning. If the baseline is within business tolerance, you may already be done.

Mixed Feature Types with ColumnTransformer

Real datasets combine numeric and categorical columns. Route each type through appropriate transformers, then concatenate results.

from sklearn.compose import ColumnTransformer from sklearn.preprocessing import OneHotEncoder numeric_features = ["tenure_months", "monthly_spend"] categorical_features = ["plan_type", "region"] preprocessor = ColumnTransformer([ ("num", StandardScaler(), numeric_features), ("cat", OneHotEncoder(handle_unknown="ignore"), categorical_features), ]) model = Pipeline([ ("prep", preprocessor), ("clf", LogisticRegression(max_iter=1000)), ]) model.fit(df_train, y_train)

Cross-Validation and Hyperparameter Tuning

from sklearn.model_selection import cross_val_score, GridSearchCV scores = cross_val_score(pipe, X_train, y_train, cv=5, scoring="f1") print(f"CV F1: {scores.mean():.3f} ± {scores.std():.3f}") param_grid = {"model__n_estimators": [100, 200], "model__max_depth": [None, 8, 16]} search = GridSearchCV(pipe, param_grid, cv=3, scoring="f1", n_jobs=-1) search.fit(X_train, y_train) print(search.best_params_)

Metrics That Match the Business Problem

TaskCommon MetricsWhen Accuracy Misleads
Binary classificationPrecision, recall, F1, ROC-AUCRare positive class (fraud, disease)
Multi-classMacro/micro F1, confusion matrixImbalanced classes across categories
RegressionMAE, RMSE, R²Outliers dominate MSE
RankingNDCG, average precisionTop-k quality matters more than overall accuracy
Common Misconception: “Higher cross-validation score always means better production model.”

Reality: CV scores on historical data do not guarantee performance under distribution shift. Monitor live metrics, calibrate thresholds, and retrain on schedule.

Model Persistence

import joblib joblib.dump(model, "churn_model.joblib") loaded = joblib.load("churn_model.joblib") loaded.predict(new_customers_df)
  1. Short Answer: When should you use scikit-learn rather than deep learning, according to this lecture? Answer: Tabular data with hand-crafted features, in-memory datasets needing fast baselines, interpretability or simple deployment, and robust preprocessing pipelines—not raw images, audio, or long text that need GPU training and autograd
  2. True/False: Every sklearn object shares fit / predict or transform, so you can swap algorithms without rewriting glue code. Answer: True
  3. Multiple Choice: In the StandardScaler example, why is fit_transform used only on X_train? Answer: Fit on train only so test data uses train statistics and avoids leakage; X_test uses transform only
  4. Short Answer: Why wrap preprocessing and the model in a Pipeline? Answer: So cross-validation applies transformations correctly on each fold and prevents leakage
  5. True/False: A baseline model in this lesson is often logistic regression or a shallow tree ensemble, and you should establish it before deep learning. Answer: True
  6. Multiple Choice: What does ColumnTransformer do here? Answer: Routes numeric columns through StandardScaler and categorical columns through OneHotEncoder, then concatenates the results
  7. Short Answer: How does GridSearchCV refer to pipeline hyperparameters in the example? Answer: With double-underscore names such as model__n_estimators and model__max_depth
  8. True/False: Higher cross-validation score always means a better production model. Answer: False — CV on historical data does not guarantee performance under distribution shift; monitor live metrics and retrain
  9. Multiple Choice: When does accuracy especially mislead in binary classification? Answer: Rare positive class (for example, fraud or disease)—use precision, recall, F1, or ROC-AUC instead
  10. Short Answer: How does this lecture persist a trained model for deployment? Answer: joblib.dump(model, "churn_model.joblib") and joblib.load to reload and predict

Key Takeaways

  • Scikit-learn is the go-to library for tabular ML baselines and preprocessing pipelines.
  • The fit/transform/predict API enforces correct train/test separation.
  • Always establish a baseline before deep learning.
  • Choose metrics aligned with business costs, not convenience.
  • Next: PyTorch for differentiable programming and neural networks.
Trainer’s Guide

Capstone sketch: Churn prediction with ColumnTransformer + RandomForest. Require CV scores, a confusion matrix plot, and a written paragraph on precision vs recall for customer retention.

Recap: Scikit-learn gives you fit/transform pipelines, baselines, CV, and metrics for tabular ML—next, learn differentiable neural nets in PyTorch.