Visualizing Explained Variance and Component Loadings
Veeg om het menu te tonen
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.
Was alles duidelijk?
Bedankt voor je feedback!
Sectie 1. Hoofdstuk 10
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Sectie 1. Hoofdstuk 10