-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlda.py
35 lines (24 loc) · 1.01 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
import numpy as np
class LinearDiscriminantAnalysis:
def fit(self, X, y):
self.labels, self.class_priors = np.unique(y, return_counts=True)
self.class_priors = self.class_priors / y.shape[0]
self.cov = np.cov(X.T)
self.Mu = []
for k in range(len(self.labels)):
X_k = X[y==self.labels[k]]
self.Mu.append(np.mean(X_k, axis=0))
def predict(self, X):
labels = []
for i in range(X.shape[0]):
labels.append(self.predict_sample(X[i]))
return np.array(labels)
def predict_sample(self, X):
max_label = 0
max_likelihood = 0
for k in range(len(self.labels)):
likelihood = np.exp(-1/2 * (X - self.Mu[k]).T @ np.linalg.inv(self.cov) @ (X - self.Mu[k]))
if likelihood > max_likelihood:
max_label = self.labels[k]
max_likelihood = likelihood
return max_label