-
Notifications
You must be signed in to change notification settings - Fork 2
/
analysis.py
142 lines (117 loc) · 5.33 KB
/
analysis.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
from main import *
import numpy as np
import itertools
import sklearn.metrics
import matplotlib.pyplot as plt
from sklearn.metrics import precision_recall_fscore_support as score
def predict(dataset, days, method):
# truth_a, predict_a, accuracy_a
return calculate(dataset, days, method)
def bmatrix(a):
"""Returns a LaTeX bmatrix
:a: numpy array
:returns: LaTeX bmatrix as a string
"""
if len(a.shape) > 2:
raise ValueError('bmatrix can at most display two dimensions')
lines = str(a).replace('[', '').replace(']', '').splitlines()
rv = [r'\begin{bmatrix}']
rv += [' ' + ' & '.join(l.split()) + r'\\' for l in lines]
rv += [r'\end{bmatrix}']
return '\n'.join(rv)
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True')
plt.xlabel('Predicted label')
if __name__ == '__main__':
np.set_printoptions(precision=2)
# prova mia
print("A - test 2 - no Time Slice")
for method in [1,2]:
for j in [1,2]:
if method == 1:
if(j==1):
precisionmatrix = np.zeros(9)
recallmatrix = np.zeros(9)
fscorematrix = np.zeros(9)
else:
precisionmatrix = np.zeros(10)
recallmatrix = np.zeros(10)
fscorematrix = np.zeros(10)
else:
if(j==1):
precisionmatrix = np.zeros(10)
recallmatrix = np.zeros(10)
fscorematrix = np.zeros(10)
else:
precisionmatrix = np.zeros(11)
recallmatrix = np.zeros(11)
fscorematrix = np.zeros(11)
for i in [6,7,8,9,10]:
truth_a, predict_a, accuracy_a = predict(j,i,method) # dataset, days, method
# cambia il tipo dell'array
if method == 1:
if (j == 1):
truth_a = np.append([0, 1, 2, 3, 4, 5, 6, 7, 8], truth_a)
predict_a = predict_a.astype('uint8')
predict_a = np.append([0, 1, 2, 3, 4, 5, 6, 7, 8], predict_a)
else:
truth_a = np.append([0, 1, 2, 3, 4, 5, 6, 7, 8,9], truth_a)
predict_a = predict_a.astype('uint8')
predict_a = np.append([0, 1, 2, 3, 4, 5, 6, 7, 8,9], predict_a)
else:
if (j == 1):
truth_a = np.append([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], truth_a)
predict_a = predict_a.astype('uint8')
predict_a = np.append([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], predict_a)
else:
truth_a = np.append([0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10], truth_a)
predict_a = predict_a.astype('uint8')
predict_a = np.append([0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10], predict_a)
precision, recall, fscore, support = score(predict_a, truth_a)
precisionmatrix = numpy.vstack([precisionmatrix, precision])
recallmatrix = numpy.vstack([recallmatrix, recall])
fscorematrix = numpy.vstack([fscorematrix, fscore])
conf_mat_a = sklearn.metrics.confusion_matrix(truth_a, predict_a)
# print(sklearn.metrics.classification_report(truth_a, predict_a))
# plt.figure(num=None, figsize=(8, 6), dpi=80)
# plot_confusion_matrix(
# conf_mat_a,
# list(map(str, range(max(truth_a)))),
# normalize=True
# )
# plt.savefig('confusionMatrix'+str(method)+str(j)+str(i)+'.png')
# plt.show()
precisionmatrix = numpy.delete(precisionmatrix, (0), axis=0)
fscorematrix = numpy.delete(fscorematrix, (0), axis=0)
recallmatrix = numpy.delete(recallmatrix, (0), axis=0)
meanprec = precisionmatrix.mean(0)
meanfscore = fscorematrix.mean(0)
meanrecall = recallmatrix.mean(0)
print("meanprec dataset "+str(j)+" method "+str(method))
print(bmatrix(meanprec) + '\n')
print("meanfscore dataset " + str(j) + " method " + str(method))
print(bmatrix(meanfscore) + '\n')
print("meanrecall dataset " + str(j) + " method " + str(method))
print(bmatrix(meanrecall) + '\n')