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
PipelineandColumnTransformer. - 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
joblibfor 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 features | Raw images, audio, or long text sequences |
| Dataset fits in memory and needs fast baselines | You need representation learning at scale |
| Interpretability and simple deployment matter | State-of-the-art accuracy on unstructured data is required |
| You need robust preprocessing pipelines | You 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.
Pipelines Prevent Leakage
Wrap preprocessing and modeling in a single Pipeline so cross-validation applies transformations correctly on each fold.
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.
Cross-Validation and Hyperparameter Tuning
Metrics That Match the Business Problem
| Task | Common Metrics | When Accuracy Misleads |
|---|---|---|
| Binary classification | Precision, recall, F1, ROC-AUC | Rare positive class (fraud, disease) |
| Multi-class | Macro/micro F1, confusion matrix | Imbalanced classes across categories |
| Regression | MAE, RMSE, R² | Outliers dominate MSE |
| Ranking | NDCG, average precision | Top-k quality matters more than overall accuracy |
Reality: CV scores on historical data do not guarantee performance under distribution shift. Monitor live metrics, calibrate thresholds, and retrain on schedule.
Model Persistence
- 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
- True/False: Every sklearn object shares
fit/predictortransform, so you can swap algorithms without rewriting glue code. Answer: True - Multiple Choice: In the
StandardScalerexample, why isfit_transformused only onX_train? Answer: Fit on train only so test data uses train statistics and avoids leakage;X_testusestransformonly - Short Answer: Why wrap preprocessing and the model in a
Pipeline? Answer: So cross-validation applies transformations correctly on each fold and prevents leakage - 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
- Multiple Choice: What does
ColumnTransformerdo here? Answer: Routes numeric columns throughStandardScalerand categorical columns throughOneHotEncoder, then concatenates the results - Short Answer: How does
GridSearchCVrefer to pipeline hyperparameters in the example? Answer: With double-underscore names such asmodel__n_estimatorsandmodel__max_depth - 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
- 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
- Short Answer: How does this lecture persist a trained model for deployment? Answer:
joblib.dump(model, "churn_model.joblib")andjoblib.loadto 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.
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.