Visualizing Explained Variance and Component Loadings
Swipe um das Menü anzuzeigen
After fitting PCA, it's important to understand how much information (variance) each principal component captures. The explained variance ratio tells you this. You can also inspect the component loadings to see how original features contribute to each principal component.
1234567891011121314151617181920212223242526272829303132333435import numpy as np import pandas as pd from sklearn.datasets import load_iris from sklearn.preprocessing import StandardScaler from sklearn.decomposition import PCA import matplotlib.pyplot as plt import seaborn as sns # Load the Iris dataset data = load_iris() X = data.data feature_names = data.feature_names # Standardize features (important for PCA) scaler = StandardScaler() X_scaled = scaler.fit_transform(X) # Apply PCA to reduce to 2 components pca = PCA(n_components=2) X_pca = pca.fit_transform(X_scaled) # Plot explained variance ratio plt.figure(figsize=(6,4)) plt.bar(range(1, len(pca.explained_variance_ratio_)+1), pca.explained_variance_ratio_, alpha=0.7) plt.ylabel('Explained Variance Ratio') plt.xlabel('Principal Component') plt.title('Explained Variance by Principal Components') plt.show() # Display component loadings as a heatmap loadings = pd.DataFrame(pca.components_.T, columns=['PC1', 'PC2'], index=feature_names) plt.figure(figsize=(6,4)) sns.heatmap(loadings, annot=True, cmap='coolwarm') plt.title('Principal Component Loadings') plt.show()
The bar plot shows the proportion of variance explained by each principal component. The heatmap displays the loadings, which indicate how much each original feature contributes to each principal component. Large absolute values mean a feature is important for that component.
War alles klar?
Danke für Ihr Feedback!
Abschnitt 1. Kapitel 10
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Abschnitt 1. Kapitel 10