Adversarial Validation Pipeline With Drift Report
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Other
Given feature matrices `X_train` and `X_test`, train a cross-validated classifier to tell them apart. `adversarial_validation(X_train, X_test, n_val, k=5, C=1.0, feature_names=None)` returns a dict with keys `oof`, `auc`, `drift`, `val_idx`, `fit_idx`, built from the helpers below.
Requirements:
- `stratified_fold_ids(labels, k)` assigns, per class and in input order, contiguous folds `0..k-1`, where the first `n_c % k` folds get one extra member. `k < 2` or a class with fewer than `k` members raises `ValueError`.
- `roc_auc(y_true, scores)` returns the Mann-Whitney AUC with average ranks, so ties count 0.5. A missing class raises `ValueError`.
- `adversarial_oof_scores(X_train, X_test, k=5, C=1.0)` stacks train (label 0) then test (label 1), assigns folds with `stratified_fold_ids`, fits `StandardScaler` on the training folds only, then fits `LogisticRegression(solver="lbfgs", C=C, max_iter=1000)`. It returns the out-of-fold probability of the test class, with length `n_train + n_test`.
- `feature_drift_table(X_train, X_test, k=5, C=1.0, feature_names=None)` returns the per-column OOF AUC as a DataFrame with columns `feature` and `auc` (float64), stably sorted by AUC descending. Default names are `f0, f1, ...`.
- `select_test_like_rows(oof_scores, n_train, n_val)` takes the top `n_val` of the first `n_train` scores (ties: lower index first) as `val_idx` and the rest as `fit_idx`, both ascending int64. It requires `0 < n_val < n_train` and raises `ValueError` otherwise.
Example:
```python
stratified_fold_ids([0]*7 + [1]*5, 3)
# [0, 0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2]
roc_auc([0, 0, 1, 1], [0.1, 0.4, 0.4, 0.8]) # 0.875
select_test_like_rows([0.2, 0.9, 0.5, 0.9, 0.1, 0.7, 0.3], 5, 3)
# (array([1, 2, 3]), array([0, 4]))
```
```hint Fold sizes
For each class, work out how many members each fold gets before assigning anything, then expand those sizes into a vector of fold ids in input order.
```
```hint Leakage
Anything that learns from data, the scaler included, may only see the rows outside the held-out fold.
```
```hint Tie rules
Check what average ranks do to tied scores in the AUC, and find a single sort that orders by score descending while breaking ties by row index.
```
### Constraints & Assumptions
- `numpy`, `pandas`, `scipy` and `scikit-learn` are available.
- No shuffling or randomness is involved, so every output is deterministic.
### What a Strong Answer Covers
- Fold ids that follow the contiguous per-class rule exactly, with `ValueError` for `k < 2` or a class smaller than `k`.
- A rank-based AUC that gives ties half credit and rejects input with only one class.
- Out-of-fold scores where the scaler and the classifier see only the training folds, returning the test-class probability for every stacked row.
- A drift table with a stable descending sort and default names, and a test-like split that follows the stated tie rule and validation.
- What the result means: an AUC near 0.5 versus well above it, and what to do with the features that drift.
### Follow-up Questions
- The adversarial AUC is 0.95. What do you do next?
- How would you use the `oof` scores for importance weighting instead of row selection?
- Why fit the scaler inside each fold instead of on all the stacked data?
- What kinds of drift can a single-feature logistic regression miss in the drift table?
Overview: Build an adversarial validation pipeline in Python that cross-validates a classifier separating train rows from test rows, reports per-feature drift by AUC and picks the most test-like training rows for validation. Tests leak-free cross-validation, rank-based AUC with ties and deterministic tie-breaking.