-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaussian.py
35 lines (29 loc) · 1.06 KB
/
gaussian.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
class GaussianNB:
def fit(self, X, t):
self.priors = dict()
self.means = dict()
self.covs = dict()
self.classes = np.unique(t)
for c in self.classes:
X_c = X[t == c]
self.priors[c] = X_c.shape[0] / X.shape[0]
self.means[c] = np.mean(X_c, axis=0)
self.covs[c] = np.diag(np.diag(np.cov(X_c, rowvar=False)))
def predict(self, X):
preds = list()
for x in X:
posts = list()
for c in self.classes:
prior = np.log(self.priors[c])
inv_cov = np.linalg.inv(self.covs[c])
inv_cov_det = np.linalg.det(inv_cov)
diff = x-self.means[c]
likelihood = 0.5*np.log(inv_cov_det) - 0.5*diff.T @ inv_cov @ diff
post = prior + likelihood
posts.append(post)
pred = self.classes[np.argmax(posts)]
preds.append(pred)
return np.array(preds)