-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearDiscriminantAnalysis.py
73 lines (55 loc) · 2.08 KB
/
LinearDiscriminantAnalysis.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
import numpy as np
class LDA:
"""
Classification (multi-class) usign linear discriminant analysis.
Args:
X : numpy array of dimension Nxp, where N is the number of trainig examples
and p is the number of predictors.
Y : numpy array of dimension Nx1, where N is the number of training examples.
"""
def __init__(self, X, Y):
self.X = X
self.Y = Y
# Useful quantities
self.n = self.X.shape[0]
self.p = self.X.shape[1]
self.mean = {}
self.counter = {}
self.covar = {}
self.pi = {}
self.X_sorted = sorted(self.X.tolist(), key=lambda x: self.Y[self.X.tolist().index(x)])
self.X_sorted = np.array(self.X_sorted)
self.predictions = 0
def estimate_mean_covar_prior(self):
self.classes = sorted(list(set(self.Y)))
self.K = len(self.classes)
self.Sigma = np.zeros((self.p, self.p))
for i in self.classes:
self.counter[i] = 0
self.mean[i] = 0
for y in self.Y:
self.counter[y] += 1
f = 0
for y in self.classes:
m = self.X_sorted[f: f+ self.counter[y]]
self.mean[y] = np.mean(m, axis=0)
self.covar[y] = np.cov(m.T)
self.pi[y] = self.counter[y] / self.X.shape[0]
f = f + self.counter[y]
for y in self.classes:
self.Sigma += self.covar[y]
self.Sigma = self.Sigma * 1/(self.n - self.K)
def predict_one(self, x):
probs = []
for y in self.classes:
delta = np.matmul(x.T, np.matmul(np.linalg.inv(self.Sigma), self.mean[y])) -\
1/2 * np.matmul(self.mean[y].T, np.matmul(np.linalg.inv(self.Sigma), self.mean[y])) + \
np.log(self.pi[y])
probs.append(delta)
return self.classes[probs.index(max(probs))]
def predict(self):
self.estimate_mean_covar_prior()
predictions = []
for x in self.X:
predictions.append(self.predict_one(x))
self.predictions = np.array(predictions)