Skip to content

Instantly share code, notes, and snippets.

@justagist
Created August 14, 2024 09:12
Show Gist options
  • Save justagist/c7f3eabbe4ebcd1460e86b83ce0e53b7 to your computer and use it in GitHub Desktop.
Save justagist/c7f3eabbe4ebcd1460e86b83ce0e53b7 to your computer and use it in GitHub Desktop.
principal component analysis in code
import numpy as np
def PCA(X , num_components):
#Step-1
X_meaned = X - np.mean(X , axis = 0)
#Step-2
cov_mat = np.cov(X_meaned , rowvar = False)
#Step-3
eigen_values , eigen_vectors = np.linalg.eigh(cov_mat)
#Step-4
sorted_index = np.argsort(eigen_values)[::-1]
sorted_eigenvalue = eigen_values[sorted_index]
sorted_eigenvectors = eigen_vectors[:,sorted_index]
#Step-5
eigenvector_subset = sorted_eigenvectors[:,0:num_components]
#Step-6
X_reduced = np.dot(eigenvector_subset.transpose() , X_meaned.transpose() ).transpose()
return X_reduced
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment