Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Visualizing Explained Variance and Component Loadings | Implementing PCA in Python
Dimensionality Reduction with PCA

bookVisualizing Explained Variance and Component Loadings

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.

1234567891011121314151617181920212223242526272829303132333435
import 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()
copy

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.

question mark

What does a large absolute value in a component loading matrix indicate about a feature's relationship to a principal component in PCA

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 8.33

bookVisualizing Explained Variance and Component Loadings

Swipe to show menu

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.

1234567891011121314151617181920212223242526272829303132333435
import 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()
copy

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.

question mark

What does a large absolute value in a component loading matrix indicate about a feature's relationship to a principal component in PCA

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2
some-alt