26 · Baum-Ensembles und Gradient Boosting
baum_tiefe.png
Abbildung · Quellcode

Erzeugt von fig_baum_tiefe() in module/26-ensembles-boosting/code/figures.py, Zeile 76–95.
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_baum_tiefe(X_train, X_val, y_train, y_val) -> None: """Train vs validation AUC over decision tree depth (overfitting curve).""" depths = [1, 2, 3, 4, 5, 6, 7, 8, 10, 12] train_aucs, val_aucs = [], [] for d in depths: tree = DecisionTreeClassifier(max_depth=d, random_state=SEED) tree.fit(X_train, y_train) train_aucs.append(roc_auc_score(y_train, tree.predict_proba(X_train)[:, 1])) val_aucs.append(roc_auc_score(y_val, tree.predict_proba(X_val)[:, 1])) fig, ax = plt.subplots(figsize=(7, 4.2)) ax.plot(depths, train_aucs, "o-", color=PRIMARY, label="Trainings-AUC") ax.plot(depths, val_aucs, "s-", color=EVENT, label="Validierungs-AUC") ax.axvline(depths[int(np.argmax(val_aucs))], color=SECONDARY, lw=0.9, ls="--", label=f"Optimale Tiefe = {depths[int(np.argmax(val_aucs))]}") ax.set_xlabel("Baumtiefe (max_depth)") ax.set_ylabel("ROC-AUC") ax.set_title("Überanpassung: AUC über Baumtiefe") ax.legend(loc="lower right") save(fig, ASSETS / "baum_tiefe.png")