Free JYnapse Knowledge Base · Cheat Sheet

Machine Learning Cheat Sheet

The end-to-end workflow — frame the problem, split, preprocess, train, evaluate — with scikit-learn code and the metrics that tell you if it actually works.

For Analysts moving into ML · founders scoping AI Level Beginner → Intermediate Languages EN · BM · 中文 on request

1 Which problem is it?

Predict a categoryClassification
Predict a numberRegression
Group similar itemsClustering
Simplify / compressDim. reduction
What to recommendRecommenders

First two are supervised (you have labelled answers). Clustering is unsupervised. Know which before touching a model.

2 The workflow

  • 1. Define the target & success metric.
  • 2. Split train / test before anything else.
  • 3. Explore & clean (on train only).
  • 4. Engineer features.
  • 5. Train a simple baseline.
  • 6. Evaluate, tune, compare.
  • 7. Ship & monitor.

Never let test data influence training — that's leakage, and it fakes great scores.

3 Split the data

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42,
    stratify=y)   # keep class balance

Typical 80/20 or 70/30. random_state makes it reproducible. Keep the test set locked away until the very end.

4 Preprocess

from sklearn.preprocessing import StandardScaler, OneHotEncoder

# scale numbers, encode categories
StandardScaler().fit_transform(X_num)
OneHotEncoder(handle_unknown="ignore")
  • Scale numeric features for distance/gradient models.
  • Encode categoricals (one-hot or ordinal).
  • Impute missing values (SimpleImputer).
  • Fit on train, apply the same transform to test.

5 Pick a model

Linear / LogisticFast baseline, interpretable
Decision TreeRules, easy to explain
Random ForestStrong default, low fuss
Gradient BoostingOften best on tabular (XGBoost/LightGBM)
KNN / SVMSmall, clean datasets
K-MeansClustering

For tabular business data, start with a Random Forest or gradient boosting — rarely the wrong call.

6 Train & predict

from sklearn.ensemble import RandomForestClassifier

model = RandomForestClassifier(n_estimators=300,
                               random_state=42)
model.fit(X_train, y_train)

pred  = model.predict(X_test)
proba = model.predict_proba(X_test)[:, 1]

Every scikit-learn estimator follows the same fit → predict pattern — swap models with one line.

7 Score classification

Accuracy% correct — bad on imbalance
PrecisionOf predicted-positive, how many right
RecallOf actual-positive, how many caught
F1Balance of precision & recall
ROC-AUCRanking quality, 0.5 = random

99% accuracy is meaningless if 99% of cases are one class. Use the confusion matrix + F1/recall for fraud, churn, disease.

8 Score regression

MAEAvg absolute error — same units
RMSEPenalises big misses
R²% of variance explained
MAPEError as a %
from sklearn.metrics import mean_absolute_error, r2_score
mean_absolute_error(y_test, pred)
r2_score(y_test, pred)

9 Over/underfitting

  • Overfit — great on train, poor on test. Memorised noise.
  • Underfit — poor on both. Too simple.
  • Fix overfit: more data, fewer features, regularise, simpler model.
  • Trust cross-validation, not a single split:
from sklearn.model_selection import cross_val_score
cross_val_score(model, X, y, cv=5).mean()

10 Pipeline it

from sklearn.pipeline import make_pipeline

pipe = make_pipeline(StandardScaler(), model)
pipe.fit(X_train, y_train)
  • A Pipeline bundles preprocessing + model — no leakage, one object to deploy.
  • Tune with GridSearchCV / RandomizedSearchCV.
  • Save with joblib.dump(pipe, "model.pkl").
  • Monitor live: data drifts, and models go stale.