-
Notifications
You must be signed in to change notification settings - Fork 21
/
gaussian_discriminant_analysis.py
45 lines (38 loc) · 1.35 KB
/
gaussian_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
import numpy as np
from scipy.stats import multivariate_normal
class GDA:
def fit(self, X, y):
'''
Parameters
----------
X : shape (n_samples, n_features)
Training data
y : shape (n_samples,)
Target labels
'''
n_samples, n_features = X.shape
self.__classes = np.unique(y)
n_classes = len(self.__classes)
self.__phi = np.zeros((n_classes, 1))
self.__means = np.zeros((n_classes, n_features))
self.__sigma = 0
for i in range(n_classes):
indexes = np.flatnonzero(y == self.__classes[i])
self.__phi[i] = len(indexes) / n_samples
self.__means[i] = np.mean(X[indexes], axis=0)
self.__sigma += np.cov(X[indexes].T) * (len(indexes) - 1)
self.__sigma /= n_samples
def predict(self, X):
'''
Parameters
----------
X : shape (n_samples, n_features)
Predicting data
Returns
-------
y : shape (n_samples,)
Predicted class label per sample.
'''
pdf = lambda mean: multivariate_normal.pdf(X, mean=mean, cov=self.__sigma)
y_probs = np.apply_along_axis(pdf, 1, self.__means) * self.__phi
return self.__classes[np.argmax(y_probs, axis=0)]