Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dimensionality-reduction/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from pca import pca
56 changes: 56 additions & 0 deletions dimensionality-reduction/pca.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import numpy as np
from numpy import linalg as la


class pca():

"""
PCA is a dimendionality reduction technique.
Linear dimensionality reduction using Singular Value Decomposition of the data to project it to a lower dimensional space

"""

def __init__(self):
self.cov=cov
self.eigen=eigen
self.eigval=None
self.eigvct=None
self.covar=None
self.princicomp=None

def cov(self,x):

"""
first we have to find the covariance matrix of the given array/dataframe

"""
self.covar=np.cov(x)

return self.covar

def eigen(self,x):

"""
Second step is to find eigen values and vectors of the given array

"""
self.eigval, self.eigvct = np.la.eig(self.covar)

return self.eigval,self.eigvct

def ncomp(self,numcomponents):

"""
Method to choose number of principal components

"""
self.princicomp=self.eigvct[0:numcomponents]

"""
For examples, if numcomponents=2 then this function returns first 2 rows of eigenvectors

"""

return self.princicomp


5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
numpy
matplotlib
pandas
collections
os