matlab用confusionmat计算SE, SP, F1 score

matlab里有个confusionmat函数
接口是这样的

M = confusionmat(true_label,predict_label)

M计算出来是下面这样
比如第一个class airplane吧,923意思是true class是airplane, 而我们预测的label也是airplane的个数
而第一行第二列的4就表示true class是airplane, 而我们预测的label是automobile的个数是4
在这里插入图片描述
那么就相当于对角线上蓝色背景的都是预测对的

如果是2分类的情况,true class是0,1,假设1是positive
那么TP的个数就是M(2,2)
SE 即TPR= TP / (TP+FN) , 同时也是recall (TPR: true positive rate)
SP 即TNR= TN / (TN+FP) (TNR: true negative rate)

F1 score = 2 * (precision * recall) / (precision + recall)
precision = TP / (TP + FP), 即PPV(positive predictive value)

function [score, TPR, TNR] = f1_score(label, predict)
   M = confusionmat(label, predict);
   #以下两行为二分类时用
   TPR = M(2,2) / (M(2,1) + M(2,2)); #SE: TP/(TP+FN)
   TNR = M(1,1) / (M(1,1) + M(1,2)); #SP: TN/(TN+FP)
   #转置,可以不转同时调换方向
   M = M';
   precision = diag(M)./(sum(M,2) + 0.0001);  #按列求和: TP/(TP+FP)
   recall = diag(M)./(sum(M,1)+0.0001)'; #按行求和: TP/(TP+FN)
   precision = mean(precision);
   recall = mean(recall);
   score = 2*precision*recall/(precision + recall);
end
  • 3
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值