skills/tabular/adversarial-validation/SKILL.md
Trains a classifier to distinguish train from test data, detecting distribution shift and identifying leaked features.
npx skillsauth add wenmin-wu/ds-skills tabular-adversarial-validationInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Train a binary classifier where train samples are label 0 and test samples are label 1. If the classifier achieves high AUC, there is significant distribution shift between train and test. The most important features in this classifier reveal which features are shifting — drop or transform them to reduce overfitting.
import numpy as np
import lightgbm as lgb
from sklearn.model_selection import cross_val_score
def adversarial_validation(X_train, X_test, features):
"""Detect train/test distribution shift.
Returns AUC score and feature importances.
"""
X = np.vstack([X_train[features], X_test[features]])
y = np.concatenate([np.zeros(len(X_train)), np.ones(len(X_test))])
clf = lgb.LGBMClassifier(n_estimators=100, learning_rate=0.1, n_jobs=-1)
auc = cross_val_score(clf, X, y, cv=5, scoring='roc_auc').mean()
clf.fit(X, y)
importances = dict(zip(features, clf.feature_importances_))
return auc, importances
auc, imp = adversarial_validation(train, test, feature_cols)
print(f"AUC: {auc:.3f}") # >0.7 means significant shift
data-ai
Scaled Pinball Loss (SPL) metric for evaluating quantile forecasts, normalized by mean absolute successive differences of training data
data-ai
Walk backward through a time series and multiplicatively rescale segments when jumps exceed a fraction of the running mean to correct data collection anomalies
testing
Transform forecasting target to next/current ratio minus one so that optimizing MAE or squared error implicitly minimizes SMAPE
tools
Convert point forecasts to prediction intervals by scaling with logit-transformed quantile ratios passed through a Normal CDF