-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplot.py
96 lines (87 loc) · 3.38 KB
/
plot.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
"""
Copyright (C) 2023 SmartSecLab, Kristiania University College- All Rights Reserved
You may use, distribute and modify this code under the
terms of the MIT license.
You should have received a copy of the MIT license with
this file. If not, please write to: https://opensource.org/licenses/MIT
@Programmer: Guru Bhandari
"""
import matplotlib.pyplot as plt
import numpy as np
class Plotter:
def __init__(self, config):
self.config = config
def plot_metrics(self, history):
print(history.history.keys())
plt.plot(history.history["acc"])
plt.title("The CNN model accuracy")
plt.ylabel("Accuracy")
plt.xlabel("Epoch")
plt.legend(["Accuracy"], loc="lower right")
# plt.show()
def plot_history(self, history, fig_name=None):
"""
plot ML performance metrics obtained from the training/testing history
reference: https://github.com/SmartSecLab/ENViSEC/blob/main/src/utility.py
"""
plt.figure(figsize=(12, 10))
plt.rcParams["font.size"] = "16"
plt.xlabel("epoch")
plt.ylabel("score")
plt.plot(history.epoch, np.array(
history.history["acc"]), label="train_acc")
plt.plot(history.epoch, np.array(
history.history["val_acc"]), label="val_acc")
if "precision" in history.history:
plt.plot(
history.epoch, np.array(history.history["precision"]), label="precision"
)
plt.plot(
history.epoch,
np.array(history.history["val_precision"]),
label="val_precision",
)
if "recall" in history.history:
plt.plot(history.epoch, np.array(
history.history["recall"]), label="recall")
plt.plot(
history.epoch, np.array(history.history["val_recall"]), label="val_recall"
)
plt.legend()
# plt.ylim([0, 1])
if fig_name:
plt.savefig(fig_name + ".pdf")
# plotting loss curve separately
plt.figure(figsize=(12, 10))
plt.xlabel("epoch")
plt.ylabel("score")
plt.plot(history.epoch, np.array(
history.history["loss"]), label="train_loss")
plt.plot(history.epoch, np.array(
history.history["val_loss"]), label="val_loss")
plt.legend()
# plt.ylim([0, 1])
if fig_name:
plt.savefig(fig_name + "_loss.pdf")
def plot_curves(self, fig_name, kfold, trained_model, X, y, cv=3, return_times=True):
"""
plot different curves of ML measures.
reference: https://github.com/SmartSecLab/ENViSEC/blob/main/src/utility.py
"""
# fig, ax1 = plt.subplots(1, 2, figsize=(10, 15))
title = "Learning Curves"
train_sizes, train_scores, test_scores, fit_times, _ = learning_curve(
estimator=trained_model,
X=X,
y=y,
cv=kfold, # cross validation default folds = 5 from sklearn 0.22
return_times=return_times,
)
plt.rcParams["font.size"] = "16"
plt.plot(train_sizes, np.mean(train_scores, axis=1))
plt.plot(train_sizes, np.mean(test_scores, axis=1))
plt.legend(["Training", "Testing"], loc="lower right")
plt.title(title)
plt.xlabel("Training examples")
plt.ylabel("Score")
plt.savefig(fig_name)