Hello Mat

 找回密码
 立即注册
查看: 6180|回复: 0

混淆矩阵confusion_mat计算预测正确率和召回率

[复制链接]

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2017-6-10 23:02:49 | 显示全部楼层 |阅读模式
混淆矩阵confusion_mat计算预测正确率和回召率
正样本 负样本
预测为正样本 真阳性 假阳性
预测为负样本 假阴性真阴性

样本预测准确性Accuracy:(真阳性+真阴性)/(真阳性+假阳性+假阴性+真阴性 )
正样本预测精度Precision:真阳性/( 真阳性+假阳性 )
负样本预测精度Precision:真阴性 /( 真阴性+假阴性 )
正样本召回率Recall:真阳性/( 真阳性+假阴性 )
负样本召回率Recall:真阴性 /( 真阴性+假阳性 )
F1指标:2/(1/Precision + 1/Recall)

使用环境:Win7-32bit-matlab2014a
  1. function [fm acc cm ] = confusion_mat( y_actual,y_predicted )
  2. tp=0;tn=0;fp=0;fn=0;
  3. for i=1:length(y_actual)
  4. if y_actual(i)>0
  5.     if y_actual(i)==y_predicted(i)
  6.         tp=tp+1;
  7.     else
  8.         fn=fn+1;
  9.     end
  10. end
  11. if y_actual(i)<0
  12.     if y_actual(i)==y_predicted(i)
  13.         tn=tn+1;
  14.     else
  15.         fp=fp+1;
  16.     end
  17. end
  18. end
  19. cm=[tn fp; fn tp];
  20. acc=(tp+tn)/(tp+fn+fp+tn);
  21. sens=tp/(tp+fn);
  22. prec=tp/(tp+fp);
  23. fm=(2*prec*sens)/(prec+sens);
  24. end
复制代码
等价于python:
  1. # 从sklearn.metrics里导入classification_report模块。
  2. from sklearn.metrics import classification_report
  3. # 使用逻辑斯蒂回归模型自带的评分函数score获得模型在测试集上的准确性结果。
  4. print 'Accuracy of LR Classifier:', lr.score(X_test, y_test)
  5. # 利用classification_report模块获得LogisticRegression其他三个指标的结果。
  6. print classification_report(y_test, lr_y_predict, target_names=['0', '1'])
复制代码






算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-27 05:58 , Processed in 0.250188 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表