-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLDA.py
More file actions
39 lines (33 loc) · 1.16 KB
/
LDA.py
File metadata and controls
39 lines (33 loc) · 1.16 KB
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
import numpy as np
from scipy import linalg as LA
class LDA(object):
def __init__(self, data_inputs, data_labels):
self.data_inputs = np.array(data_inputs)
self.data_labels = data_labels
self.test_cases = self.data_inputs.shape[0]
self.labels = np.unique(data_labels)
self.Sw = np.zeros((self.data_inputs.shape[1], self.data_inputs.shape[1]))
self.Sb = self.Sw.copy()
def analyse(self):
C = np.cov(self.data_inputs.T)
for label in self.labels:
indices = np.where(self.data_labels == label)
points = self.data_inputs[indices[0]]
classcov = np.cov(points.T)
self.Sw += (np.float(points.shape[0])/self.test_cases) * classcov
self.Sb = C - self.Sw
evals, evecs = LA.eig(self.Sw, self.Sb)
indices = np.argsort(evals)
indices = indices[::-1]
evals = evals[indices]
evecs = evecs[indices]
self.eigen_vals = evals
self.eigen_vecs = evecs
def reduce_dim(self, red_n, data_inputs=None):
w = self.eigen_vecs[:,:red_n]
if data_inputs is None:
data_inputs = self.data_inputs
return np.dot(data_inputs, w)
def expand_dim(self, red_data):
red_n = red_data.shape[1]
return np.transpose(np.dot(self.eigen_vecs[:,:red_n], red_data.T))