Skip to content

Commit

Permalink
Add PCA from Scratch
Browse files Browse the repository at this point in the history
  • Loading branch information
ankit2001 committed May 23, 2020
1 parent b904c59 commit 1d369cc
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Principal Component Analysis (Dimensionality Reduction)/PCA.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import numpy as np

class PCA:
def __init__(self, reduced_to_k):
self.reduced_to_k = reduced_to_k
self.Mean = None
self.final_components = None

def fit(self, X):
self.Mean = np.mean(X, axis = 0)
X = X - self.Mean
covariance = np.cov(X.T)
evalues, evectors = np.linalg.eig(covariance)
evectors = evectors.T
indexes_maxi_k = np.argsort(evalues)[ : : -1]
evectors = evector[indexes_maxi_k]
self.final_components = evectors[0 : self.reduced_to_k]

def transform(self, X):
X = X - self.Mean
FINAL_COMPONENT_MATRIX = np.dot(X, self.final_components.T)
return FINAL_COMPONENT_MATRIX


0 comments on commit 1d369cc

Please sign in to comment.