-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathperformance_report.py
75 lines (61 loc) · 2.32 KB
/
performance_report.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
import csv
import matplotlib.pyplot as plt
import os
import secrets # to generate jpg name
def performance_report(model, X, y, X_test, y_test, y_pred, model_label):
# classification accuracy
try:
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test, y_pred)
except:
accuracy = "NA"
# confusion matrix
from sklearn.metrics import confusion_matrix
from sklearn.metrics import plot_confusion_matrix
try:
conf = confusion_matrix(y_test, y_pred)
TP = conf[1][1]
TN = conf[0][0]
FP = conf[0][1]
FN = conf[1][0]
disp = plot_confusion_matrix(model, X_test, y_test, cmap=plt.cm.Blues, values_format='.0f')
title = secrets.token_hex(4)
plt.savefig(title, format="jpg")
path1 = os.path.abspath(title+".jpg")
except:
TP = TN = FP = FN = path1 = ""
# k-fold cross validation
from sklearn import model_selection
try:
kfold = model_selection.KFold(n_splits=6, shuffle=True)
KCV = model_selection.cross_val_score(model, X, y, cv=kfold, scoring='accuracy').mean()
except:
KCV = ""
# log loss (closer to 0 is better)
try:
logloss = model_selection.cross_val_score(model, X, y, cv=kfold, scoring='neg_log_loss').mean()
except:
logloss = ""
# AUC (1 is perfect predictions, 0.5 is as good as random)
try:
AUC = model_selection.cross_val_score(model, X, y, cv=kfold, scoring='roc_auc').mean()
except:
AUC = ""
# precision recall curve
from sklearn.metrics import plot_precision_recall_curve
from sklearn.metrics import average_precision_score
try:
y_score = model.decision_function(X_test)
average_precision = average_precision_score(y_test, y_score)
disp = plot_precision_recall_curve(model, X_test, y_test)
title = secrets.token_hex(4)
plt.savefig(title, format="jpg")
path2 = os.path.abspath(title+".jpg")
except:
average_precision = path2 = ""
# appending metrics to csv file
with open('metrics.csv', 'a', newline='') as file:
writer = csv.writer(file)
if os.stat("metrics.csv").st_size == 0:
writer.writerow(["Model label", "Accuracy score", "TP", "TN", "FP", "FN", "Confusion matrix file","KCV", "Log-loss", "AUC", "Average precision", "Precision-recall file"])
writer.writerow([model_label, accuracy, TP, TN, FP, FN, path1, KCV, logloss, AUC, average_precision, path2])