scikit-learn

scikit-learn is Python's standard library for classical machine learning — regression, classification, clustering, and the preprocessing and evaluation machinery around them. Every model shares one interface (fit, predict, transform), which makes swapping algorithms, composing pipelines, and running fair comparisons almost mechanical.

Deep learning gets the headlines, but most production ML on tabular business data — churn prediction, pricing, fraud scoring, lead ranking — is gradient-boosted trees and linear models, and scikit-learn (plus its ecosystem partners XGBoost/LightGBM) is where that work happens.

TL;DR

Quick Example

A complete, leak-free workflow — preprocessing, model, and evaluation in one pipeline:

Because preprocessing lives inside the pipeline, cross-validation refits the scaler and encoder on each fold's training part automatically — no leakage.

Core Concepts

The Estimator API

X is a 2-D table of features (a Pandas DataFrame or NumPy array); y is the target column.

Pipelines and ColumnTransformer

A Pipeline makes "preprocessing + model" a single estimator: it fits transformers on training data and applies them consistently at prediction time. ColumnTransformer routes different columns through different steps (scale numerics, one-hot encode categoricals). Together they:

Data Leakage — the Bug That Inflates Every Score

Leakage is training-time access to information unavailable at prediction time. The common forms:

Other classics: imputing with the full dataset's mean, target-encoding before splitting, and using features that are consequences of the label (e.g. "account_closed_date" when predicting churn). If your score looks too good, suspect leakage first.

Choosing a Model

A pragmatic ladder for tabular problems:

Deep learning (PyTorch/TensorFlow) wins on images, audio, and text — on modest tabular datasets, boosted trees typically still win.

Tuning

step__param names reach inside pipelines. Prefer randomized/Bayesian search over exhaustive grids; tune on CV, and keep the test set untouched until the end.

Evaluation Essentials

Deeper treatment in Model Evaluation.

Common Mistakes

Preprocessing Outside the Pipeline

Fitting scalers/encoders on the full dataset before splitting silently leaks test information into training. Put every fitted step in the Pipeline.

Evaluating on Training Data

model.score(X_train, y_train) measures memorization. Trees in particular will show near-perfect training scores while generalizing poorly — always report held-out or cross-validated numbers.

Tuning Against the Test Set

Every decision made by peeking at test scores turns the test set into a second training set. Tune with CV on the training portion; open the test set once.

Ignoring predict_proba and Thresholds

predict uses a 0.5 cutoff that's rarely optimal for business costs. Use probabilities and choose the threshold against your actual precision/recall trade-off.

FAQ

scikit-learn or PyTorch/TensorFlow?

Different jobs. scikit-learn covers classical ML on tabular data with minimal code; PyTorch and TensorFlow build neural networks for unstructured data (images, audio, language). Most tabular business problems are scikit-learn (or XGBoost) problems.

Where do XGBoost and LightGBM fit?

Separate libraries implementing gradient-boosted trees, exposing scikit-learn-compatible estimators — they drop into Pipelines and searches unchanged. scikit-learn's own HistGradientBoosting* is in the same family and often just as strong.

Can scikit-learn use my GPU?

Mostly no — it's CPU-oriented by design. NVIDIA's cuML mirrors the API on GPUs, and boosted-tree libraries have GPU modes; but for typical tabular sizes, CPU scikit-learn is fast enough.

How do I deploy a scikit-learn model?

Persist the whole pipeline with joblib, serve it behind a FastAPI endpoint, and pin library versions (pickles are version-sensitive). Versioning, monitoring, and retraining are MLOps territory.

How much math do I need?

To use it well: understand train/test discipline, leakage, and your evaluation metric. The library handles the optimization; your judgment handles the experiment design — that's where projects actually fail.

Related Topics

References