Lesson deck
Data, features and production ML: preprocessing, encodings, point-in-time features, skew, drift and operations flashcards
Reviewing needs JavaScript. You can read every card in the list below.
Loading your cards...
Back to the lesson21 cards in this deck
Read all 21 cards as a list
- Front
- What are MCAR, MAR and MNAR, and which one can the observed data repair?
- Back
- MCAR: missingness depends on nothing, so the observed rows are a fair sample. MAR: it depends on other observed columns, so imputing within groups of those columns repairs the bias (observed mean 47.58, group-wise fill 50.00, true 50.06). MNAR: it depends on the missing value itself, and the observed data cannot fully repair it (48.36 after the same fill).
- Front
- Why add a missing indicator next to an imputer?
- Back
- Missingness is often a signal. In the script a lab value was missing for 70% of healthy and 20% of sick patients: disease rate 0.109 where missing, 0.538 where measured. A filled column hides that. The indicator keeps it. SimpleImputer(add_indicator=True) adds one.
- Front
- What goes wrong when you mean-impute a column?
- Back
- The mean of the observed values stays wrong if the missingness is MAR or MNAR (47.58 against a true 50.06), and the spread shrinks even under MCAR: sd 8.35 against a true 9.94 with about 29% filled. The model also sees no trace that a value was filled, unless you add an indicator.
- Front
- State the rule for fitting preprocessing, and what breaks if you ignore it.
- Back
- Learn every statistic (fill value, mean, category list, encoding) on training rows only, and apply it unchanged elsewhere. Fitting on train and test hid a real shift: a test mean of 1.529 looked like 0.617 after scaling. A pipeline makes the rule automatic inside cross-validation.
- Front
- One outlier in a column: what does standard scaling do, and what are the alternatives?
- Back
- One value of 1,000 among 99 values with sd 2 pushed the standard deviation to 98.49 and shrank the scaled spread of the other values from 1.000 to 0.020. Median and IQR scaling kept it at 0.794. Also clip, or log-transform a skewed feature (skewness 5.15 became 0.02). Trees do not need scaling.
- Front
- Which encoding for which categorical feature?
- Back
- Few unordered levels: one-hot. Real order: ordinal. Many levels with a supervised signal: target encoding, out-of-fold and smoothed. Huge or open vocabularies: hashing (500 categories in 256 buckets still left 438 sharing a bucket) or learned embeddings. Cyclical numbers such as the hour: sin and cos.
- Front
- Why does target encoding leak, and what is the cure?
- Back
- A row's own label sits inside its own encoding if it is fitted and applied on the same rows. With labels independent of the category, the in-sample AUC was 0.770 and the out-of-fold AUC 0.501. Encode each fold from the other folds, use the full-train encoding for test and live rows, and give unseen levels the prior. Leave-one-out is not safe: on one constant level its training AUC was 0.000 against 0.473 for 5-fold.
- Front
- Write the smoothed target encoding and say what the smoothing does.
- Back
- (sum of targets in the level + m x overall mean) / (rows in the level + m). A level with few rows is pulled to the overall mean. With overall mean 0.3220 and m = 10, a one-row level with target 1 became 0.384 (raw 1.000) and a 100-row level at 0.800 became 0.757. It is a safeguard for rare levels, not a leakage fix.
- Front
- When does a feature cross help?
- Back
- When the effect exists only in a combination and the model is additive. With y positive when x1 times x2 is above 0, the best sign rule on x1 scored 0.500 and on x2 0.504, while the cross scored 0.952. Trees and networks learn crosses themselves. Crosses of high-cardinality features explode in width.
- Front
- State the point-in-time rule and two ways to break it.
- Back
- A feature for a prediction at time t may use only information that existed at t. Break it with a window that reaches past t, or by joining the latest value instead of the value as of t. Counting 7 days past t raised churn AUC from 0.773 to 0.921, from information that does not exist at prediction time.
- Front
- How does a point-in-time join work?
- Back
- For each event (key, time) take the latest feature row with the same key and a time not after the event, and ignore it if a TTL says it is too old. Rows (time 1, 3), (5, 4), (9, 10): an event at 8 gets 4, an event at 0 gets nothing, an event at 100 gets 10 or nothing with a TTL of 3. Rolling features use only earlier values.
- Front
- What is training-serving skew and how do you defend against it?
- Back
- A difference between performance in training and in serving, caused by different handling of data in the two pipelines, a change in the data, or a feedback loop. Defences: reuse code, log the features you served and compare, golden tests, monitoring. A 0 instead of the median for nulls cut precision from 0.831 to 0.556.
- Front
- What does a Pipeline with a ColumnTransformer buy you, and what does it not?
- Back
- One fitted object holds preprocessing and model, so cross-validation refits imputers, scalers and encoders in each fold and the saved object is the served one. By default unnamed columns are dropped (remainder='drop'). It does not cover features computed outside it, such as SQL joins and windows.
- Front
- Name the three kinds of drift and where each shows.
- Back
- Covariate (data) drift: P(X) changes with P(y | X) fixed, seen in input distributions with PSI or KS. Label shift: P(y) changes with P(X | y) fixed, hinted by the predicted positive rate (P(X) also moves, so input PSI can fire too). Concept drift: P(y | X) changes, seen only in labelled outcomes. Real changes can mix. Inputs PSI 0.0003 with accuracy falling from 1.000 to 0.659 is concept drift.
- Front
- Compute PSI and name its pitfalls.
- Back
- PSI = sum over bins of (actual share - expected share) x ln(actual / expected). Shares (0.5, 0.5) against (0.3, 0.7) give 0.1695. Pitfalls: empty bins need a floor (a new category gave 0.708 or 1.170 for floors of 1e-4 or 1e-6), bins change the value (0.2060, 0.2417, 0.2669 for 5, 10, 20 bins), and small samples add noise: about 0.095 at 100 rows with no drift.
- Front
- What does the KS statistic measure and what is its large-sample problem?
- Back
- The largest gap between two empirical CDFs, with no bins. With 100,000 rows each side, a mean shift of 0.02 gave KS 0.0076, above the approximate 5% critical value 0.0061, while PSI was 0.00033. A test can flag a shift too small to matter, so pair it with a size threshold.
- Front
- How do you monitor a model with delayed labels?
- Back
- Watch data quality, feature and score drift now; report true metrics by cohort once labels mature; compare cohorts of the same age. With a mean delay of 20 days, a 14-day-old cohort had only 0.510 of its positives labelled (rate 0.0253 against 0.0496).
- Front
- Compare retraining triggers.
- Back
- Schedule, drift threshold, performance drop on matured labels, or enough new labelled data. In the toy drift run: never 0.609 mean accuracy, every 10 weeks 0.908 with 3 retrains (worst week 0.673), accuracy trigger 0.952 with 15 retrains (worst week 0.854). Cost of retraining against cost of staleness decides.
- Front
- Describe a safe rollout and the feedback-loop trap.
- Back
- Offline validation, shadow mode (score but do not act), canary with guardrails, ramp, with rollback and a fallback. Feedback loop: if only approved applicants have outcomes, a retrained model never learns about the rejected. Keep an exploration or hold-out slice where decisions do not depend on the model.
- Front
- What do you record to reproduce a model, and why?
- Back
- Code version, a snapshot or hash of the training data, feature definitions, library versions, hyperparameters and seeds, stored with the model. A hash of a dataset changed when one cell changed. It lets you answer what changed and restore the previous model.
- Front
- In what order do you debug a model that works in the notebook and fails in production?
- Back
- 1 Inputs valid (schema, nulls, units, ranges). 2 Skew: served against training features for the same case. 3 Leakage in the original evaluation. 4 Drift (PSI per feature). 5 Label or definition mismatch. 6 Silent fallbacks, timeouts, wrong artifact. 7 Only then a real concept change: retrain or redesign.