-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_utils.py
More file actions
114 lines (94 loc) · 3.62 KB
/
Copy pathplot_utils.py
File metadata and controls
114 lines (94 loc) · 3.62 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
104
105
106
107
108
109
110
111
112
113
114
from sklearn.metrics import precision_recall_curve, roc_auc_score, roc_curve
import matplotlib.pyplot as plt
import seaborn as sn
import pandas as pd
import numpy as np
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
def plot_confusion_matrix(confusion_matrix, title='', cmap ='RdPu'):
df = pd.DataFrame(confusion_matrix, range(len(confusion_matrix)), range(len(confusion_matrix)))
plt.figure(figsize=(6,4))
if title == '' :
plt.title('Confusion Matrix')
else:
plt.title('Confusion Matrix' + ' ' + title)
sn.set(font_scale=1) # for label size
sn.heatmap(df, annot=True, annot_kws={"size": 12},fmt='.0f',cmap=cmap) # font size
plt.ylabel('Actual label')
plt.xlabel('Predicted label')
plt.show()
def plot_precision_recall_curve(actual_labels, prediction, title='', file_name=None):
precision, recall, thresholds = precision_recall_curve(actual_labels, prediction)
plt.figure(figsize=(8,6))
fig, ax = plt.subplots()
ax.plot(recall, precision, color='purple')
# add axis labels to plot
if title == '':
plt.title(title)
else:
plt.title('Precision-Recall Curve')
ax.set_ylabel('Precision')
ax.set_xlabel('Recall')
# display plot
plt.show()
if file_name is not None:
plt.savefig(file_name)
def plot_roc_curve(actual_labels, prediction, title='', file_name=None):
fpr, tpr, _ = roc_curve(actual_labels, prediction)
plt.figure(figsize=(8,6))
plt.plot(fpr, tpr)
if title == '':
plt.title(title)
else:
plt.title('ROC Learning Curves')
plt.xlabel('false positive rate')
plt.ylabel('true positive rate')
plt.show()
if file_name is not None:
plt.savefig(file_name)
def plot_metrics(history):
metrics = ['loss', 'PRC', 'Precision', 'Recall']
plt.figure(figsize=(10,10),linewidth = 7, edgecolor="whitesmoke")
for n, metric in enumerate(metrics):
name = metric.replace("_"," ").capitalize()
plt.subplot(2,2,n+1)
plt.plot(history.epoch, history.history[metric], color=colors[0], label='Train')
plt.plot(history.epoch, history.history['val_'+metric],
color=colors[0], linestyle="--", label='Val')
plt.xlabel('Epoch')
plt.ylabel(name)
if metric == 'loss':
plt.ylim([0, plt.ylim()[1]])
elif metric == 'auc':
plt.ylim([0.8,1])
else:
plt.ylim([0,1])
plt.legend()
def plot_auc_curve(actual_labels, prediction, model_name, title='', file_name = None):
fpr, tpr, _ = roc_curve(actual_labels, prediction)
auc = roc_auc_score(actual_labels, prediction).round(4)
plt.figure(figsize=(8,6))
if title == '':
plt.title(title)
else:
plt.title('AUC Learning Curves')
plt.plot(fpr,tpr, label='Model: '+ model_name + ", AUC=" + str(auc), color='red')
plt.legend(loc=4)
plt.show()
if file_name is not None:
plt.savefig(file_name)
def plot_history(history):
plt.figure(figsize=(10,5),linewidth = 7, edgecolor="whitesmoke")
n = len(history.history['Accuracy'])
plt.plot(np.arange(0,n)+1,history.history['Accuracy'], color='orange',marker=".")
plt.plot(np.arange(0,n)+1,history.history['loss'],'b',marker=".")
# offset both validation curves
plt.plot(np.arange(0,n)+ 1,history.history['val_Accuracy'],'r')
plt.plot(np.arange(0,n)+ 1,history.history['val_loss'],'g')
plt.legend(['Train Acc','Train Loss','Val Acc','Val Loss'])
plt.grid(True)
# set vertical limit to 1
plt.gca().set_ylim(0,1)
plt.xlabel("Number of Epochs")
plt.ylabel("Value")
plt.suptitle("Learning Curve", size=16, y=0.927)
plt.show()