-
Notifications
You must be signed in to change notification settings - Fork 0
/
LDA.py
49 lines (39 loc) · 1.59 KB
/
LDA.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
import numpy as np
class LDA:
def __init__(self, n_components):
self.n_components = n_components
self.linear_discriminants = None
def fit(self, X, y):
n_features = X.shape[1]
class_labels = np.unique(y)
# Within class scatter matrix:
# SW = sum((X_c - mean_X_c)^2 )
# Between class scatter:
# SB = sum( n_c * (mean_X_c - mean_overall)^2 )
mean_overall = np.mean(X, axis=0)
SW = np.zeros((n_features, n_features))
SB = np.zeros((n_features, n_features))
for c in class_labels:
X_c = X[y == c]
mean_c = np.mean(X_c, axis=0)
SW += (X_c - mean_c).T.dot((X_c - mean_c))
n_c = X_c.shape[0]
mean_diff = (mean_c - mean_overall).reshape(n_features, 1)
SB += n_c * (mean_diff).dot(mean_diff.T)
# Determine SW^-1 * SB
A = np.linalg.pinv(SW).dot(SB)
# Get eigenvalues and eigenvectors of SW^-1 * SB
eigenvalues, eigenvectors = np.linalg.eigh(A)
# -> eigenvector v = [:,i] column vector, transpose for easier calculations
# sort eigenvalues high to low
eigenvectors = eigenvectors.T
idxs = np.argsort(abs(eigenvalues))[::-1]
eigenvalues = eigenvalues[idxs]
eigenvectors = eigenvectors[idxs]
# store first n eigenvectors
self.linear_discriminants = eigenvectors[0:self.n_components]
return self.linear_discriminants
def transform(self, X):
# project data
X = X - np.mean(X, axis=0)
return np.dot(X, self.linear_discriminants.T)