24 · Workflow für Prädiktionsmodelle und Data Leakage
leakage_vergleich.png
Abbildung · Quellcode

Erzeugt von fig_leakage() in module/24-praediktion-workflow/code/figures.py, Zeile 30–48.
Python
Python-Code: in eine Datei mit Endung
.py schreiben und mit dem ▶-Knopf in VS Code ausführen – oder Zeile für Zeile in die Python-Konsole. Setzt die in Modul 02 eingerichtete Umgebung voraus.def fig_leakage(X_num, y, cv): rng = np.random.default_rng(SEED) X_aug = np.hstack([StandardScaler().fit_transform(X_num), rng.normal(size=(len(y), 300))]) leaky = cross_val_score(LogisticRegression(max_iter=1000), SelectKBest(f_classif, k=12).fit_transform(X_aug, y), y, cv=cv, scoring="roc_auc").mean() honest = cross_val_score(Pipeline([("s", SelectKBest(f_classif, k=12)), ("m", LogisticRegression(max_iter=1000))]), X_aug, y, cv=cv, scoring="roc_auc").mean() fig, ax = plt.subplots(figsize=(6, 3.4)) ax.bar(["mit Leakage\n(Auswahl auf allen Daten)", "korrekt\n(Auswahl in der Pipeline)"], [leaky, honest], color=[EVENT, PRIMARY], width=0.6) for i, v in enumerate([leaky, honest]): ax.text(i, v + 0.01, f"{v:.2f}", ha="center", fontweight="bold") ax.axhline(0.5, color=SECONDARY, lw=0.8, ls="--") ax.set_ylim(0, 1) ax.set_ylabel("Kreuzvalidierte ROC-AUC") ax.set_title("Data Leakage täuscht eine bessere Modellgüte vor") save(fig, ASSETS / "leakage_vergleich.png")