-
Notifications
You must be signed in to change notification settings - Fork 0
/
NaiveBayes.py
320 lines (259 loc) · 12.2 KB
/
NaiveBayes.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
__author__ = 'manshu'
import sys
import math
import csv
numerical_cols = ['VehOdo', 'VehicleAge', 'VehBCost', 'WarrantyCost',
'MMRAcquisitionAuctionAveragePrice', 'MMRAcquisitionAuctionCleanPrice', 'MMRAcquisitionRetailAveragePrice',
'MMRAcquisitonRetailCleanPrice', 'MMRCurrentAuctionAveragePrice', 'MMRCurrentAuctionCleanPrice', 'MMRCurrentRetailAveragePrice', 'MMRCurrentRetailCleanPrice',
'ProfitAcquisitionAverage', 'ProfitAcquisitionClean', 'ProfitCurrentAverage', 'ProfitCurrentClean', 'AverageProfit', 'CleanProfit', 'OdoPAge']
numerical_cols_id = []
def readFile(file):
class_labels = []
classifier_data = []
with open(file, 'rb') as csvfile:
csv_reader = csv.reader(csvfile, delimiter=',', quotechar='|')
line = 0
for row in csv_reader:
line += 1
if line == 1:
features = row[1:]
continue
class_labels.append(int(row[0]))
classifier_data.append({i : 0 for i in range(1, len(features) + 1)})
data_tuple = row[1:]
for i in range(0, len(data_tuple)):
attr_val = data_tuple[i]
classifier_data[-1][i] = attr_val #int(math.ceil(float(attr_val)))
if numerical_cols_id == []:
for i in range(0, len(features)):
feature = features[i]
for col_name in numerical_cols:
if feature.startswith(col_name) or col_name == feature:
numerical_cols_id.append(i)
for row in classifier_data:
for attr in row:
if attr in numerical_cols_id:
row[attr] = int(math.ceil(float(row[attr])))
else:
try:
row[attr] = int(row[attr])
except:
row[attr] = int(math.ceil(float(row[attr])))
print features
print numerical_cols_id
#print class_labels
print classifier_data[1:5]
return features, class_labels, classifier_data
def findMaxNumAttributes(training_data, test_data):
max_attribute = 0
max_attribute_values = {}
for row in (training_data + test_data):
row_max_attribute = 0 # max([int(col.split(':')[0]) for col in data_instance[1:]])
for col in row:
# attribute_val_pair = col.split(':')
# attribute = int(attribute_val_pair[0])
# attribute_value = int(attribute_val_pair[1])
attribute = col
attribute_value = row[col]
if attribute > row_max_attribute:
row_max_attribute = attribute
if attribute not in max_attribute_values:
max_attribute_values[attribute] = 0
if attribute_value > max_attribute_values[attribute]:
max_attribute_values[attribute] = attribute_value
if row_max_attribute > max_attribute:
max_attribute = row_max_attribute
for value in max_attribute_values:
max_attribute_values[value] += 1
return max_attribute, max_attribute_values
def formatData(input_data, max_attribute):
classifier_data = []
classifier_class = []
for row in input_data:
data_instance = row.split(' ')
classifier_class.append(int(data_instance[0]))
classifier_data.append({i : 0 for i in range(1, max_attribute + 1)})
for col in data_instance[1:]:
key_val_pair = col.split(':')
classifier_data[-1][int(key_val_pair[0])] = int(key_val_pair[1])
return classifier_class, classifier_data
def guassianProbability(guassian_attr_val_class, attr, xk, Ci):
gxus = 1 / math.sqrt(2 * math.pi)
if attr not in guassian_attr_val_class:
return 0
if Ci not in guassian_attr_val_class[attr]:
return 0
mean = guassian_attr_val_class[attr][Ci][0]
std = guassian_attr_val_class[attr][Ci][1]
if std == 0.0:
std = 1.0
gxus *= 1.0 / std
epow = math.e ** (-1 * ((xk - mean) * (xk - mean)) / (2 * std * std))
gxus *= epow
return gxus
def makeGuassianCalculations(train_data, train_class):
guassian_attr_val_class = {}
for i in range(0, len(train_data)):
data_instance = train_data[i]
data_class = train_class[i]
for attr in data_instance:
if attr in numerical_cols_id:
if attr not in guassian_attr_val_class:
guassian_attr_val_class[attr] = {}
if data_class not in guassian_attr_val_class[attr]:
guassian_attr_val_class[attr][data_class] = [0.0, 0.0, 0]
guassian_attr_val_class[attr][data_class][0] += data_instance[attr]
guassian_attr_val_class[attr][data_class][2] += 1
for attr in guassian_attr_val_class:
for data_class in guassian_attr_val_class[attr]:
sum = guassian_attr_val_class[attr][data_class][0]
count = guassian_attr_val_class[attr][data_class][2]
mean = sum / (1.0 * count)
guassian_attr_val_class[attr][data_class][0] = mean
for i in range(0, len(train_data)):
data_instance = train_data[i]
data_class = train_class[i]
for attr in data_instance:
if attr in guassian_attr_val_class:
mean = guassian_attr_val_class[attr][data_class][0]
guassian_attr_val_class[attr][data_class][1] += (data_instance[attr] - mean) * (data_instance[attr] - mean)
for attr in guassian_attr_val_class:
for data_class in guassian_attr_val_class[attr]:
mean_sum = guassian_attr_val_class[attr][data_class][1]
count = guassian_attr_val_class[attr][data_class][2]
std = math.sqrt(mean_sum / (1.0 * count))
guassian_attr_val_class[attr][data_class][1] = std
return guassian_attr_val_class
def makeClassifier(training_class, training_data, max_attribute_values):
probability_class = {}
probability_attribute_val_class = {}
num_samples = len(training_class)
guassian_attr_val_class = makeGuassianCalculations(training_data, training_class)
print "Guassian"
print guassian_attr_val_class
for class_label in training_class:
if class_label not in probability_class:
probability_class[class_label] = 0
probability_class[class_label] += 1
for attr in training_data[0].keys():
if attr in numerical_cols_id:
continue
probability_attribute_val_class[attr] = {}
for attr_value in range(0, max_attribute_values[attr]):
probability_attribute_val_class[attr][attr_value] = {}
for class_label in probability_class:
probability_attribute_val_class[attr][attr_value][class_label] = 1
for data_id in range(0, num_samples):
data_instance = training_data[data_id]
for attribute in data_instance:
if attribute in numerical_cols_id:
continue
attribute_value = data_instance[attribute]
# if attribute_value not in probability_attribute_val_class[attribute]:
# probability_attribute_val_class[attribute][attribute_value] = {}
# for class_label in probability_class: probability_attribute_val_class[attribute][attribute_value][class_label] = 0
probability_attribute_val_class[attribute][attribute_value][training_class[data_id]] += 1
for attribute in probability_attribute_val_class:
if attribute in numerical_cols_id:
continue
for attribute_value in probability_attribute_val_class[attribute]:
for class_label in probability_attribute_val_class[attribute][attribute_value]:
probability_attribute_val_class[attribute][attribute_value][class_label] /= (1.0 * (probability_class[class_label] + max_attribute_values[attribute]))
return [probability_class, probability_attribute_val_class, guassian_attr_val_class]
def predictClass(test_data, nBclassifier):
probability_class = nBclassifier[0]
probability_attribute_val_class = nBclassifier[1]
guassian_attr_val_class = nBclassifier[2]
predicted_class = []
for data_instance in test_data:
max_probability = 0.0
max_class = probability_class.keys()[0]
for class_label in probability_class:
ci_probability = probability_class[class_label]
for attribute in data_instance:
attribute_value = data_instance[attribute]
# if attribute_value not in probability_attribute_val_class[attribute]:
# ci_probability *= 0
# else:
if attribute in numerical_cols_id:
ci_probability *= guassianProbability(guassian_attr_val_class, attribute, attribute_value, class_label)
else:
ci_probability *= probability_attribute_val_class[attribute][attribute_value][class_label]
if ci_probability > max_probability:
max_probability = ci_probability
max_class = class_label
predicted_class.append(max_class)
return predicted_class
def generateMeasures(test_labels, predicted_labels):
tp = tn = fp = fn = 0
if not test_labels or not predicted_labels:
print str(tp) + " " + str(fn) + " " + str(fp) + " " + str(tn)
return
for i in range(0, len(test_labels)):
tvalue = test_labels[i]
pvalue = predicted_labels[i]
if tvalue == 1:
if pvalue == 1:
tp += 1
else:
fn += 1
else:
if pvalue == 1:
fp += 1
else:
tn += 1
p = len([i for i in range(0, len(test_labels)) if test_labels[i] == 1])
n = len([i for i in range(0, len(test_labels)) if test_labels[i] == 0])
accuracy = (tp + tn) / (1.0 * (p + n))
error_rate = (fp + fn) / (1.0 * (p + n))
senstivity = tp / (1.0 * p)
recall = senstivity
specificity = tn / (1.0 * n)
precision = tp / (1.0 * (tp + fp))
f1_score = 2.0 * (precision * recall) / (1.0 * (precision + recall))
B = 0.5
fbscore1 = ((1 + B*B) * precision * recall) / (1.0 * (B*B*precision + recall))
B = 2
fbscore2 = ((1 + B*B) * precision * recall) / (1.0 * (B*B*precision + recall))
print "Accuracy = " + str(accuracy) + ", Error Rate = " + str(error_rate) + ", Senstivity = " + str(senstivity) + ", Specificity = " + str(specificity) + ", Precision = " + str(precision) + ", f1-score = " + str(f1_score) + ", Fb(0.5) = " + str(fbscore1) + ", Fb(2) = " + str(fbscore2)
print str(tp) + " " + str(fn) + " " + str(fp) + " " + str(tn)
def solveAssignment(training_file, test_file):
# read data from both files as it is
features, training_class, training_data = readFile(training_file)
f2, ref_ids, test_data = readFile(test_file)
print "RefIds", ref_ids
#
# # find max number of attributes in both the files
max_attribute, max_attribute_values = findMaxNumAttributes(training_data, test_data)
print max_attribute
print max_attribute_values
#
# # format training and test data which can be used by classifier
# training_class, training_data = formatData(training_data, max_attribute)
# test_class, test_data = formatData(test_data, max_attribute)
#
# # make classifier from training_data and class labels
nBclassifier = makeClassifier(training_class, training_data, max_attribute_values)
#
# #print nBclassifier[0]
# #print nBclassifier[1]
#
# predict using the classifier built on training and test data
predicted_class = predictClass(training_data, nBclassifier)
generateMeasures(training_class, predicted_class)
predicted_class = predictClass(test_data, nBclassifier)
print predicted_class
output = []
for i in range(0, len(predicted_class)):
output.append([ref_ids[i], predicted_class[i]])
with open("output.csv", "wb") as f:
writer = csv.writer(f)
writer.writerows(output)
# generateMeasures(test_class, predicted_class)
if __name__ == "__main__":
# if len(sys.argv) < 2:
# print "Please run the file with a training file and a test file"
# sys.exit(1)
training_file = "x.csv" #sys.argv[1]
test_file = "y.csv" #sys.argv[2]
solveAssignment(training_file, test_file)