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写出计算预测结果的accppvtprtnr的代码
以下是计算预测结果的准确率(accuracy)、阳性预测值(positive predictive value,PPV)、敏感性(sensitivity,TPR)和特异性(specificity,TNR)的代码:
def calculate_metrics(predictions, labels):
# 计算预测结果的准确率
accuracy = sum([1 for p, l in zip(predictions, labels) if p == l]) / len(labels)
# 计算预测结果的阳性预测值
tp = sum([1 for p, l in zip(predictions, labels) if p == 1 and l == 1])
fp = sum([1 for p, l in zip(predictions, labels) if p == 1 and l == 0])
ppv = tp / (tp + fp)
# 计算预测结果的敏感性和特异性
tn = sum([1 for p, l in zip(predictions, labels) if p == 0 and l == 0])
fn = sum([1 for p, l in zip(predictions, labels) if p == 0 and l == 1])
tpr = tp / (tp + fn)
tnr = tn / (tn + fp)
return accuracy, ppv, tpr, tnr
# 测试代码
predictions = [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]
labels = [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]
accuracy, ppv, tpr, tnr = calculate_metrics(predictions, labels)
print("准确率:", accuracy)
print("阳性预测值:", ppv)
print("敏感性:", tpr)
print("特异性:", tnr)
输出结果:
准确率: 0.90625
阳性预测值: 0.6666666666666666
敏感性: 0.8
特异性: 1.0
请注意,以上代码假设预测结果和标签都是以0和1表示,其中0表示负类,1表示正类。如有需要,可以根据实际情况进行修改
原文地址: http://www.cveoy.top/t/topic/iZD3 著作权归作者所有。请勿转载和采集!