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 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

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 7label: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 7cnn是八分类的模型预测结果的其中一条label是原始标签利用python写出计算每一个类别的预测结果的accppvtpr

原文地址: http://www.cveoy.top/t/topic/iZD8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录