-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphs.py
177 lines (158 loc) · 4.72 KB
/
Graphs.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
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
from sklearn.utils.multiclass import unique_labels
from sklearn.metrics import confusion_matrix
class Graphs:
"""
Class responsible for plot analysis graphs.
"""
def __init__(self, name="graphs"):
"""
Initialize class.
"""
self.name = name
def show_train_validation(self, epochs, model_out):
"""
Args:
---------
epochs: number of train epochs
model_out: object of trained model
Return:
---------
graph with train/validation loss and accuracy
"""
N = np.arange(0, epochs)
model_info = pd.DataFrame()
model_info['N'] = N
model_info['loss'] = model_out.history["loss"]
model_info['val_loss'] = model_out.history["val_loss"]
model_info['acc'] = model_out.history['acc']
model_info['val_acc'] = model_out.history['val_acc']
# loss graphs
fig1 = plt.figure(figsize=(12, 12))
ax = fig1.add_subplot(1, 1, 1)
sns.lineplot(
x=model_info['N'],
y=model_info['loss'],
ax=ax,
data=model_info,
legend='full'
)
sns.lineplot(
x=model_info['N'],
y=model_info['val_loss'],
ax=ax,
data=model_info,
legend='full'
)
plt.title(
"Loss variation",
fontsize=20
)
plt.xlabel(
"Epochs",
fontsize=15
)
plt.ylabel(
"Loss",
fontsize=15
)
fig1.legend(
labels=['loss', 'val_loss']
)
plt.savefig('model1_loss')
# plt.show()
# accuracy graphs
fig1 = plt.figure(figsize=(12, 12))
ax = fig1.add_subplot(1, 1, 1)
sns.lineplot(
x=model_info['N'],
y=model_info['acc'],
data=model_info
)
sns.lineplot(
x=model_info['N'],
y=model_info['val_acc'],
data=model_info
)
plt.title(
"Accuracy variation",
fontsize=20
)
plt.xlabel(
"Epochs",
fontsize=15
)
plt.ylabel(
"Accuracy",
fontsize=15
)
fig1.legend(
labels=['accuracy', 'val_accuracy']
)
# plt.savefig('model1_accuracy')
plt.show()
def show_confusion_matrix(self, y_true, y_pred, classes):
"""
"""
self.__plot_confusion_matrix(
y_true,
y_pred,
classes,
normalize=True
)
return 0
def __plot_confusion_matrix(
self,
y_true,
y_pred,
classes,
normalize=False,
title=None,
cmap=plt.cm.Blues
):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if not title:
if normalize:
title = 'Normalized confusion matrix'
else:
title = 'Confusion matrix, without normalization'
cm = confusion_matrix(y_true, y_pred)
# Only use the labels that appear in the data
classes = classes[unique_labels(y_true, y_pred)]
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
fig, ax = plt.subplots()
im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
ax.figure.colorbar(im, ax=ax)
# We want to show all ticks...
ax.set(xticks=np.arange(cm.shape[1]),
yticks=np.arange(cm.shape[0]),
# ... and label them with the respective list entries
xticklabels=classes, yticklabels=classes,
title=title,
ylabel='True label',
xlabel='Predicted label')
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
# Loop over data dimensions and create text annotations.
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i in range(cm.shape[0]):
for j in range(cm.shape[1]):
ax.text(j, i, format(cm[i, j], fmt),
ha="center", va="center",
color="white" if cm[i, j] > thresh else "black")
fig.tight_layout()
plt.show()
return ax