-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation.py
127 lines (94 loc) · 4.02 KB
/
evaluation.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from itertools import product
import numpy as np
import matplotlib.pyplot as plt
def confusion_matrix(y_pred, y_true, num_classes):
"""
Create a confusion matrix for label encodings in PyTorch.
Parameters:
y_pred (torch.Tensor): Predicted labels tensor.
y_true (torch.Tensor): True labels tensor.
num_classes (int): Number of classes.
Returns:
numpy.ndarray: Confusion matrix.
"""
# if len(y_pred) != len(y_true):
# raise ValueError("Shapes of predictions and true labels must match. y_pred shape: {} y_true shape: {}".format(y_pred.shape, y_true.shape))
conf_matrix = np.zeros((num_classes, num_classes), dtype=np.int64)
y_pred_np = y_pred.argmax(dim=1).cpu().numpy()
y_true_np = y_true.cpu().numpy()
for pred, true in zip(y_pred_np, y_true_np):
conf_matrix[pred, true] += 1
return conf_matrix
def calculate_confusion_matrix(test_emb, test_labels, model):
model.eval()
output = model(test_emb)
return confusion_matrix(output, test_labels, 6)
def class_accuracy(conf_matrix):
"""
Calculate accuracy for each class based on a confusion matrix.
Parameters:
conf_matrix (numpy.ndarray): Confusion matrix.
Returns:
list: List of accuracies for each class.
"""
# diagonal = np.diag(conf_matrix)
# row_sums = conf_matrix.sum(axis=1)
# accuracies = diagonal / row_sums.astype(float)
# return accuracies
diagonal = np.diag(conf_matrix)
row_sums = conf_matrix.sum(axis=1)
with np.errstate(divide='ignore', invalid='ignore'):
accuracies = np.where(row_sums != 0, diagonal / row_sums.astype(float), 0.0)
return accuracies
def class_f1_score(conf_matrix, epsilon=1e-7):
"""
Calculate F1 score for each class based on a confusion matrix.
Parameters:
conf_matrix (numpy.ndarray): Confusion matrix.
epsilon (float): Smoothing term to avoid division by zero.
Returns:
list: List of F1 scores for each class.
"""
tp = np.diag(conf_matrix)
fp = conf_matrix.sum(axis=0) - tp
fn = conf_matrix.sum(axis=1) - tp
precision = tp / (tp + fp + epsilon)
recall = tp / (tp + fn + epsilon)
f1_scores = 2 * (precision * recall) / (precision + recall + epsilon)
return f1_scores
def class_wise_precision_recall(conf_matrix: np.ndarray):
num_classes = conf_matrix.shape[0]
precision = np.zeros(num_classes)
recall = np.zeros(num_classes)
for i in range(num_classes):
true_positives = conf_matrix[i, i]
false_positives = np.sum(conf_matrix[:, i]) - true_positives
false_negatives = np.sum(conf_matrix[i, :]) - true_positives
precision[i] = true_positives / (true_positives + false_positives)
recall[i] = true_positives / (true_positives + false_negatives)
return precision, recall
def plot_confusion_matrix(conf_matrix, modelname, plot= False):
labels = {0: "SADNESS", 1: "JOY", 2: "LOVE", 3: "ANGER", 4: "FEAR", 5: "SURPRISE"}
plt.style.use("seaborn")
plt.figure(figsize=(7,7))
norm_conf_mat = np.divide(conf_matrix, np.sum(conf_matrix, axis= 0))
plt.imshow(norm_conf_mat, cmap='coolwarm', interpolation='nearest')
plt.colorbar()
plt.xticks(list(labels.keys()), [labels[i] for i in range(len(labels))])
plt.yticks(list(labels.keys()), [labels[i] for i in range(len(labels))])
fmt = '.2f'
for i, j in product(range(norm_conf_mat.shape[0]), range(norm_conf_mat.shape[1])):
plt.text(j, i, format(norm_conf_mat[i, j], fmt),
horizontalalignment="center",
color="black")
plt.xlabel('Predicted label',fontdict={"fontweight":"bold"})
plt.ylabel('True label',fontdict={"fontweight":"bold"})
plt.title('Confusion Matrix',fontdict={"fontsize":15,"fontweight":"bold"})
plt.grid(False)
if plot:
plt.show()
else:
if modelname == 'bilstm':
plt.savefig(fname= "./images/bilstm_confmat.png", dpi=300)
elif modelname == 'cnn':
plt.savefig(fname= "./images/cnn_confmat.png", dpi=300)