-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtility.py
47 lines (38 loc) · 1.22 KB
/
Utility.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
import numpy as np
def PCA(m, n):
"""
Computes the principal component analysis of the input matrix
Parameters: m: numpy array to be decomposed
n: number of components
Returns: A numpy array of Eigenvectors and a python list of Eigenvalues
"""
mean = np.mean(m, axis=1)
m = m - np.tile(mean, (n, 1)).T
if m.shape[1] <= m.shape[0]:
C = np.dot(np.transpose(m), m)
eigvals, eigvectors = np.linalg.eigh(C)
eigvectors = np.dot(m, eigvectors)
for i in range(0, m.shape[1]):
eigvectors[:,i] = eigvectors[:,i]/np.linalg.norm(eigvectors[:,i])
else:
C = np.dot(m, np.transpose(m))
eigvals, eigvectors = np.linalg.eigh(C)
eigvectors = eigvectors[:,np.argsort(eigvals)]
return eigvals, eigvectors, mean
def Normalize(m, low=0, high=255):
"""
Normalizes a numpy array
"""
min, max = np.min(m), np.max(m)
m = m - min
m = m / (max - min)
m = m * (high - low)
m = m + low
return m
def Project(x, w, mean=0):
scalars = np.dot((x-mean).T, w)
#return scalars
proj = np.zeros(shape=(x.shape[0]))
for i in xrange(w.shape[1]):
proj = proj + scalars[i] * w[:,i]
return proj