Calculate Accuracy, Precision, Recall, and Specificity for Multi-Class Classification
This code calculates the accuracy, positive predictive value (ppv), true positive rate (tpr), and true negative rate (tnr) for each class based on the CNN predictions and the original labels. The calculate_metrics function takes in the CNN predictions, labels, and the number of classes as input. It then iterates through each prediction and label, incrementing the appropriate counters for tp, tn, fp, and fn. Finally, it calculates the metrics using these counters and returns the results.
import numpy as np
def calculate_metrics(cnn, label, class_num):
# Initialize counters for true positive, true negative, false positive, false negative
tp = np.zeros(class_num)
tn = np.zeros(class_num)
fp = np.zeros(class_num)
fn = np.zeros(class_num)
for i in range(len(cnn)):
if cnn[i] == label[i]:
tp[cnn[i]] += 1
for j in range(class_num):
if j != cnn[i]:
tn[j] += 1
else:
fp[cnn[i]] += 1
fn[label[i]] += 1
acc = (tp + tn) / (tp + tn + fp + fn) # Accuracy
ppv = tp / (tp + fp) # Positive Predictive Value (Precision)
tpr = tp / (tp + fn) # True Positive Rate (Recall, Sensitivity)
tnr = tn / (tn + fp) # True Negative Rate (Specificity)
return acc, ppv, tpr, tnr
# Example usage
cnn = [0, 2, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 2, 0, 0, 7, 7, 7, 7]
label = [0, 2, 2, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 2, 0, 0, 7, 7, 7, 7]
class_num = 8
acc, ppv, tpr, tnr = calculate_metrics(cnn, label, class_num)
print('Accuracy for each class:', acc)
print('Positive Predictive Value for each class:', ppv)
print('True Positive Rate for each class:', tpr)
print('True Negative Rate for each class:', tnr)
This code provides a clear and concise method for evaluating the performance of a multi-class classification model. It calculates essential metrics like accuracy, precision, recall, and specificity, allowing for a thorough understanding of the model's strengths and weaknesses across different classes.
原文地址: https://www.cveoy.top/t/topic/qFDz 著作权归作者所有。请勿转载和采集!