1. Cheat sheets
  2. Data

For Data Scientist, AI Engineer and Data Analyst

9 minReference

Data science and ML cheat sheet

Machine learning from framing to production on a few screens: how to frame the problem, split without leaking, pick a model and a metric, forecast, explain a prediction, and keep a model healthy once it is live. Code is scikit-learn. Keep it open while you plan or review a model.

Frame it before you fit it

Most failed models answer the wrong question. Write these down first (Framing).

  • Decision: what changes when the model speaks? "Send a win-back voucher to customers likely to stop ordering in the next 30 days."
  • Unit and moment: one prediction per customer, made every Monday with data up to Sunday night.
  • Target: exactly defined. "No app order in the 30 days after the prediction date."
  • Baseline: the rule you must beat, such as "no order in the last 21 days".
  • Cost of errors: a wasted voucher costs $2; a lost customer costs about $40 of margin a year.
  • Success metric: a business result (customers kept per dollar), not only AUC.

Problem type: "I want to..."

Start with the simplest model that could work, and keep it as the baseline the complex one must beat (Models).

I want to...ProblemStart withLesson
Predict yes or no (will churn, is fraud)ClassificationLogistic regression, then gradient boosted treesModels, Churn
Predict a number (basket size, delivery minutes)RegressionLinear regression, then gradient boosted treesModels
Predict demand per store per dayForecastingSeasonal naive (same day last week), then a model with calendar featuresForecasting
Group similar stores or customersClusteringk-means on scaled featuresModels
Order items by relevanceRankingPopularity per segment, then a learned rankerModels
Flag unusual till totalsAnomaly detectionRobust z-score per store, then isolation forestModels
Classify or extract from free textNLPAn LLM prompt with evals, or TF-IDF plus logistic regressionLLM APIs

Splits and leakage

Leakage is when the model sees, during training, information it will not have at prediction time. It gives great test scores and useless predictions (Leakage).

SituationSplit this wayLesson
Rows are independenttrain_test_split(X, y, test_size=0.2, stratify=y, random_state=42)Leakage
Anything with timeTrain on the past, test on a later period; TimeSeriesSplit for cross-validationLeakage, Forecasting
Many rows per customer or storeKeep each group on one side: GroupKFoldLeakage
Tuning hyperparametersTrain, validation and a test set you touch once at the endModels

Leakage you will meet

  • A feature computed after the prediction moment: "refund issued" when predicting a bad delivery.
  • Scaling or imputing on all the data before the split. Fit those steps on training data only, inside a Pipeline.
  • Target encoding computed on the full dataset, which lets each row see its own label.
  • Duplicates of one order on both sides of the split.
  • A feature that is a proxy for the label, such as "account closed date" for churn.

Rule: for every feature, ask "would I know this value at 23:59 on Sunday, when the prediction is made?" (Leakage).

A pipeline that cannot leak

Put every preprocessing step inside the pipeline, so cross-validation refits it on each training fold (scikit-learn 1.2 and later; Models, Pipelines).

Python
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.impute import SimpleImputer
from sklearn.ensemble import HistGradientBoostingClassifier
from sklearn.model_selection import TimeSeriesSplit, cross_val_score

num = ["orders_90d", "days_since_last", "avg_basket_cents"]
cat = ["city", "home_store"]

prep = ColumnTransformer([
    ("num", Pipeline([("imp", SimpleImputer(strategy="median")),
                      ("sc", StandardScaler())]), num),
    ("cat", OneHotEncoder(handle_unknown="ignore", sparse_output=False), cat),
])
model = Pipeline([("prep", prep), ("clf", HistGradientBoostingClassifier())])

scores = cross_val_score(model, X, y, cv=TimeSeriesSplit(n_splits=5),
                         scoring="average_precision")
print(scores.mean(), scores.std())

Which metric

Pick the metric from the cost of each kind of mistake, then report it on the test set beside the baseline (Models, Churn).

MetricMeansUse whenscikit-learn
PrecisionOf those flagged, the share that were rightFalse alarms are costly (vouchers, fraud holds)precision_score
RecallOf the real cases, the share you caughtMisses are costly (food safety, fraud losses)recall_score
F1Balance of precision and recallYou need one number and both matterf1_score
ROC AUCChance a random positive ranks above a random negativeComparing rankers, classes not too rareroc_auc_score
PR AUC (average precision)Precision across all recall levelsRare positives, such as 2% fraudaverage_precision_score
AccuracyShare of all predictions that were rightBalanced classes only; 98% is easy with 2% fraudaccuracy_score
MAEAverage absolute error, in real unitsEasy to explain: "off by 4 minutes"mean_absolute_error
RMSEPunishes big errors moreLarge misses hurt more than small onesroot_mean_squared_error (1.4+)
MAPEAverage error as a percentageComparing stores of different sizes; breaks near zeromean_absolute_percentage_error
CalibrationDo 30% predictions come true 30% of the time?Probabilities feed a cost decisionCalibrationDisplay

If you see X, reach for Y

The symptom usually tells you which side to fix: the data, the model or the setup (Models, Leakage).

You seeLikely causeTry
Test score far above anything you expectedLeakageCheck feature timestamps; split by time or group
Train score high, validation lowOverfittingSimpler model, regularisation, fewer features, more data
Both scores low and closeUnderfitting or weak featuresBetter features, a more flexible model
Great in the notebook, poor in productionTraining and serving compute features differentlyOne feature code path for both
Scores swing a lot between foldsSmall or uneven dataMore folds, report the spread, simpler model
Rare class never predictedClass imbalance with a default 0.5 thresholdChoose the threshold from costs; class_weight="balanced"
One feature dominates importanceA proxy for the label, or an IDRemove it and see what is left
Accuracy slowly drops month by monthData or concept driftMonitor inputs, retrain on a schedule

Forecasting

Forecast at the grain the decision needs: Mangrove orders stock per store per article per day, so a city-level weekly forecast does not help the buyer (Forecasting).

  • Always beat a naive baseline first: same day last week for daily retail data.
  • Features that usually matter: day of week, holidays and festival dates, paydays at month end, promotions, weather, lagged sales (last week, last year).
  • Backtest: train up to week N, forecast week N+1, move forward, repeat. Report error per week, not one average.
  • Give a range, not a single number. Stock decisions need the 90th percentile, not the mean.
  • Out-of-stock days hide true demand: sales of zero do not mean demand of zero. Flag or remove them.
  • Lag features must use only data available on the forecast day.

Explaining a model

Explain for the person who acts on it: a buyer needs "why this store, this week", a reviewer needs global importance and fairness checks (Explaining).

QuestionMethodLesson
Which features matter overall?Permutation importance on the test set: sklearn.inspection.permutation_importanceExplaining
How does the prediction move with one feature?Partial dependence: PartialDependenceDisplayExplaining
Why this one prediction?SHAP values (the shap package)Explaining
Simple enough to read whole?Logistic regression coefficients, or a shallow treeExplaining
Does it treat groups fairly?Compare error rates and outcomes per groupResponsible ML

ML in production

A model is a product with an owner, a version, inputs that drift and users who depend on it (Pipelines, Serving).

ConcernDo thisLesson
Reproducible trainingPin code, data snapshot, library versions and random seed; train from a pipeline, not a notebookPipelines
Same features in training and servingOne feature definition, computed by shared code or a feature store, with point-in-time joinsFeatures
Knowing what is liveA model registry: version, metrics, training data, approver, stageRegistry
Batch or real timeBatch scoring overnight if the decision is daily; an API only when an answer is needed in the requestServing
Safe rolloutShadow mode first, then a canary on a share of traffic, with a rollback to the previous versionServing
Input driftCompare live feature distributions with training (PSI, or simple percentiles) and alertMonitoring
Performance driftJoin predictions to outcomes when labels arrive; track the metric weeklyMonitoring
Silent failuresAlert on missing features, prediction volume and the share of default valuesMonitoring
Harm and accountabilityA model card: purpose, data, limits, fairness checks, owner, how to appeal a decisionResponsible ML

Numbers and rules of thumb

  • Test set: 10% to 20% of the data, or the most recent weeks for anything with time.
  • Population stability index (PSI): below 0.1 is usually stable, above 0.25 is a large shift. Treat these as conventions, not laws.
  • A model must beat the baseline by enough to pay for running it: monitoring, retraining and on-call.
  • Small test sets give noisy metrics: with 600 customers flagged in the test set, a precision of 40% has an interval of roughly ±4 points.
  • Retraining cadence follows how fast the world changes: weekly for demand, monthly or quarterly for churn.

Glossary

Plain definitions, with the lesson that explains each one.

Baseline
A simple rule or model that a new model must beat to be worth it. Framing
Backtesting
Testing a forecast by replaying the past: train up to a date, forecast after it, repeat. Forecasting
Calibration
How well predicted probabilities match how often things really happen. Churn
Class imbalance
When one outcome is much rarer than the other, such as fraud. Churn
Concept drift
The link between inputs and outcome changes, so an old model gets less accurate. Monitoring
Cross-validation
Training and testing several times on different splits to get a steadier score. Models
Data drift
Live inputs no longer look like the training data. Monitoring
Feature
An input column the model learns from. Features
Feature store
A shared place that computes and serves features the same way for training and serving. Features
Gradient boosting
Many small decision trees, each correcting the errors of the ones before; strong on tables. Models
Hyperparameter
A setting chosen before training, such as tree depth, tuned on validation data. Models
Label (target)
The outcome the model learns to predict. Framing
Leakage
Training information that will not be available at prediction time. Leakage
Model card
A short document of a model's purpose, data, performance, limits and owner. Responsible ML
Model registry
A catalogue of trained model versions with their metrics and approval stage. Registry
Overfitting
Learning the training data's noise, so the model does worse on new data. Models
Pipeline
Preprocessing and model chained as one object, so the same steps run every time. Pipelines
Point-in-time join
Joining each training row to feature values as they were at that row's time, never later. Features
Precision and recall
Share of flagged cases that were right, and share of real cases that were caught. Churn
Regularisation
A penalty on model complexity that reduces overfitting. Models
Shadow mode
Running a new model on live traffic without acting on its output, to compare it safely. Serving
SHAP
A method that splits one prediction into contributions from each feature. Explaining
Threshold
The score above which you act; choose it from the cost of each error. Churn
Training-serving skew
A difference between how features are built in training and in production. Features

Courses on Kolom

  • The scikit-learn user guide at scikit-learn.org, especially "Common pitfalls and recommended practices".
  • An Introduction to Statistical Learning by James, Witten, Hastie and Tibshirani, free at statlearning.com, with a Python edition.
  • Forecasting: Principles and Practice by Rob Hyndman and George Athanasopoulos, free at otexts.com/fpp3.
  • Interpretable Machine Learning by Christoph Molnar, free at christophm.github.io.
  • Designing Machine Learning Systems by Chip Huyen. Data, features, deployment and monitoring in production.