-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat_utils.py
34 lines (30 loc) · 1.19 KB
/
stat_utils.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
import matplotlib.pyplot as plt
import numpy as np
def get_class_accuracy(y_true, y_pred, num_classes):
class_acc = np.zeros(num_classes)
class_correct = 0
for i in range(num_classes):
for j in range(len(y_true)):
if y_true[j] == i and y_pred[j] == i:
class_correct += 1
class_acc[i] = (class_correct / y_true.count(i))*100
class_correct = 0
return class_acc
def plot_confusion_matrix(cm, num_classes, classes):
print(cm)
fig, ax = plt.subplots(figsize=(num_classes, num_classes))
ax.imshow(cm, cmap=plt.cm.Blues)
ax.set_title('Confusion matrix')
tick_marks = np.arange(num_classes)
ax.set_xticks(tick_marks)
ax.set_yticks(tick_marks)
ax.set_xticklabels(classes, rotation=45)
ax.set_yticklabels(classes)
ax.set_ylabel('True label')
ax.set_xlabel('Predicted label')
for i in range(num_classes):
for j in range(num_classes):
percentage = cm[i][j]/sum(cm[i])*100
color = "white" if percentage > 50 else "black"
text = "%.1f%%" % ((cm[i][j]/sum(cm[i]))*100) if percentage > 0 else "-"
ax.text(j, i, text, ha='center', va='center', color=color)