-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPCA.py
61 lines (43 loc) · 1.17 KB
/
PCA.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Author: Ian Russell
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from PIL import Image
import scipy.ndimage
class PCA:
def __init__(self, A, d_reduction):
"""
Pass in numpy array to instantiate PCA parameters
"""
self.A = A
self.d_reduction = d_reduction
def decompose(self):
"""
Function to perform singular value decompostion.
Returns eigen values and vectors.
"""
# Center Data
mean = np.mean(self.A, axis=0)
centered = self.A-mean
# Compute covariance and corresponding eigen decomp.
U, S, V = np.linalg.svd(centered)
return U, S, V
def reduce(self):
"""
Function to perform dimensionality reduction.
Returns projection array for corresponding
dimensionality reduction.
"""
res = self.decompose()
U = res[0]
S = res[1]
V = res[2]
# Decompose data matrix
components = V[:self.d_reduction]
projected = U[:,:self.d_reduction]*S[:self.d_reduction]
return projected, components
def eigface(self, faces):
v = self.reduce()[1]
for i in range(faces):
plt.imshow(scipy.ndimage.rotate((v[i].reshape(30,30)), -90), interpolation='nearest', cmap='gray')
plt.show()