29 · Unüberwachtes Lernen und Phänotypisierung von Patient:innen
pca_scale_effect.png
Abbildung · Quellcode

Erzeugt von fig_pca_scale_effect() in module/29-unueberwacht-phenotyping/code/figures.py, Zeile 128–162.
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_pca_scale_effect() -> None: """Show the effect of scaling on PCA (Scale Dependency).""" df = load_cohort().merge(load_labs(), on="patient_id", how="left").dropna(subset=["alter", "kreatinin_mg_dl"]) X_raw = df[["alter", "kreatinin_mg_dl"]].values # Left: Without scaling pca_raw = PCA(n_components=2, random_state=SEED) X_raw_2d = pca_raw.fit_transform(X_raw) # Right: With scaling scaler = StandardScaler() X_scaled = scaler.fit_transform(X_raw) pca_scaled = PCA(n_components=2, random_state=SEED) X_scaled_2d = pca_scaled.fit_transform(X_scaled) fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4.5)) # Without scaling scatter sc1 = ax1.scatter(X_raw_2d[:, 0], X_raw_2d[:, 1], c=X_raw[:, 1], cmap="coolwarm", alpha=0.7) ax1.set_xlabel("PC 1 (Dominiert vom Alter)") ax1.set_ylabel("PC 2") ax1.set_title("Ohne Standardisierung\n(Alter dominiert wegen großer Varianz)") fig.colorbar(sc1, ax=ax1, label="Kreatinin (mg/dl)") ax1.grid(True, linestyle=":", alpha=0.6) # With scaling scatter sc2 = ax2.scatter(X_scaled_2d[:, 0], X_scaled_2d[:, 1], c=X_raw[:, 1], cmap="coolwarm", alpha=0.7) ax2.set_xlabel("PC 1") ax2.set_ylabel("PC 2") ax2.set_title("Mit Standardisierung (StandardScaler)\n(Beide Merkmale tragen gleichwertig bei)") fig.colorbar(sc2, ax=ax2, label="Kreatinin (mg/dl)") ax2.grid(True, linestyle=":", alpha=0.6) plt.tight_layout() save(fig, ASSETS / "pca_scale_effect.png")