🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
💼 Servicesℹ️ About✉️ ContactView Pricing Plansfrom $10

ML Cheatsheet — Quick Reference Guide

Expert TopicsReference🟢 Free Lesson

Advertisement

Career

ML Cheatsheet — Everything You Need in One Place

Your comprehensive quick reference for machine learning concepts, algorithms, formulas, and best practices. Perfect for interviews and daily work.

  • Algorithm Summaries — Quick reference for all major ML algorithms
  • Formula Reference — Mathematical foundations at your fingertips
  • Best Practices — Proven guidelines for ML projects

"Knowledge is power, but organized knowledge is superpower."


Prerequisites

Before using this cheatsheet, make sure you're comfortable with:

  • Linear Algebra — Vectors, matrices, matrix multiplication, eigenvalues
  • Calculus — Derivatives, gradients, chain rule
  • Probability — Distributions, Bayes theorem, expected value
  • Python — NumPy, Pandas, scikit-learn basics
  • ML Fundamentals — Supervised vs unsupervised, training vs testing

Learning Objectives

After using this cheatsheet, you will be able to:

  1. Quickly look up any ML algorithm, its pros, cons, and when to use it
  2. Reference key mathematical formulas for any ML concept
  3. Choose the right model for a given problem type and data characteristics
  4. Apply proper evaluation metrics for classification and regression tasks
  5. Recall common hyperparameter ranges and tuning strategies
  6. Identify the right Python library for any ML task
  7. Apply best practices for model selection, training, and deployment
  8. Use this as a quick reference during interviews and daily work

ML Cheatsheet — Quick Reference

A comprehensive quick reference for machine learning algorithms, metrics, math, and Python code.


Algorithm Comparison Chart

Algorithm Comparison: When to Use WhatAlgorithmTypeProsConsBest ForLinear RegressionLinearSimple, interpretableAssumes linearityBaseline, interpretableLogistic RegressionLinearProbabilities, fastLinear boundaryBinary classificationRandom ForestEnsembleRobust, handles missingLess interpretableTabular data defaultXGBoostEnsembleBest accuracy, fastHyperparameter sensitiveCompetitions, tabularSVMKernelEffective in high-dSlow on large dataSmall-medium datasetsNeural NetworkDeepUniversal approximatorNeeds lots of dataImages, text, speechK-MeansClusteringSimple, scalableMust specify KCustomer segmentationDBSCANClusteringFinds arbitrary shapeStruggles with densityAnomaly detection

Decision Tree: Model Selection

Model Selection Decision TreeWhat type of problem?Supervised (labeled data)Unsupervised (no labels)ClassificationRegressionBinaryLogReg, SVM, XGBMulti-classRF, XGB, NNContinuousLinReg, XGB, NNClusteringDim. ReductionK-MeansKnown KDBSCANUnknown KPCA, t-SNEVisualizationGolden RulesStart simple (linear) then add complexity if needed. Feature engineering beats algorithm choice. Cross-validate everything.

Classification Metrics

MetricFormulaWhen to Use
Accuracy(TP+TN)/(TP+TN+FP+FN)Balanced classes
PrecisionTP/(TP+FP)Cost of false positive is high (spam)
RecallTP/(TP+FN)Cost of false negative is high (cancer)
F1 Score2 * (P * R) / (P + R)Imbalanced classes
AUC-ROCArea under ROC curveRanking quality
Log Loss-1/N * sum[y*log(p) + (1-y)*log(1-p)]Probabilistic predictions

Regression Metrics

MetricFormulaInterpretation
MSE1/N * sum(y_i - y_hat_i)^2Penalizes large errors
RMSEsqrt(MSE)Same units as target
MAE1/N * sum(abs(y_i - y_hat_i))Robust to outliers
R21 - SS_res / SS_totVariance explained (0-1)
MAPE100%/N * sum(abs((y-y_hat)/y))Percentage error

Math Quick Reference


Python Libraries

  • Data: pandas, numpy
  • Visualization: matplotlib, seaborn, plotly
  • ML: scikit-learn, xgboost, lightgbm
  • Deep Learning: pytorch, tensorflow, keras
  • NLP: transformers, spacy, nltk
  • CV: opencv, torchvision
  • AutoML: auto-sklearn, optuna
  • Deployment: fastapi, flask, streamlit
  • Experiment: mlflow, wandb

Real-World Applications

6+ Detailed Use Cases

1. Tabular Data (Structured)

  • Dataset type: CSV, database tables
  • Best algorithms: XGBoost, LightGBM, Random Forest
  • Key considerations: Feature engineering, missing values, class imbalance
  • Common metrics: AUC-ROC, F1, precision/recall

2. Image Classification (Computer Vision)

  • Dataset type: JPEG, PNG images
  • Best algorithms: ResNet, EfficientNet, Vision Transformer
  • Key considerations: Data augmentation, transfer learning, GPU memory
  • Common metrics: Top-1 accuracy, mAP, F1 per class

3. Natural Language Processing (Text)

  • Dataset type: Text documents, reviews, tweets
  • Best algorithms: BERT, GPT, RoBERTa
  • Key considerations: Tokenization, context length, domain adaptation
  • Common metrics: BLEU, ROUGE, accuracy, F1

4. Time Series Forecasting

  • Dataset type: Sequential numerical data
  • Best algorithms: ARIMA, Prophet, LSTM, Temporal Fusion Transformer
  • Key considerations: Seasonality, trends, lookback window
  • Common metrics: MAE, RMSE, MAPE, coverage

5. Recommendation Systems

  • Dataset type: User-item interactions
  • Best algorithms: Collaborative filtering, matrix factorization, neural embeddings
  • Key considerations: Cold start, scalability, diversity vs relevance
  • Common metrics: NDCG, MAP, hit rate

6. Anomaly Detection

  • Dataset type: Transaction logs, sensor data
  • Best algorithms: Isolation Forest, Autoencoders, DBSCAN
  • Key considerations: Extreme class imbalance, concept drift
  • Common metrics: AUC-ROC, precision at k, F1

7. Reinforcement Learning

  • Dataset type: State-action-reward sequences
  • Best algorithms: Q-Learning, PPO, SAC
  • Key considerations: Exploration vs exploitation, reward shaping
  • Common metrics: Cumulative reward, episode length, success rate

Common Mistakes and How to Avoid Them

5+ Common Mistakes

1. Not Scaling Features for Distance-Based Algorithms

  • Mistake: Using raw features with KNN, SVM, or K-Means
  • Solution: Always scale (StandardScaler, MinMaxScaler) for distance-based methods
  • Impact: Features with larger scales dominate distance calculations

2. Using Accuracy on Imbalanced Datasets

  • Mistake: Reporting accuracy when 95% of data is one class
  • Solution: Use F1, AUC-ROC, precision/recall curves instead
  • Impact: 95% accuracy can be achieved by always predicting the majority class

3. Not Cross-Validating

  • Mistake: Trusting a single train/test split
  • Solution: Use 5-fold or 10-fold cross-validation for all model comparisons
  • Impact: Single splits can be misleading due to random variation

4. Data Leakage from Future to Past

  • Mistake: Using future information in features for time series
  • Solution: Split by time, use point-in-time correct features
  • Impact: Models appear to work in training but fail in production

5. Not Handling Missing Values Properly

  • Mistake: Dropping all rows with missing values or using naive imputation
  • Solution: Analyze missingness patterns; use appropriate imputation (median, KNN, model-based)
  • Impact: Loss of data or biased imputation affects model performance

6. Over-Tuning Hyperparameters on Test Set

  • Mistake: Iteratively tuning on test set until performance improves
  • Solution: Use validation set for tuning; test set only for final evaluation
  • Impact: Overfitting to test set gives unrealistic performance estimates

Comparison Table

Algorithm Quick Selection Guide

ScenarioFirst ChoiceAlternativeAvoid
Small dataset (<1K rows)Logistic RegressionSVM, KNNDeep Learning
Large dataset (>100K rows)XGBoost/LightGBMRandom ForestSVM
Image dataCNN (ResNet/EfficientNet)Vision TransformerRandom Forest
Text dataBERT/RoBERTaTF-IDF + LogRegNaive Bayes
Time seriesXGBoost + featuresLSTM, ProphetRandom split
Interpretability neededDecision Tree, LogRegSHAP with any modelBlack-box ensemble

Interview Questions

7 Quick-Fire Cheatsheet Questions

Q1: When do you use L1 vs L2 regularization? A: L1 for feature selection (sparse models, zeros out weights). L2 for smooth weights when all features matter. Elastic Net when you want both.

Q2: What is the difference between bagging and boosting? A: Bagging trains models in parallel on bootstrapped samples and averages predictions (reduces variance). Boosting trains models sequentially, each correcting the previous one's errors (reduces bias).

Q3: How do you handle missing data? A: (1) Analyze missingness patterns, (2) Mean/median/mode imputation for simple cases, (3) KNN or model-based imputation for complex patterns, (4) Add missing indicator feature, (5) Use algorithms that handle missing values (XGBoost, LightGBM).

Q4: What is the curse of dimensionality? A: As the number of features increases, the data becomes increasingly sparse in high-dimensional space. Distance metrics become less meaningful, more data is needed, and models are more prone to overfitting. Solutions: feature selection, dimensionality reduction (PCA), regularization.

Q5: Explain precision vs recall with an example. A: Precision = TP/(TP+FP) -- of all predicted positives, how many are correct? Recall = TP/(TP+FN) -- of all actual positives, how many did we catch? Example: Email spam filter -- high precision (do not mark legitimate emails as spam) vs cancer detection -- high recall (do not miss any cancers).

Q6: What is cross-validation and why use it? A: Cross-validation splits data into K folds, trains on K-1 folds, and tests on the remaining fold, rotating through all folds. It provides a more reliable estimate of model performance than a single train/test split, reduces variance in performance estimates, and helps detect overfitting.

Q7: How do you choose between models? A: Consider: (1) Data size and type, (2) Interpretability requirements, (3) Latency constraints, (4) Training time budget, (5) Baseline performance, (6) Cross-validation scores with error bars, (7) Business metrics (not just ML metrics), (8) Maintenance and monitoring cost.


Practice Exercise

Hands-On: Quick Reference Implementation Challenge

Objective: Implement 3 common ML algorithms from memory.

Exercise 1: K-Nearest Neighbors (10 min)

import numpy as np
from collections import Counter

class KNN:
    def __init__(self, k=3):
        self.k = k

    def fit(self, X, y):
        self.X_train = X
        self.y_train = y

    def predict(self, X):
        return [self._predict(x) for x in X]

    def _predict(self, x):
        distances = [
            np.sqrt(np.sum((x_train - x) ** 2))
            for x_train in self.X_train
        ]
        k_indices = np.argsort(distances)[:self.k]
        k_labels = self.y_train[k_indices]
        most_common = Counter(k_labels).most_common(1)
        return most_common[0][0]

Exercise 2: Decision Tree Split (10 min)

import numpy as np

def gini_impurity(y):
    counts = np.bincount(y)
    probs = counts / len(y)
    return 1 - np.sum(probs ** 2)

def best_split(X, y):
    best_gini = float('inf')
    best_feature, best_threshold = None, None

    for feature in range(X.shape[1]):
        thresholds = np.unique(X[:, feature])
        for threshold in thresholds:
            left_mask = X[:, feature] <= threshold
            right_mask = ~left_mask

            if left_mask.sum() == 0 or right_mask.sum() == 0:
                continue

            gini = (
                left_mask.sum() * gini_impurity(y[left_mask])
                + right_mask.sum() * gini_impurity(y[right_mask])
            ) / len(y)

            if gini < best_gini:
                best_gini = gini
                best_feature = feature
                best_threshold = threshold

    return best_feature, best_threshold, best_gini

Exercise 3: Model Comparison (15 min)

from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC

X, y = make_classification(n_samples=1000, n_features=20, random_state=42)

models = {
    "Logistic Regression": LogisticRegression(max_iter=1000),
    "Random Forest": RandomForestClassifier(n_estimators=100),
    "SVM": SVC(kernel='rbf'),
}

for name, model in models.items():
    scores = cross_val_score(model, X, y, cv=5, scoring='f1')
    print(f"{name}: F1 = {scores.mean():.3f} +/- {scores.std():.3f}")

Bonus Challenge:

  • Add feature scaling and compare results
  • Implement leave-one-out cross-validation
  • Visualize decision boundaries for each model

Key Formulas Reference

FormulaExpressionContext
Linear Regressiony = wX + bBaseline regression
Logistic Regressionp = sigmoid(wX + b)Binary classification
MSE LossL = 1/N * sum(y - y_hat)^2Regression training
Cross-EntropyL = -1/N * sum(y*log(p) + (1-y)*log(1-p))Classification training
Gradient Descentw = w - lr * dL/dwOptimization
Gini ImpurityG = 1 - sum(p_i^2)Decision tree splits
Information GainIG = H(parent) - weighted H(children)Feature selection
TF-IDFTF-IDF(t,d) = TF(t,d) * log(N/DF(t))Text features

Key Takeaways


Further Reading

  • "An Introduction to Statistical Learning" (ISLR) -- Free, accessible ML textbook
  • "The Elements of Statistical Learning" (ESL) -- Advanced, mathematically rigorous
  • "Hands-On Machine Learning" by Aurelien Geron -- Practical guide with scikit-learn and TensorFlow
  • "Python Machine Learning" by Sebastian Raschka -- Comprehensive Python-focused guide
  • scikit-learn documentation -- Best ML library documentation with examples
  • Fast.ai -- Top-down practical deep learning course

What to Learn Next

-> What is Machine Learning? -- Complete Introduction Learn about what is machine learning? -- complete introduction.

-> Linear Regression -- Complete Guide with Math and Code Learn about linear regression -- complete guide with math and code.

-> Model Evaluation -- Metrics, Cross-Validation and Selection Learn about model evaluation -- metrics, cross-validation and selection.

-> Transformers -- Attention Is All You Need Complete Guide Learn about transformers -- attention is all you need complete guide.

-> ML System Design -- Architecture and Production Patterns Learn about ml system design -- architecture and production patterns.

-> ML Interview Prep -- Questions, Answers and System Design Learn about ml interview prep -- questions, answers and system design.

Need Expert Machine Learning Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement