Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Machine Learning Cheat Sheet

Supervised Learning

AlgorithmTypeProsConsUse When
Linear RegressionRegressionSimple, interpretableAssumes linearityBaseline, linear relationships
Logistic RegressionClassificationFast, probabilisticLinear boundaryBinary/multiclass baseline
Decision TreesBothInterpretable, no scalingOverfittingExplainability needed
Random ForestBothReduces overfittingSlow inferenceGeneral purpose
XGBoostBothState-of-art tabularHarder to tuneCompetitions, tabular data
LightGBMBothFast, memory efficientSensitive to hyperparamsLarge datasets
SVMBothWorks in high-dimSlow on large dataSmall-medium data, high-dim
KNNBothSimple, no trainingSlow inference, memorySmall data, similarity-based
Naive BayesClassificationFast, works with little dataFeature independenceText classification

Key Formulas

Accuracy  = (TP + TN) / (TP + TN + FP + FN)
Precision = TP / (TP + FP)          → "Of predicted positive, how many correct?"
Recall    = TP / (TP + FN)          → "Of actual positive, how many found?"
F1        = 2 × (Precision × Recall) / (Precision + Recall)
AUC-ROC   = Area under TPR vs FPR curve

MSE  = (1/n) Σ(y - ŷ)²
MAE  = (1/n) Σ|y - ŷ|
RMSE = √MSE
R²   = 1 - (SS_res / SS_tot)
Cross-Entropy = -Σ yᵢ log(ŷᵢ)

Bias-Variance Tradeoff

Total Error = Bias² + Variance + Irreducible Error

High Bias → Underfitting → More complex model
High Variance → Overfitting → Regularization, more data

Regularization

TypeFormulaEffect
L1 (Lasso)+ λΣ|wᵢ|Feature selection (sparse)
L2 (Ridge)+ λΣwᵢ²Small weights, smooth
Elastic NetL1 + L2Combined
DropoutRandom neurons offEnsemble effect
Early StopStop before overfitImplicit regularization

Feature Engineering

  • Scaling: StandardScaler (μ=0, σ=1), MinMaxScaler (0-1), RobustScaler (median/IQR)
  • Encoding: One-hot (nominal), Label/Ordinal (ordinal), Target encoding (high cardinality)
  • Missing: Mean/median/mode imputation, KNN imputation, indicator variable
  • Selection: Mutual information, chi-squared, correlation filter, RFE

Cross-Validation

K-Fold: Split into K folds, train on K-1, test on 1, rotate
Stratified: Preserves class distribution
Leave-One-Out: K = n (expensive)
Time Series: Always forward-looking (no future leakage)

Evaluation Metrics Quick Reference

MetricRangeBestUse Case
Accuracy0-11Balanced classes
Precision0-11Cost of false positive high
Recall0-11Cost of false negative high
F10-11Imbalanced classes
AUC-ROC0-11Ranking, threshold-independent
Log Loss0-∞0Probabilistic predictions
RMSE0-∞0Regression (penalizes large errors)
MAE0-∞0Regression (robust to outliers)

Ensemble Methods

Bagging: Train models on random subsets → aggregate (Random Forest)
Boosting: Train sequentially, each fixing previous errors (XGBoost, LightGBM)
Stacking: Train meta-model on base model predictions
Voting: Hard (majority) or Soft (average probabilities)

Interview Quick Tips

  1. Always ask: classification or regression? Supervised or unsupervised?
  2. Start simple (logistic regression), then complex
  3. Check for data leakage (future info, target-derived features)
  4. Handle class imbalance: SMOTE, class weights, undersampling, threshold tuning
  5. Feature importance: tree-based → impurity; linear → coefficients