-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulateResults.py
More file actions
103 lines (89 loc) · 3.74 KB
/
SimulateResults.py
File metadata and controls
103 lines (89 loc) · 3.74 KB
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
# %%
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn import neighbors
import itertools
import math
import operator
class SimulateResults:
def getAccuracy(self,testResult, predictions):
correct = 0
for x in range(len(testResult)):
if testResult[x] == predictions[x]:
correct += 1
return (correct / float(len(testResult))) * 100
def getConfusionMatrix(self,testResult, predictions):
label = [0, 1]
cmatrix = confusion_matrix(testResult, predictions, label)
return cmatrix
def plot_confusion_matrix(self,confusionmatrix,
target_names,
title='Confusion matrix',
cmap=None,
normalize=False):
accuracy = np.trace(confusionmatrix) / float(np.sum(confusionmatrix))
misclass = 1 - accuracy
if cmap is None:
cmap = plt.get_cmap('Blues')
plt.figure(figsize=(8, 6))
plt.imshow(confusionmatrix, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
if target_names is not None:
tick_marks = np.arange(len(target_names))
plt.xticks(tick_marks, target_names, rotation=45)
plt.yticks(tick_marks, target_names)
if normalize:
confusionmatrix = confusionmatrix.astype('float') / confusionmatrix.sum(axis=1)[:, np.newaxis]
thresh = confusionmatrix.max() / 1.5 if normalize else confusionmatrix.max() / 2
for i, j in itertools.product(range(confusionmatrix.shape[0]), range(confusionmatrix.shape[1])):
if normalize:
plt.text(j, i, "{:0.4f}".format(confusionmatrix[i, j]),
horizontalalignment="center",
color="white" if confusionmatrix[i, j] > thresh else "black")
else:
plt.text(j, i, "{:,}".format(confusionmatrix[i, j]),
horizontalalignment="center",
color="white" if confusionmatrix[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label\naccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, misclass))
plt.show()
def calculate(self,cmatrix):
i = 0
true_positive = cmatrix[i][0]
false_negative = cmatrix[i][1]
false_positive = cmatrix[i + 1][0]
true_negative = cmatrix[i + 1][1]
sdr = true_positive / (true_positive + false_negative)
ldr = true_negative / (true_negative + false_positive)
tdr = (true_positive + true_negative) / (true_positive + false_negative + true_positive + false_positive)
return (round(sdr,4),round(ldr,4), round(tdr,4))
def generateGraph(self,data):
N = 4
y = []
z = []
k = []
width = 0.27
ind = np.arange(N)
for dict in data:
classifier = dict['classifier']
sdr = dict['sdr']
ldr = dict['ldr']
tdr = dict['tdr']
y.append(sdr)
k.append(tdr)
z.append(ldr)
ax = plt.subplot(111)
rects1 = ax.bar(ind, y, width, color='b', align='center')
rects2 = ax.bar(ind + width, z, width, color='g', align='center')
rects3 = ax.bar(ind + width * 2, k, width, color='r', align='center')
ax.legend((rects1[0], rects2[0], rects3[0]), ('SDR', 'LDR', 'TDR'))
ax.set_ylabel('Detection Rate')
ax.set_xticks(ind + width)
ax.set_xticklabels(('KNN', 'Naive Bayes', 'Decision Trees', 'SVM'))
plt.show()
# %%