Chapter 92 — ML Fundamentals for Senior Interviews

📖 Definition

Machine learning is building a function f(x) → y from examples instead of hand-written rules. A model has parameters learned from data by minimising a loss (how wrong the predictions are) on a training set, and we judge it on data it has not seen.

Senior GenAI interviews still start here: if you can't explain overfitting, leakage or the right metric, nobody trusts your LLM answers.

🔍 The Three Types of Learning

Type Data Goal Examples
Supervised Inputs + labels Predict the label Spam detection, price prediction, LLM fine-tuning (SFT)
Unsupervised Inputs only Find structure Clustering customers, anomaly detection, topic modelling
Self-supervised Inputs only, labels made from the data itself Learn representations Next-token prediction (GPT), masked tokens (BERT), contrastive embeddings
Reinforcement Actions + rewards Maximise long-term reward Game playing, RLHF for LLMs

Regression predicts a number; classification predicts a category.

🔍 Bias–Variance Trade-off (in Plain Words)

High bias (underfitting) High variance (overfitting)
What happens Model too simple, misses the pattern Model memorises the training data, including noise
Train error High Very low
Validation error High High (big gap from train)
Fixes Bigger model, more features, train longer, less regularisation More data, regularisation, dropout, early stopping, simpler model, data augmentation

Total error ≈ bias² + variance + irreducible noise.

Modern twist (senior): very large neural networks show double descent — test error can go down again as you grow the model past the point where it fits the training data perfectly. That's one reason huge, over-parameterised LLMs still generalise.

🔍 Train / Validation / Test Splits

Split Used for Rule
Train Learning parameters Model sees it
Validation Choosing hyperparameters, early stopping Model is tuned against it
Test Final, honest estimate Touch once at the end
  • K-fold cross-validation: split into k parts, train on k−1, validate on 1, rotate; average the scores. Good for small data.
  • Time series: never shuffle — train on the past, validate on the future (rolling / forward-chaining splits).
  • Group split: keep all rows of one user/patient in the same split, or you leak identity.

⚠️ Data Leakage (The #1 Silent Killer)

Leakage = information in training that won't exist at prediction time. Results look amazing offline and fail in production.

Leak Example Fix
Target leakage Feature "refund_issued" when predicting "will customer complain" Only features available before the prediction moment
Train–test contamination Fitting the scaler/tokenizer on the full dataset Fit preprocessing on train only (pipelines)
Temporal leakage Random split on time-ordered data Time-based split
Duplicate leakage Same document in train and test Deduplicate before splitting
LLM benchmark contamination Test questions were in pretraining data Private, fresh eval sets; n-gram overlap checks

💻 Code Example — A Leak-Free Pipeline (scikit-learn)

python
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold, cross_val_score

numeric = ["age", "income", "tenure_days"]
categorical = ["plan", "country"]

preprocess = ColumnTransformer([
    ("num", Pipeline([("impute", SimpleImputer(strategy="median")),
                      ("scale", StandardScaler())]), numeric),
    ("cat", OneHotEncoder(handle_unknown="ignore"), categorical),
])

model = Pipeline([
    ("prep", preprocess),                        # fitted inside each fold → no leakage
    ("clf", LogisticRegression(C=1.0, class_weight="balanced", max_iter=1000)),
])

cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)   # keeps class ratio per fold
scores = cross_val_score(model, X, y, cv=cv, scoring="average_precision")
print(scores.mean(), scores.std())

🔍 Regularisation

Technique How it helps
L2 (Ridge / weight decay) Penalises large weights → smoother model
L1 (Lasso) Pushes some weights to exactly 0 → feature selection
Elastic net Mix of L1 and L2
Dropout Randomly zero activations during training → no single neuron is relied on
Early stopping Stop when validation loss stops improving
Data augmentation More varied training data
Label smoothing Targets like 0.9/0.1 instead of 1/0 → less over-confident

📊 Choosing the Right Metric

Problem Metric When
Balanced classification Accuracy, F1 Classes roughly equal
Imbalanced (fraud 0.1%) PR-AUC, recall at fixed precision Accuracy is useless — predicting "no fraud" gives 99.9%
Ranking quality across thresholds ROC-AUC Probability of ranking a random positive above a random negative
Cost of false negatives high (cancer) Recall / sensitivity Missing a positive is worse
Cost of false positives high (spam to inbox) Precision Flagging good items is worse
Regression MAE (robust to outliers), RMSE (punishes big errors), MAPE Business-friendly units
Ranking / search / recsys NDCG@k, MRR, Recall@k, MAP Order of results matters
Probabilities used for decisions Calibration (ECE, Brier score) "70% confident" should be right 70% of the time
Language models Perplexity, task accuracy, human / LLM-judge preference See Evaluation chapter
text
Precision = TP / (TP + FP)      "Of what I flagged, how much was right?"
Recall    = TP / (TP + FN)      "Of all real positives, how many did I catch?"
F1        = 2 · P · R / (P + R)

🔍 Class Imbalance

  1. Use the right metric (PR-AUC, recall@precision).
  2. Class weights in the loss (class_weight="balanced").
  3. Resampling: undersample majority, oversample minority (SMOTE) — only on the training fold.
  4. Tune the decision threshold on validation instead of using 0.5.
  5. Focal loss (down-weights easy examples) for deep models.

📊 Classic Models — When to Use What

Model Strengths Weaknesses
Linear / logistic regression Fast, interpretable, strong baseline Only linear patterns (unless you engineer features)
Decision tree Interpretable, non-linear Overfits alone
Random forest (bagging) Robust, little tuning Larger, slower inference
Gradient boosting (XGBoost, LightGBM, CatBoost) Best for tabular data, handles mixed features Sequential training, many hyperparameters
k-NN Simple, no training Slow at scale, needs good distance
SVM Good on small, high-dimensional data Doesn't scale to millions of rows
Neural networks Images, text, audio, huge data Need lots of data and compute; less interpretable

Bagging = train many models in parallel on bootstrap samples and average (reduces variance). Boosting = train models sequentially, each fixing the previous errors (reduces bias).

Senior point: for tabular business data, gradient-boosted trees usually beat deep learning and LLMs. Saying "I'd start with LightGBM and a logistic-regression baseline" is a strong answer.

🔍 Feature Engineering Essentials

  • Numeric: scaling (needed for linear/NN/k-NN, not for trees), log transform for skewed values, binning.
  • Categorical: one-hot (low cardinality), target encoding (high cardinality, do it inside CV folds), embeddings.
  • Time: hour/day-of-week, time since last event, rolling counts/averages (computed only from the past).
  • Text: TF-IDF baseline, or embeddings from a pretrained model.
  • Missing values: impute + add an "is_missing" flag (missingness itself can be signal).

🔍 Interpretability

Tool Gives
Coefficients (linear) Direction and size of each feature's effect
Feature importance (trees) Global ranking (can be biased toward high-cardinality features)
SHAP Per-prediction contribution of each feature, consistent and additive
Partial dependence How prediction changes with one feature
LIME Local simple model around one prediction

🧠 What a Senior Answer Adds

  • Starts from the business goal and cost of errors, then picks the metric.
  • Builds a simple baseline first and shows the lift of anything complex.
  • Talks about leakage, splits by time/group, and data quality before models.
  • Thinks about monitoring and retraining (Chapter on MLOps) — the model is not done at deploy.

🌍 Real-World Impact

  • A churn model with 0.95 AUC offline dropped to 0.62 in production because a "cancellation_reason" feature leaked the label.
  • Switching a fraud model's metric from accuracy to recall at 95% precision changed which model the team shipped.

🎯 Likely Interview Questions

  1. Explain the bias-variance trade-off. — Simple models underfit (high bias), complex ones overfit (high variance); pick complexity and regularisation by validation error.
  2. How do you detect overfitting? — Training error much lower than validation error; learning curves diverge.
  3. What is data leakage? Give examples. — Info at train time not available at prediction time: target leakage, preprocessing on full data, random split on time series, duplicates.
  4. Why is accuracy bad for imbalanced data? — Predicting the majority class scores high while catching nothing; use PR-AUC, recall/precision.
  5. ROC-AUC vs PR-AUC? — ROC-AUC is insensitive to class balance and can look great on rare-event problems; PR-AUC focuses on the positive class.
  6. L1 vs L2 regularisation? — L1 creates sparse weights (feature selection); L2 shrinks all weights smoothly.
  7. Bagging vs boosting? — Parallel averaging to reduce variance vs sequential error-correction to reduce bias.
  8. Why do gradient-boosted trees win on tabular data? — Handle mixed feature types, non-linear interactions, missing values, little preprocessing; data is usually too small/structured for deep nets to shine.
  9. How do you handle class imbalance? — Right metric, class weights, resampling in train folds only, threshold tuning, focal loss.
  10. How do you split time-series data? — Chronologically; train on past, validate on future, rolling windows.
  11. What is calibration and why care? — Predicted probabilities match observed frequencies; needed when probabilities drive decisions (pricing, risk). Fix with Platt scaling or isotonic regression.
  12. What is double descent? — Test error can fall again when models become much larger than needed to fit the training data.

← Cost & Latency Optimization | Index | Next: Deep Learning & Training →