This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NaiveBaysian.py
186 lines (149 loc) · 6.37 KB
/
NaiveBaysian.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import numpy as np
"""
An implementation of Gaussian Naive Bayesian Classifier
@author: Sotheanith Sok, Jesse Blacklock
@class: CECS 550
@instructor: Arjang Fahim
@date: 9/23/2019
"""
class GaussianNaiveBayesianClassifier(object):
def __init__(self, shuffle=False, ratio=2.0 / 3.0):
"""Initialize the object, load data, and start to predict the result.
Keyword Arguments:
shuffle {bool} -- Determine if training set and test set should be randomly pick from data set. Load predetermined training set and test set by default. (default: {False})
ratio {float} -- Determine the ratio between training set and test set. Only applicable if shuffle is true (default: {2.0/3.0})
"""
super().__init__()
# Load data
self.trainingSet, self.testSet = self.loadData(shuffle)
# Calculate Prior probability
self.pY = (
np.count_nonzero(self.trainingSet[:, 8]) / np.shape(self.trainingSet)[0]
)
# Calculate likelihood of Y=1
self.meanPY, self.variancePY = self.calculateMeanAndVariance(
self.trainingSet[self.trainingSet[:, 8] == 1][:, :8]
)
# Calculate likelihood of Y=0
self.meanPNotY, self.VariancePNotY = self.calculateMeanAndVariance(
self.trainingSet[self.trainingSet[:, 8] == 0][:, :8]
)
# Initialize performance trackers to zero
self.TN, self.FP, self.FN, self.TP = 0, 0, 0, 0
# Start predicting on test set
self.predict()
# Calculate metrics and print them out
self.accuracy = 0
self.error = 0
self.sensitivity = 0
self.specificity = 0
self.calculateMetrics()
def loadData(self, shuffle=False, ratio=2.0 / 3.0):
"""Load data into our object from files
Keyword Arguments:
shuffle {bool} -- Determine if training set and test set should be randomly pick from data set. Load predetermined training set and test set by default. (default: {False})
ratio {float} -- Determine the ratio between training set and test set. Only applicable if shuffle is true (default: {2.0/3.0})
"""
# For non-shuffling data, use predetermine testing and training sets
if shuffle == False:
testSet = np.genfromtxt("./data/test.csv", delimiter=",")
trainingSet = np.genfromtxt("./data/train.csv", delimiter=",")
return trainingSet, testSet
# Shuffle entire dataset and split base on ratio if required
else:
dataSet = np.genfromtxt(
"./data/pima-indians-diabetes.data.csv", delimiter=","
)
np.random.shuffle(dataSet)
splitTarget = int(np.shape(dataSet)[0] * ratio)
return dataSet[:splitTarget], dataSet[splitTarget:]
def calculateMeanAndVariance(self, input):
"""Calculate means and variances column wise
Arguments:
input {2d array} -- An array of features set
"""
mean = np.mean(input, axis=0)
variance = np.var(input, axis=0)
return mean, variance
def calculateGaussianPDF(self, x, mean, variance):
"""Calcualte gaussian probability density of a single feature
Arguments:
x {float} -- Value of a feature
mean {float} -- mean of other similar features
variance {float} -- variance of other similar features
Returns:
float -- Gaussian probability density
"""
exponent = np.exp(-(np.power(x - mean, 2) / (2 * variance)))
return (1 / (np.sqrt(2 * np.pi * variance))) * exponent
def predict(self):
"""
Predicts the test set and records results
"""
for X in self.testSet:
probYX, probNotYX = 0, 0
for i in range(X.size - 1):
# Calculate P(X|Y=1)
probXY = self.calculateGaussianPDF(
X[i], self.meanPY[i], self.variancePY[i]
)
if probXY > 0:
probYX = np.log(probXY) + probYX # logarithmic estimation
# Calculate P(X|Y=0)
probXNotY = self.calculateGaussianPDF(
X[i], self.meanPNotY[i], self.VariancePNotY[i]
)
if probXNotY > 0:
# logarithmic estimation
probNotYX = np.log(probXNotY) + probNotYX
# Calculate P(Y=1|X) = P(Y=1|X) * P(Y=1)
# P(Y=1|X) = log(P(Y=1|X) * P(Y=1)) = log(P(Y=1|X)) + log(P(Y=1))
probYX = probYX + np.log(self.pY)
# Calculate P(Y=0|X) = P(Y=0|X) * P(Y=0)
probNotYX = probNotYX + np.log(1 - self.pY)
# Make the decision
predict = 1 if probYX > probNotYX else 0
# Record result
if predict == 0 and X[8] == 0:
self.TN = self.TN + 1
elif predict == 0 and X[8] == 1:
self.FN = self.FN + 1
elif predict == 1 and X[8] == 0:
self.FP = self.FP + 1
else:
self.TP = self.TP + 1
def calculateMetrics(self):
"""
Calcualte metrics such as accuracy and print out those matrics
"""
self.accuracy = (self.TP + self.TN) / (self.TP + self.FP + self.TN + self.FN)
self.error = (self.FP + self.FN) / (self.TP + self.FP + self.TN + self.FN)
self.sensitivity = self.TP / (self.FN + self.TP)
self.specificity = self.TN / (self.TN + self.FP)
# Print results
print("Accuracy:", self.accuracy)
print("Error:", self.error)
print("Sensitivity:", self.sensitivity)
print("Specificity:", self.specificity)
# Run n times and average the results
accuracy = 0
error = 0
sensitivity = 0
specificity = 0
for i in range(100):
# Keep track of trials
print("Trial ", i, " :")
# Shuffle input data and split at random ratio
gnb = GaussianNaiveBayesianClassifier(True, np.random.random_sample())
# Record results
accuracy = accuracy + gnb.accuracy
sensitivity = sensitivity + gnb.sensitivity
error = error + gnb.error
specificity = specificity + gnb.specificity
print("-------------------------------------")
i=i+1
print("Final results: ")
print("Accuracy:", accuracy / i)
print("Error:", error / i)
print("Sensitivity:", sensitivity / i)
print("Specificity:", specificity / i)