Machine Learning Fundamentals — Cheatsheet
The Core Workflow
python
from sklearn.model_selection import train_test_split
from sklearn.dummy import DummyClassifier
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 1. Baseline first — always
baseline = DummyClassifier(strategy="most_frequent").fit(X_train, y_train)
print("baseline:", baseline.score(X_test, y_test))
# 2. Real model
model.fit(X_train, y_train)
print("train:", model.score(X_train, y_train))
print("test:", model.score(X_test, y_test)) # big gap vs. train = overfitting
Model Families, at a Glance
|---|---|---|
| Linear regression | A number | Relationship is roughly a straight line |
|---|
| Logistic regression | A probability/class | Simple, interpretable classification |
| Decision tree | A number or class | Need something human-readable |
| Random forest | A number or class | Want accuracy, less worried about interpretability |
| Gradient boosting | A number or class | Want top accuracy on structured/tabular data |
| K-means | Cluster assignment (unsupervised) | No labels exist, just want to find groups |
Classification Metrics
python
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score, accuracy_score, roc_auc_score
confusion_matrix(y_true, y_pred)
precision_score(y_true, y_pred) # of flagged, how many right
recall_score(y_true, y_pred) # of real positives, how many caught
f1_score(y_true, y_pred) # harmonic mean of the two
accuracy_score(y_true, y_pred) # overall % correct — misleading on imbalanced data
roc_auc_score(y_true, y_proba) # separating power across every threshold
Regression Metrics
python
from sklearn.metrics import mean_absolute_error, root_mean_squared_error
mean_absolute_error(y_true, y_pred) # average error, all errors weighted equally
root_mean_squared_error(y_true, y_pred) # current API (sklearn 1.4+) — punishes big errors more
The Leakage Checklist
For every feature, ask: "would I actually know this value before the moment I need to make this prediction?" If no — it's leakage, no matter how predictive it looks in testing.
The Overfitting Checklist
1.Compare train score vs. test score — a big gap means overfitting.
2.Use cross-validation on small datasets, not a single split.
3.Limit model complexity (tree depth, number of features) before trusting a near-perfect training score.
Memory line: Baseline first, train/test always, watch the gap, check for leakage — in that order, every time.