Interview conceptMachine Learning

SMOTE, Imbalanced Learning, And Cost-Sensitive Training

Asked of: Machine Learning Engineer

Last updated

What's being tested

These questions test whether you can reason about imbalanced classification as both a data-distribution problem and a decision-cost problem. You should distinguish resampling, synthetic-example generation, class weighting, and per-example weighting, then explain how each changes optimization, calibration, compute, and failure modes. General cares because an MLE must ship training and evaluation pipelines that preserve minority-class signal, avoid leakage, support reproducible retraining, and make predictions useful under asymmetric operational costs.

Core knowledge

  • Class imbalance means the observed class prior is highly skewed, but accuracy can remain high while the minority class is ignored. Use precision, recall, PR-AUC, class-specific confusion matrices, and cost-weighted metrics rather than accuracy alone.

  • Random undersampling removes majority examples, reducing training cost and potentially improving minority recall, but discards information and increases variance. With a seeded permutation, retain exactly the requested count, define deterministic tie-breaking, and preserve original row indices for downstream joins.

  • If a majority row is retained with probability qq, inverse-probability weighting assigns it weight 1/q1/q so its expected contribution matches the full population. For example, keeping 20% of majority rows gives each retained row weight 55; do not accidentally multiply minority weights and change the intended ratio.

  • Random oversampling duplicates minority examples, while SMOTE creates synthetic points: xnew=xi+λ(xj−xi)x_{\text{new}}=x_i+\lambda(x_j-x_i), where λ∼U(0,1)\lambda\sim U(0,1) and xjx_j is a minority neighbor. It can improve local coverage but may amplify mislabeled or boundary minority points.

  • Borderline-SMOTE computes nearest neighbors for each minority point and focuses on “danger” seeds whose neighborhood contains many majority examples. It should not blindly synthesize from isolated minority outliers or points surrounded almost entirely by majority examples.

Tip: Apply any resampling or synthetic generation only inside each training fold, after splitting by the correct entity or time boundary.

  • Performing SMOTE before cross-validation leaks neighborhood and label information into validation data and produces over-optimistic metrics.

  • Class weighting changes the loss by class, commonly using wc∝1/ncw_c \propto 1/n_c or a domain cost ratio. For binary cross-entropy, L=−wy[ylog⁡p+(1−y)log⁡(1−p)]L=-w_y[y\log p+(1-y)\log(1-p)]; it changes the fitted decision boundary and often harms naive probability calibration.

  • Sample weighting assigns wiw_i per example rather than only by label. It supports exposure correction, confidence scores, recency, duplicated-row correction, or instance-specific business costs; class weights are the special case wi=wyiw_i=w_{y_i}.

  • Cost-sensitive decisioning separates probability estimation from action selection. With calibrated p=P(y=1∣x)p=P(y=1\mid x), false-positive cost CFPC_{FP}, and false-negative cost CFNC_{FN}, predict positive when p>CFPCFP+CFNp>\frac{C_{FP}}{C_{FP}+C_{FN}}, assuming correct predictions have zero cost.

  • Compare resampling and weighting on identical, untouched validation data. Weighting preserves feature information and is usually cheaper; undersampling can reduce training cost; oversampling can overfit duplicates; SMOTE increases compute and synthetic-noise risk, especially in sparse or categorical feature spaces.

  • For tree systems such as XGBoost or LightGBM, class weights or scale-positive-style parameters alter gradient and Hessian contributions, not the raw data distribution. For neural networks, weighted minibatches affect gradient variance; monitor minority examples per batch and avoid unstable extreme weights.

  • After weighted training, evaluate calibration with reliability curves, Brier score, or expected calibration error and recalibrate using Platt scaling or isotonic regression on untouched data. Threshold tuning must use deployment-representative prevalence and an explicit cost matrix.

Worked example: Downsample and Upweight the Majority Class

For “Downsample and Upweight the Majority Class,” I would first clarify the input contract: which label is majority, whether the requested sample count is exact, whether replacement is forbidden, and whether output order and original indices must be preserved. I would state that the goal is deterministic majority selection plus weights whose expected loss contribution matches the original dataset. I would organize the answer around validating labels and counts, generating a seeded permutation, selecting the kept majority indices with stable tie-breaking, and assigning inverse sampling-probability weights. The key implementation invariant is that each retained majority row gets Nmajority/NkeptN_{\text{majority}}/N_{\text{kept}}, while untouched classes receive their specified baseline weight. I would explicitly choose whether to return rows in original order or sampled order, because hidden tests often inspect index bookkeeping and reproducibility. I would also mention edge cases such as keeping all rows, requesting more rows than available, empty classes, and a seed of zero. I would avoid claiming that downsampling improves statistical quality automatically: it trades information for compute and must be evaluated against weighted full-data training. If I had more time, I would add property-based tests checking reproducibility, exact counts, unique indices, and equality of weighted class totals.

A second angle

For “False positive vs false negative in a spam filter and a medical test: which error costs more?”, the same imbalance principles become a decision-policy problem rather than a sampling implementation problem. A spam filter may make false positives expensive because legitimate mail is hidden, while a medical screen may make false negatives more expensive because disease goes undetected. I would distinguish model discrimination from threshold selection: the same score model can support different thresholds for different costs. I would also ask about prevalence, follow-up capacity, and whether a positive prediction triggers a reversible review or an irreversible action.

Common pitfalls

Pitfall: Analytical mistake: Saying “always maximize recall” for rare-event detection ignores false-positive cost, review capacity, and precision.

A stronger answer defines the confusion-matrix costs, evaluates PR-AUC and thresholded operating points, and selects a threshold using deployment prevalence.

Pitfall: Evaluating SMOTE-generated validation rows or using random cross-validation across the same user leaks information and inflates recall.

Pitfall: Communication mistake: Listing oversampling, undersampling, and weighting without stating what changes makes the answer sound memorized.

Contrast them directly: resampling changes observed training frequency, weighting changes loss contribution, and neither automatically fixes calibration.

Pitfall: Depth mistake: Treating SMOTE as safe interpolation in every feature space is incorrect.

Interpolation can create invalid categorical values, unrealistic combinations, or samples across class boundaries; use a suitable variant, feature representation, or weighting instead.

Connections

An interviewer may pivot to threshold tuning, probability calibration, and precision-recall operating points, or to leakage-safe cross-validation and temporal validation. For production MLE work, expect follow-ups on training-serving skew, minority-class drift, alert-volume monitoring, and retraining when prevalence or error costs change.

Further reading

Practice questions

Related concepts