forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Naive_Bayes_from_Scratch.py
128 lines (94 loc) · 3.98 KB
/
Naive_Bayes_from_Scratch.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Naive Bayes Algorithm
import numpy as np
class NaiveBayesClassifier:
def __init__(self):
pass
# divides the dataset into a subset of data belonging to each class
def divide_classes(self, X, Y):
"""
X: list of features
Y: list consisting of target
The function returns: A dictionary with Y as keys and assigned X as values.
"""
divided_classes = {}
for i in range(len(X)):
values = X[i]
target_class_name = Y[i]
if target_class_name not in divided_classes:
divided_classes[target_class_name] = []
divided_classes[target_class_name].append(values)
return divided_classes
# standard deviation and mean are required for the (Gaussian) distribution function
def info(self, X):
"""
X: list of features
The function returns: A dictionary with standard deviation and mean as keys and assigned features as values.
"""
for i in zip(*X):
yield {
'std' : np.std(i),
'mean' : np.mean(i)
}
# fitting data that would be required to train the model
def fit_data (self, X, Y):
"""
X: training features
y: target variable
The function returns: A dictionary with the probability, mean, and standard deviation of each class.
"""
divided_classes = self.divide_classes(X, Y)
self.summary = {}
for target_class_name, values in divided_classes.items():
self.summary[target_class_name] = {
'given_prob': len(values)/len(X),
'summary': [i for i in self.info(values)]
}
return self.class_summary
# Gaussian distribution function
def Gaussian_distribution(self, X, mean, std):
"""
X: value of feature
mean: the average value of feature
stdev: the standard deviation of feature
The function returns: A value of normal probability.
"""
exponent = np.exp(-((X-mean)**2 / (2*std**2)))
return exponent / (np.sqrt(2*np.pi)*std)
# finally predicting the class
def predict(self, X):
"""
X: test dataset
The function returns: List of predicted class for each row of dataset.
"""
# Maximum a posteriori (MAP): In Bayesian statistics, a maximum a posteriori
# probability (MAP) estimate is an estimate of an unknown quantity, that equals
# the mode of the posterior distribution.
MAPs = []
for i in X:
joint_prob = {}
for target_class_name, values in self.summary.items():
total_values = len(values['summary'])
likelihood = 1
for idx in range(total_values):
value = i[idx]
mean = value['summary'][idx]['mean']
stdev = value['summary'][idx]['std']
normal_prob = self.Gaussian_distribution(value, mean, stdev)
likelihood *= normal_prob
prior_prob = values['prior_proba']
joint_prob[target_class_name] = prior_prob * likelihood
MAP = max(joint_prob, key= joint_prob.get)
MAPs.append(MAP)
return MAPs
# calculating accuracy
def model_accuracy(self, y_test, y_pred):
"""
y_test: actual values
y_pred: predicted values
The function returns: A number between 0-1, representing the percentage of correct predictions.
"""
correct_true = 0
for y_t, y_p in zip(y_test, y_pred):
if y_t == y_p:
correct_true += 1
return correct_true / len(y_test)