-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdim_reduction.py
97 lines (85 loc) · 2.6 KB
/
dim_reduction.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import numpy as np
class PCA:
w = ''
tol = 0
eigenvalues = ''
eigenvectors = ''
mean = ''
decomp = ''
def __init__(self, decomp='eig'):
self.w = 0
self.tol = 0
self.decomp = decomp
def reset(self):
self.w = 0
self.tol = 0
def get_w(self):
return self.w
def get_tol(self):
return self.tol
def setup(self, x, tol):
self.tol = tol
sigma = np.zeros((x.shape[0],x.shape[0]))
mean = np.mean(x, axis=1).reshape((x.shape[0], 1))
self.mean = mean
eigval = 0
eigvec = 0
if self.decomp == 'eig':
for i in range(0, x.shape[1]):
diff = x[:,i].reshape((x.shape[0],1)) - mean
sigma += diff.dot(diff.T)
sigma = (1/x.shape[1])*sigma
eigval,eigvec = np.linalg.eig(sigma)
else:
u,s,vt = np.linalg.svd(x.T,full_matrices=False)
eigval = s*s/(x.shape[1]-1)
eigvec = vt.T
eigsum = np.sum(eigval)
order = np.argsort(eigval)[::-1]
eigvec = eigvec[:,order]
eigval = eigval[order]
self.eigenvalues = eigval
self.eigenvectors = eigvec
n = x.shape[0]
# print(n)
while np.sum(eigval[0:n])/eigsum > self.tol:
n -= 1
n+=1
# print(n)
self.w = eigvec[:,0:n]
def reduce(self, data):
return self.w.T.dot(data)
class FLD:
w = ''
sigma = ''
mean0 = 0
mean1 = 0
s0 = 0
s1 = 0
def __init__(self):
self.w = 0
def reset(self):
self.w = 0
def setup(self, x, xclass):
num_var = x.shape[0]
samples0 = np.array([x[:,i] for i in range(0, x.shape[1]) if xclass[i] == 0]).T
samples1 = np.array([x[:,i] for i in range(0, x.shape[1]) if xclass[i] == 1]).T
mean0 = np.mean(samples0, axis=1).reshape((num_var, 1))
mean1 = np.mean(samples1, axis=1).reshape((num_var, 1))
self.mean0 = mean0
self.mean1 = mean1
S0 = np.zeros((num_var, num_var))
S1 = np.zeros((num_var, num_var))
for i in range(0, samples0.shape[1]):
diff = samples0[:,i].reshape((num_var,1)) - mean0
S0 = np.add(np.dot(diff, diff.T), S0)
for i in range(0, samples1.shape[1]):
diff = samples1[:,i].reshape((num_var,1)) - mean1
S1 = np.add(np.dot(diff, diff.T), S1)
self.s0 = S0
self.s1 = S1
Sw = S0 + S1
self.sigma = Sw
self.w = np.linalg.inv(Sw).dot(mean0-mean1)
def reduce(self, data):
return self.w.T.dot(data)