-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCA.py
More file actions
41 lines (28 loc) · 871 Bytes
/
PCA.py
File metadata and controls
41 lines (28 loc) · 871 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import numpy as np
class Pca():
def __init__(self,x):
self.x = x
self.m = x.shape[0]
def sigEval(self):
"""
Covariance matrix
"""
self.sigma = (1/self.m) * np.dot(np.transpose(self.x),self.x)
def dim_reduction(self, k):
"""
Reducing dimensions from n(input) to k using PCA
"""
self.sigEval()
self.u, self.s, _ = np.linalg.svd(self.sigma)
self.u_red = self.u[:,:k]
self.z = np.dot(self.x,self.u_red)
covar_retain = (np.sum(self.s[:k])/np.sum(self.s[:]))*100
print(covar_retain,"percent of covariance retained")
return self.z
def mean_normalization(self):
"""
Feature scaling using mean normalization method
"""
self.avg = np.mean(self.x, axis = 0)
self.ran = np.max(self.x, axis = 0)-np.min(self.x, axis = 0)
self.x = (self.x - self.avg)/self.ran