Lesson deck
Evaluation, leakage and imbalance: validation design, metrics, thresholds and rare classes flashcards
Reviewing needs JavaScript. You can read every card in the list below.
Loading your cards...
Back to the lesson14 cards in this deck
Read all 14 cards as a list
- Front
- Which splitter for which data?
- Back
- Independent rows: k-fold. Rare class: stratified k-fold (every fold keeps about the same class mix). Several rows per patient, user or device: group k-fold (no group on both sides). Time order: forward-chaining, train on the past and test on the next block, with an optional gap. Use the test set once, after every choice is made.
- Front
- Why can cross-validation still mislead you?
- Back
- Choosing the best of many configurations on the same folds makes the winner's score optimistic, so keep a test set for one final look or use nested cross-validation. Small data also gives wide error bars: Varoquaux saw roughly 10 points of uncertainty at about 100 samples in neuroimaging, and the standard error across folds underestimates it.
- Front
- Name the main kinds of leakage and one fix each.
- Back
- Target (a feature known only after the outcome): fix a prediction time and audit features. Preprocessing before the split: fit inside the training fold with a pipeline. Shared entities or duplicates: group k-fold, de-duplicate. Temporal (random split of time data): forward-chaining. Resampling before the split: resample the training fold only.
- Front
- How big can a preprocessing leak be?
- Back
- In the lesson's script, with 100 rows of noise and 2,000 noise features, selecting 10 features on all rows gave a test accuracy of 0.817 and selecting on the training rows only gave 0.492 (chance). The scikit-learn docs show 0.76 against about 0.5 with 200 samples and 10,000 noise features. Put every fitted step in a Pipeline.
- Front
- What are the red flags for a leak, and the cheap checks?
- Back
- Flags: a score too good for the problem, one feature carrying most of the importance, a big gap between cross-validation and a later holdout. Checks: test rows that exactly duplicate training rows, group ids on both sides of a split, and for every feature the question of when it becomes known. In the script, row-level CV on patient data gave 0.944 against 0.490 for group CV.
- Front
- Why does 99.8% accuracy tell you little with 0.2% positives?
- Back
- A model that never flags anything scores 99.8% and finds nothing. Use the confusion matrix, precision (TP over flagged), recall (TP over real positives), F1 (their harmonic mean), and a cost. In the lesson's example a model with lower accuracy and F1 but higher recall was cheaper (12,100 against 26,250 at 5,000 per miss and 50 per false alarm).
- Front
- ROC-AUC versus PR-AUC on rare positives?
- Back
- ROC-AUC is the chance a random positive outranks a random negative, so adding more negatives of the same kind barely moves it (it does not change in expectation). Average precision depends on the positive rate. Same ranker in the script: ROC-AUC 0.903 against 0.911 but AP 0.910 against 0.091 when negatives went from 100 to 49,900. A constant score gets an AP equal to the positive rate.
- Front
- How do you pick a threshold from costs?
- Back
- For calibrated probabilities, predict positive when p exceeds C_FP over (C_FP plus C_FN). A miss of 5,000 and a false alarm of 50 give 0.0099, and in the script that cost 14.54 per row against 191.23 at 0.5, at 20% precision. If capacity is limited, choose the threshold that matches the alert volume. Tune it on validation data, not on the training data.
- Front
- What is calibration and how do you check it?
- Back
- Among cases scored near 0.8, about 80% should be positive. Check with reliability bins, expected calibration error, log loss or Brier score. It is separate from ranking: doubling the logits left ROC-AUC at 0.8346 and moved ECE from 0.011 to 0.093. Fix with Platt sigmoid, isotonic (more than about 1,000 samples) or temperature scaling, fitted on held-out data.
- Front
- MAE, RMSE, R-squared and MAPE: when each?
- Back
- MAE is the typical miss (best constant is the median). RMSE squares errors so big misses dominate (errors 1, 1, 1, 1, 20: MAE 4.8, RMSE 8.99). R-squared compares with predicting the mean. MAPE is unstable near zero: an actual of 0.1 predicted as 1.1 is a 1000% error.
- Front
- In what order do you treat class imbalance?
- Back
- Baseline and a suitable metric, stratified splits with the test set at the natural rate, class weights, tune the threshold on validation, resample only inside training folds if needed, recalibrate, then consider anomaly framing or more labels. Balanced class weights are n over (classes times class count): 0.5051 and 50.0 for 99 negatives and 1 positive.
- Front
- Why must resampling and SMOTE happen inside the fold?
- Back
- Copies or interpolated points of a minority row would sit in both training and validation, and the validation rate stops matching real traffic. In the script, duplicating first gave a balanced accuracy of 0.972 on noise and doing it inside the folds gave 0.495. SMOTE interpolates between neighbours, so its points may be unrealistic and it needs a variant for categorical data.
- Front
- What do weights and resampling do to probabilities?
- Back
- They keep the ranking but shift the probabilities: weighting positives by w multiplies the odds by w, so a true 0.01 becomes 0.500 at w = 99. Recalibrate on held-out data at the natural rate before using the score as a probability or in a cost-based threshold.
- Front
- What do baselines and error analysis add?
- Back
- A metric means little without a baseline: DummyClassifier most_frequent, a constant score (AP equal to the positive rate), the base-rate log loss, a simple model, and the current rule. Then slice: 9,000 rows at 95% and 1,000 at 70% make 92.5% overall. Read the worst errors, group them by cause and fix the biggest group first.