-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
47 lines (30 loc) · 947 Bytes
/
evaluate.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
from sklearn.metrics import accuracy_score
from sklearn.metrics import f1_score
from sklearn.metrics import confusion_matrix
import pandas as pd
import numpy as np
import json
import seaborn as sns
import matplotlib.pyplot as plt
import common
def show_confusion(y, y_predicted):
mat = confusion_matrix(y_true=y, y_pred=y_predicted)
sns.heatmap(mat.T, annot=True, fmt='d')
plt.show()
def calculate_accuracy(model, X, y):
y_predicted = model.predict(X)
y_probs = model.predict_proba(X)
total = 0
correct = 0
for index,_ in enumerate(y_predicted):
total+=1
winner = np.argsort(y_probs[index])[-1]
if y.values[index] == winner:
correct+=1
manual_accuracy = correct/total
accuracy = accuracy_score(y, y_predicted)
return accuracy, manual_accuracy
def evaluate(result_name, model, X, y):
accuracy, manual_accuracy = calculate_accuracy(model, X, y)
output = [result_name, f"{accuracy:.4f}", "{}"]
return output