-
Notifications
You must be signed in to change notification settings - Fork 21
/
linear_discriminant_analysis.py
60 lines (50 loc) · 1.57 KB
/
linear_discriminant_analysis.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
import numpy as np
import metrics
class LDA:
def __init__(self, n_components = None):
'''
Parameters
----------
n_components : Number of components to keep
'''
self.__n_components = n_components
def fit_transform(self, X, y):
'''
Parameters
----------
X : shape (n_samples, n_features)
Training data
y : shape (n_samples,)
Data label
Returns
-------
X : shape (n_samples, n_components)
The data of dimensionality reduction
'''
n_samples, n_features = X.shape
classes = np.unique(y)
n_classes = len(classes)
s_t = np.cov(X.T) * (n_samples - 1)
s_w = 0
for i in range(n_classes):
items = np.flatnonzero(y == classes[i])
s_w += np.cov(X[items].T) * (len(items) - 1)
s_b = s_t - s_w
eig_values, eig_vectors = np.linalg.eigh(np.linalg.pinv(s_w).dot(s_b))
self.__eig_vectors = eig_vectors[:, ::-1][:, :self.__n_components]
pc = self.transform(X)
if self.__n_components == 2:
metrics.scatter_feature(pc, y)
return pc
def transform(self, X):
'''
Parameters
----------
X : shape (n_samples, n_features)
Predicting data
Returns
-------
X : shape (n_samples, n_components)
The data of dimensionality reduction
'''
return X.dot(self.__eig_vectors)