Skip to content

Instantly share code, notes, and snippets.

@smzn
Created January 25, 2024 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smzn/82104cac24976ac9f794d4c8ad820168 to your computer and use it in GitHub Desktop.
Save smzn/82104cac24976ac9f794d4c8ad820168 to your computer and use it in GitHub Desktop.
主成分分析
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
# Excluding the target variable 'Diabetes_binary' for PCA
features = data.drop('Diabetes_binary', axis=1)
# Scaling the data
scaler = StandardScaler()
scaled_features = scaler.fit_transform(features)
# Performing PCA
pca = PCA()
principal_components = pca.fit_transform(scaled_features)
# Explained variance ratio for each principal component
explained_variance_ratio = pca.explained_variance_ratio_
# Calculating the cumulative variance
cumulative_variance = explained_variance_ratio.cumsum()
# Finding the number of components to reach 80% cumulative variance
n_components_80 = (cumulative_variance >= 0.80).argmax() + 1
# Performing PCA with the desired number of components
pca_80 = PCA(n_components=n_components_80)
principal_components_80 = pca_80.fit_transform(scaled_features)
# Creating a DataFrame for the principal components
principal_df_80 = pd.DataFrame(data=principal_components_80, columns=[f'PC{i}' for i in range(n_components_80)])
# Calculating loadings (correlation between original features and the selected principal components)
loadings_80 = pca_80.components_.T * np.sqrt(pca_80.explained_variance_)
# Creating a DataFrame for the loadings
loadings_df_80 = pd.DataFrame(data=loadings_80, columns=[f'PC{i}' for i in range(n_components_80)], index=features.columns)
# Transposing the loadings DataFrame to swap rows and columns
loadings_df_80_transposed = loadings_df_80.transpose()
# Display the transposed loadings DataFrame
loadings_df_80_transposed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment