-
Notifications
You must be signed in to change notification settings - Fork 0
/
BNBC_functions.py
198 lines (133 loc) · 6.28 KB
/
BNBC_functions.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
186
187
188
189
190
191
192
193
194
195
196
197
198
import numpy as np
IG_TOP = 400
# P(X=1) * (1 - P(X=1))
VAR_THRESHOLD = 0.8 * 0.2
# Class separation function
def separate_classes(training_set, response):
class_0 = list()
class_1 = list()
for i in range(len(training_set)):
if response[i] == 0:
class_0.append(training_set[i])
else:
class_1.append(training_set[i])
class_0 = np.array(class_0)
class_1 = np.array(class_1)
return class_0, class_1
# Calculate entropy for 2 classes
def twoClassEntropy(class_Prob):
entropy = 0
if (class_Prob != 0) and (class_Prob != 1):
entropy = - (class_Prob * np.log2(class_Prob)) - ((1 - class_Prob) * np.log2(1 - class_Prob))
return entropy
# Calculate Information Gain for each feature
def calculate_IG(features, response):
examples = len(response)
InfoGain = list()
prior_1 = response.sum(axis=0) / len(response)
# Class 1 entropy
HC = twoClassEntropy(prior_1)
prob_feature_1 = list()
prob_feature_0_given_class_1 = list()
prob_feature_1_given_class_1 = list()
HC_11 = list()
HC_01 = list()
class_0, class_1 = separate_classes(np.transpose(features), response)
for i in range(len(features)):
feature_1_count = features[i].sum(axis=0)
feature_0_given_class_1_count = np.count_nonzero(class_1.T[i] == 0)
feature_1_given_class_1_count = np.count_nonzero(class_1.T[i] == 1)
# P(X=1)
prob_feature_1.append(feature_1_count / examples)
# P(X=1/C=1)
if feature_1_count == 0:
prob_feature_1_given_class_1.append(0)
else:
prob_feature_1_given_class_1.append(feature_1_given_class_1_count / feature_1_count)
# P(X=0/C=1)
if feature_1_count == examples:
prob_feature_0_given_class_1.append(0)
else:
prob_feature_0_given_class_1.append(feature_0_given_class_1_count / (examples - feature_1_count))
# P(X=0/C=1) entropy
HC_01.append(twoClassEntropy(prob_feature_0_given_class_1[i]))
# P(X=1/C=1) entropy
HC_11.append(twoClassEntropy(prob_feature_1_given_class_1[i]))
# IG formula...
InfoGain.append(HC - ((prob_feature_1[i] * HC_11[i]) + ((1 - prob_feature_1[i]) * HC_01[i])))
return InfoGain
# Return indices of the 'IG_TOP' features with the highest IG
def sort_features(features, response):
IG = calculate_IG(features, response)
highest_IG = list()
for i in range(IG_TOP):
highest_IG.append(np.argmax(IG))
IG = np.delete(IG, highest_IG[i])
return highest_IG
# Second method to sort the features (Do not use both..)
def sort_features_by_var(features):
high_var_features = list()
for i in range(len(features)):
var = np.var(features[i])
if var > VAR_THRESHOLD:
high_var_features.append(i)
return high_var_features
# Calculate prior log probability for each class (smoothed to avoid 0 values)
def calculate_prior_y(response):
prior_1 = (response.sum(axis=0) + 1) / (len(response) + 2)
return [np.log(1 - prior_1), np.log(prior_1)]
# Calculate log Likelihood for each feature (smoothed to avoid 0 values)
# returns a vector like [ [log(P(X=0/C=0)), log(P(X=1/C=0))],[log(P(X=0/C=1)), log(P(X=1/C=1))] ]
# we use logarithm to smooth the data and avoid floating errors
def calculate_log_likelihood(training_set, response):
class_0, class_1 = separate_classes(training_set, response)
log_likelihood_vector = list()
for i in range(len(np.transpose(training_set))):
log_likelihood_00 = np.log(np.count_nonzero(class_0.T[i] == 0) + 1) - np.log(len(class_0) + 2)
log_likelihood_10 = np.log(np.count_nonzero(class_0.T[i] == 1) + 1) - np.log(len(class_0) + 2)
log_likelihood_01 = np.log(np.count_nonzero(class_1.T[i] == 0) + 1) - np.log(len(class_1) + 2)
log_likelihood_11 = np.log(np.count_nonzero(class_1.T[i] == 1) + 1) - np.log(len(class_1) + 2)
log_likelihood_vector.append([[log_likelihood_00, log_likelihood_10], [log_likelihood_01, log_likelihood_11]])
return np.array(log_likelihood_vector)
# Calculate log post probability log(P(C=0/X=<...>)) and log(P(C=1/X=<...>)) for each example
# and compare to make final prediction
def fit_and_predict(testing_set, response, sorted_features, training_set):
classes = np.array([0, 1])
predictions = list()
training_set = np.transpose(training_set)[sorted_features]
testing_set = np.transpose(testing_set)[sorted_features]
log_prior = calculate_prior_y(response)
log_likelihood = calculate_log_likelihood(training_set.T, response)
for test_obs in testing_set.T:
likelihood_vector = [1] * len(classes)
for j in range(len(classes)):
for f in range(len(sorted_features)):
# feature_index = int(feature)
feature_value = int(test_obs[f])
likelihood_vector[j] += log_likelihood[f][j][feature_value]
log_post_prob = [1] * len(classes)
for j in range(len(classes)):
log_post_prob[j] = likelihood_vector[j] + log_prior[j]
predictions.append(np.argmax(log_post_prob))
return np.array(predictions)
# Calculate the metrics to evaluate the model
def calculate_metrics(y_prediction_set, y_real):
corrects = 0
true_positives = 0
false_positives = 0
false_negatives = 0
for i in range(len(y_prediction_set)):
if y_prediction_set[i] == y_real[i]:
corrects += 1
if (y_prediction_set[i] == 1) and (y_real[i] == 1):
true_positives += 1
if (y_prediction_set[i] == 1) and (y_real[i] == 0):
false_positives += 1
if (y_prediction_set[i] == 0) and (y_real[i] == 1):
false_negatives += 1
accuracy = int((corrects * 100) / len(y_prediction_set))
recall = float(true_positives / (true_positives + false_negatives))
precision = float(true_positives / (true_positives + false_positives))
mse = np.mean(np.square(np.subtract(y_real, y_prediction_set)))
f1_score = 2 * ((recall * precision) / (recall + precision))
return accuracy, mse, recall, precision, f1_score