Skip to content
Tutorials Center
My learning

Topics

Lesson 111 of 111 ยท Practice and Review

Machine Learning Cheat Sheet

A one-page machine learning cheat sheet: choose a model and metric, copy scikit-learn snippets and a pipeline template, and check your work against a pitfalls list.

You now know how a machine learning project fits together. This page is the quick-reference version: tables for choosing a model and a metric, the scikit-learn calls you will type again and again, a pipeline template you can copy, and a checklist of mistakes to rule out before you trust a score. Use it when you remember that something exists but not what it is called or when to reach for it.

Every table points back to the lesson that explains the details. The code assumes scikit-learn 1.8, and every example is seeded so it prints the same thing each time.

The loop behind every project

Frame it Target, cost of errors, metric.
Split Hold out a test set first.
Baseline Dummy model sets the bar.
Iterate Pipeline, CV, tune, repeat.
1 / 2

Choosing a model

Start simple, measure against a baseline, then move up only if the simple model is not good enough.

Situation Good first choice Why Lesson
Numeric target, roughly linear LinearRegression, Ridge Fast, interpretable Linear Regression
Many correlated or noisy features Ridge, Lasso, ElasticNet Shrinks variance; lasso selects Ridge, Lasso and Elastic Net
Binary or multiclass labels, need coefficients LogisticRegression Probabilities, log-odds Logistic Regression
Tabular data, strong default HistGradientBoosting* Accurate, handles NaN and categories Histogram Gradient Boosting
Tabular data, little tuning RandomForest* Robust, hard to break Random Forests
Need a readable rule set DecisionTree* (shallow) Rules you can print Decision Trees
Small data, clear margin SVC, LinearSVC Strong with scaling Support Vector Machines
Text counts, quick baseline MultinomialNB, linear model on TF-IDF Fast and sparse-friendly Naive Bayes
Huge data, streaming SGDClassifier with partial_fit Online learning Linear Models with SGD
Groups without labels KMeans, GaussianMixture, DBSCAN Round, soft or arbitrary shapes Clustering Basics
Too many features PCA, feature selection Fewer, denser inputs Principal Component Analysis
Rare weird points IsolationForest, LocalOutlierFactor No labels needed Anomaly and Outlier Detection

A few rules of thumb worth remembering:

  • Distance-based and margin-based models (k-NN, SVM, linear models with penalties, PCA) need scaled features. Trees do not.
  • Trees cannot extrapolate beyond the training range; linear models can.
  • When two models score about the same, pick the simpler or faster one.

Choosing a metric

Pick the metric that mirrors the real cost of mistakes, before you train anything.

Task Metric Use it when Watch out for
Regression MAE You want the typical error in target units Treats all errors alike
Regression RMSE Big misses are especially costly Dominated by outliers
Regression R squared Comparing to "predict the mean" Can look fine with a bad fit; use residual plots
Classification Accuracy Classes are balanced and errors cost the same Misleading on imbalance
Classification Precision False alarms are expensive Ignores missed cases
Classification Recall Misses are expensive Ignores false alarms
Classification F1 One number balancing precision and recall Hides which side is weak
Classification ROC AUC Ranking quality, balanced-ish classes Rosy on rare positives
Classification Average precision Rare positive class Depends on the base rate
Probabilities Log loss, Brier score Predicted probabilities are used directly Needs calibration to be trusted
Clustering Silhouette, adjusted Rand index Compare cluster counts, or compare to known labels Silhouette favors round clusters

See Regression Metrics, Classification Metrics and Evaluating Clusters.

Scikit-learn snippets

All estimators share one interface, so these calls work for almost every model. See The scikit-learn API.

Task Code
Split, keeping class ratios train_test_split(X, y, test_size=0.25, stratify=y, random_state=0)
Fit and predict model.fit(X_train, y_train), model.predict(X_test)
Class probabilities model.predict_proba(X_test)[:, 1]
Default score model.score(X_test, y_test)
Cross-validated score cross_val_score(model, X, y, cv=5, scoring="f1")
Several metrics at once cross_validate(model, X, y, cv=5, scoring=["accuracy", "f1"])
Groups stay together GroupKFold(n_splits=5)
Time-ordered data TimeSeriesSplit(n_splits=5)
Grid search GridSearchCV(model, {"clf__C": [0.1, 1, 10]}, cv=5)
Random search RandomizedSearchCV(model, distributions, n_iter=20, random_state=0)
Copy an unfitted model sklearn.base.clone(model)
Inspect or set settings model.get_params(), model.set_params(alpha=0.5)
Report classification_report(y_true, y_pred)
Baseline DummyClassifier(strategy="most_frequent"), DummyRegressor()
Save and load joblib.dump(model, "m.joblib"), joblib.load("m.joblib")

Learned attributes end with an underscore (coef_, feature_importances_, best_params_); settings you choose do not. See Cross-Validation and Hyperparameter Tuning.

Python Metrics for a classifier and a regressor

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The classifier's accuracy of 0.964 hides the weaker minority class: recall for class 1 is only 0.720. That is why you read the whole report, not one number.

Pipeline template

Put every learned step (imputing, scaling, encoding, selection) and the model in one Pipeline. Cross-validation then refits all of it on each training fold, which prevents leakage. The template below handles numeric and categorical columns, missing values and unseen categories.

Python A copy-and-adapt tabular pipeline

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Swap in your own column lists and replace LogisticRegression with any estimator (a HistGradientBoostingClassifier needs no scaler and handles missing values itself). Parameters of a step are addressed as step__parameter, for example clf__C in a grid search. See Pipelines and ColumnTransformer and Mixed Data.

Comparing candidates fairly

Evaluate every candidate with the same folds and metric, include a baseline, and report the spread, not only the mean. If two models differ by less than the standard deviation across folds, treat them as tied.

Python One loop, six models

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The baseline scores an F1 of 0.000 because it never predicts the positive class, so any real model has a clear bar to clear. On this dataset the two tree ensembles lead, with histogram boosting at 0.901 and the forest at 0.889, both well above logistic regression at 0.771.

Pitfalls checklist

Run through this list before you believe a number or ship a model.

Check Question to ask Lesson
Baseline Does the model beat a dummy model by a meaningful margin? Baselines and Dummy Models
Split Was the test set held out before any exploration or tuning? Train, Validation and Test Sets
Leakage Is every fitted step inside a pipeline? Any feature that is a result of the label? Nested Cross-Validation and Data Leakage
Groups and time Are related rows or future rows in the training set? Cross-Validation for Groups and Time
Metric Does it match the cost of errors and the class balance? Classification Metrics
Threshold Was the decision threshold chosen on validation data, not left at 0.5 by habit? Thresholds, ROC and Precision-Recall Curves
Overfitting Is the gap between train and validation scores small? Overfitting, Underfitting and Bias-Variance
Tuning Is the final score from data never used to pick settings? Hyperparameter Tuning
Probabilities Are predicted probabilities calibrated if they are used as probabilities? Probability Calibration and Uncertainty
Reproducibility Are seeds, library versions and data versions recorded? Common Machine Learning Mistakes
Fairness Do error rates differ across groups that matter? Fairness, Bias and Responsible ML
Deployment Is the serving code identical to training code, and is drift monitored? Saving Models and Deploying

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which model type needs scaled features to work well?

    k-NN works with distances, so a feature with a large range dominates. Tree-based models split on thresholds one feature at a time and are unaffected by scale.
  2. 2. You predict a rare event (1% positives). Which metric is most informative?

    Accuracy is 99% for a model that never finds the event. Average precision focuses on how well the positives are ranked and found.
  3. 3. Why put the scaler and the model together in one Pipeline for cross-validation?

    Every learned step is fit only on the training part of each fold. Scaling on all data first leaks information from the held-out rows.
  4. 4. A baseline DummyClassifier scores the same F1 as your new model. What does that tell you?

    A model that does not beat the trivial strategy adds no value. Investigate the features, the target and the setup before tuning further.

Summary

  • Frame the problem, hold out a test set, set a baseline, then iterate with pipelines and cross-validation.
  • Start with a simple model; histogram gradient boosting, random forests and regularized linear models cover most tabular problems.
  • Choose the metric from the cost of errors, and read more than one number on imbalanced data.
  • Keep all learned preprocessing inside a Pipeline and tune settings with cross-validation, never on the test set.
  • Compare candidates on the same folds, with a baseline, and report the spread.
  • Walk the pitfalls checklist before trusting a score or shipping a model.

That completes the Machine Learning track. To keep practicing, revisit the Machine Learning Interview Questions or work through the projects.