Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Visualizing Explained Variance and Component Loadings | Section
Principal Component Analysis Fundamentals

bookVisualizing Explained Variance and Component Loadings

Sveip for å vise menyen

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

Velg det helt riktige svaret

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 1. Kapittel 10

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Seksjon 1. Kapittel 10
some-alt