-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaive_bayes.py
61 lines (42 loc) · 1.67 KB
/
naive_bayes.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
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 29 10:57:04 2023
@author: iason
we will use the Gaussian Naive Bayes
"""
#%% NaiveBayes
import numpy as np
class NaiveBayes:
def fit(self , X , y) :
n_samples , n_features = X.shape
self._classes = np.unique(y)
n_classes = len(self._classes)
#calculate mean , var and prior for each class
self._mean = np.zeros(n_classes , n_features )
self._var = np.zeros(n_classes , n_features )
self._priors = np.zeros(n_classes)
for i , c in enumerate(self._classes):
X_c = X[y==c] #samples of the class
self._mean[i, :] = X_c.mean(axis=0)
self._var[i, :] = X_c.var(axis=0)
self._priors[i] = X_c.shape[0] / float(n_samples)
def predict(self , X) :
y_pred = [self._predict(x) for x in X]
return np.array(y_pred)
def _predict(self , x) :
posteriors = []
# caclculate th eposterior probabiliti for each class
for i , c in enumerate(self._classes) :
prior = np.log(self._priors[i])
posterior = np.sum(np.log(self._pdf(i , x)))
posterior = posterior + prior
posteriors.append(posterior)
#return the class with the highest posterior
return self._classes[np.argmax(posteriors)]
def _pdf(self , class_i , x):
mean = self._mean[class_i]
var = self._var[class_i]
numerator = np.exp(-((x-mean)**2 /( 2 * var)))
denominator = np.sqrt(2 * np.pi * var)
return numerator/denominator
#%%