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
- One API for every model:
estimator.fit(X, y)trains,estimator.predict(X)infers, transformers addtransform(X). - Split before you touch anything: evaluate on data the model never saw (
train_test_split, then cross-validation). - Pipelines chain preprocessing + model into one object — the single best habit for correctness, because they prevent data leakage.
- Fit preprocessing on training data only; apply it to test data. Fitting a scaler on the full dataset is the classic leakage bug.
- For tabular data, start with a gradient-boosted tree (
HistGradientBoostingClassifier) and a linear baseline — beat those before anything fancier. - Accuracy is often the wrong metric — on imbalanced data use precision/recall, F1, or ROC-AUC.
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:
- eliminate train/test preprocessing skew,
- make cross-validation honest,
- give you one artifact to persist and deploy.
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
- Stratify splits on classification (
stratify=y) so class ratios hold. - Imbalanced classes: accuracy lies (99% accuracy on 1% fraud = useless). Use
classification_report, ROC-AUC or average precision, and considerclass_weight="balanced". - Regression: MAE is interpretable, RMSE punishes large errors; check residuals, not just the score.
- Time series: never random-split — use
TimeSeriesSplitso training always precedes testing.
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
- Feature Engineering — Preparing the
Xthat models learn from - Model Evaluation — Metrics and validation strategy in depth
- Pandas — The DataFrames feeding
fit - Python for Data Science — The surrounding stack
- MLOps — Deploying and monitoring trained models
- PyTorch — When the problem needs deep learning