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

bookVisualizing Explained Variance and Component Loadings

Pyyhkäise näyttääksesi valikon

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

Valitse oikea vastaus

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 10

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Osio 1. Luku 10
some-alt