utils.pca.get_loading_scores
utils.pca.get_loading_scores(pca, feature_names)Computes the loading scores matrix for Principal Component Analysis (PCA).
Creates and returns a DataFrame showing how each original feature contributes to each principal component.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| pca | sklearn.decomposition.PCA |
Fitted PCA object containing the components_ attribute with the principal components. | required |
| feature_names | list - like |
Names of the original features, must match the order of features used in PCA fitting. | required |
Returns
| Name | Type | Description |
|---|---|---|
| pd.DataFrame | pd.DataFrame: DataFrame containing the loading scores matrix with features as rows and principal components as columns. |
Example
from sklearn.decomposition import PCA from sklearn.datasets import load_iris from spotpython.utils.pca import print_loading_scores,
Load and prepare iris dataset
iris = load_iris() X = iris.data feature_names = iris.feature_names
Fit PCA
pca = PCA() pca.fit(X)
Print loading scores
scores_df = print_loading_scores(pca, feature_names) print(scores_df)