http://iccm.cc/classification-model-evaluation-confusion-matrix/

分类器评价与在R中的实现:混淆矩阵

Posted by c cm on January 18, 2014

分类模型评价一般有以下几种方法:混淆矩阵(Confusion Matrix)、收益图(Gain Chart)、提升图(Lift Chart)、KS图(KS Chart)、接受者操作特性曲线(ROC Chart)。“分类模型评价与在R中的实现”系列中将逐个介绍。

本篇介绍最基础的混淆矩阵。

一、混淆矩阵简介

混淆矩阵将分类预测结果与实际目标进行比较,并汇总成NXN列联表(N为分类类型数)。

以二元分类为例:

Confusion Matrix Target

Confusion Matrix Positive Negative

Model Positive TP(True Positive) FP(False Positive)

Negative FN(False Negative) TN(True Negative)

P(Positive Sample) N(Negative Sample)

由上表可以计算的指标有:

Accuracy = (TP+TN)/(P+N)

Error Rate = 1 – Accuracy = (FP+FN)/(P+N)

False Positive Rate = Fallout = FP/N

True Positive Rate = Recall = Sensitivity = TP/P

False Negative Rate = Miss = FN/P

True Negative Rate = Specificity = TN/N

Positive Predictive Value = Precision = TP/(TP+FP)

Negative Predictive Value = TN/(TN+FN)

Prediction-conditioned Fallout = FP/(TP+FP)

Prediction-conditioned Miss = FN/(TN+FN)

Rate of Positive Predictions = Detection Prevalence = (TP+FP)/(P+N)

Rate of Negative Predictions = (TN+FN)/(P+N)

Prevalence = (TP+FN)/(P+N)

Detection Rate = TP/(P+N)

Balanced Accuracy = (Sensitivity+Specificity)/2

MCC(Matthews correlation coefficient) = (TP*TN - FP*FN)/[(TP+FP)(TP+FN)(TN+FP)(TN+FN)]^(1/2)

是不是感觉这些货已经组合完所有的分子/分母了?没关系,其实只要知道TP和TN越高越好就好了。

二、在R中计算混淆矩阵

这次使用ROCR包中的ROCR.simple数据集,其中prediction是预测值,labels为真实值。

require(ROCR)

data(ROCR.simple)

str(ROCR.simple)

## List of 2

## $ predictions: num [1:200] 0.613 0.364 0.432 0.14 0.385 ...

## $ labels : num [1:200] 1 1 0 0 0 1 1 1 1 0 ...

1 用table()直接计算

在确定好阀值后,可以直接用table函数计算列联表,再根据之前的公式计算各个指标。假设我们认为prediciton>0.5的都预测为1,其余为0.

pred.class <- as.integer(ROCR.simple$predictions > 0.5)

print(cft <- table(pred.class, ROCR.simple$labels))

##

## pred.class 0 1

## 0 91 14

## 1 16 79

通过对混淆矩阵中数值的计算可以得到:

tp <- cft[2, 2]

tn <- cft[1, 1]

fp <- cft[2, 1]

fn <- cft[1, 2]

print(accuracy <- (tp + tn)/(tp + tn + fp + fn))

## [1] 0.85

print(sensitivity <- tp/(tp + fn))

## [1] 0.8495

print(specificity <- tn/(tn + fp))

## [1] 0.8505

2 用confusionMatrix()算

如果不想手动算,可以借助caret包中的confusionMatrix函数计算。该函数既可以用混淆矩阵的结果,也可以直接输入预测/目标两列原始数据计算上述值。只要确定好positive分类是那个,就能得出跟之前一样的结果。

require(caret)

confusionMatrix(cft, positive = "1")

confusionMatrix(pred.class, ROCR.simple$labels, positive = "1")

## Confusion Matrix and Statistics

##

## Reference

## Prediction 0 1

## 0 91 14

## 1 16 79

##

## Accuracy : 0.85

## 95% CI : (0.793, 0.896)

## No Information Rate : 0.535

## P-Value [Acc > NIR] : <2e-16

##

## Kappa : 0.699

## Mcnemar's Test P-Value : 0.855

##

## Sensitivity : 0.849

## Specificity : 0.850

## Pos Pred Value : 0.832

## Neg Pred Value : 0.867

## Prevalence : 0.465

## Detection Rate : 0.395

## Detection Prevalence : 0.475

## Balanced Accuracy : 0.850

##

## 'Positive' Class : 1

##

三、混淆矩阵的缺点

1 需要自己确定阀值

可以看到,混淆矩阵4个值的确定都依赖于最直线我们主观设定的0.5。如果只依靠混淆矩阵这种原始的方法,那么不经过繁琐的试错我们无法确认哪个阀值是最好的。

2不平衡数据鲁棒性不好

一些positive事件发生概率极小的不平衡数据集(imbalanced data),混淆矩阵可能效果不好。比如对信用卡交易是否异常做分类的情形,很可能1w笔交易中只有1笔交易是异常的。一个将将所有交易都判定为正常的分类器,准确率是99.99%。这个数字虽然很高,但是没有任何现实意义。

在之前列举的各种分类模型评价方法中,收益图(Gain Chart)、提升图(Lift Chart)、KS图(KS Chart)、接受者操作特性曲线(ROC Chart)都对混淆矩阵的缺点的有改进。参考后续。

https://www.jianshu.com/p/c61feeb8b36f

混淆矩阵相关指标(sen,spe,auc,recall,f1)计算(R语言)

ConfusionMatrix

image.png

由上表可以计算的指标有:

Accuracy = (TP+TN)/(P+N)

Error Rate = 1 – Accuracy = (FP+FN)/(P+N)

False Positive Rate = Fallout = FP/N

True Positive Rate = Recall = Sensitivity = TP/P

False Negative Rate = Miss = FN/P

True Negative Rate = Specificity = TN/N

Positive Predictive Value = Precision = TP/(TP+FP)

Negative Predictive Value = TN/(TN+FN)

Prediction-conditioned Fallout = FP/(TP+FP)

Prediction-conditioned Miss = FN/(TN+FN)

Rate of Positive Predictions = Detection Prevalence = (TP+FP)/(P+N)

Rate of Negative Predictions = (TN+FN)/(P+N)

Prevalence = (TP+FN)/(P+N)

Detection Rate = TP/(P+N)

Balanced Accuracy = (Sensitivity+Specificity)/2

MCC(Matthews correlation coefficient) = (TPTN - FPFN)/[(TP+FP)(TP+FN)(TN+FP)(TN+FN)]^(1/2)

以ROCR自带的数据集ROCR.simple为例:

require(ROCR)

data(ROCR.simple)

str(ROCR.simple)

## List of 2

## $ predictions: num [1:200] 0.613 0.364 0.432 0.14 0.385 ...

## $ labels : num [1:200] 1 1 0 0 0 1 1 1 1 0 ...

pred.class <- as.integer(ROCR.simple$predictions > 0.5) #这里假设prediciton>0.5的预测为1,其余为0.

print(cft <- table(pred.class, ROCR.simple$labels)) #先prediction value 后label

## pred.class 0 1

## 0 91 14

## 1 16 79

Method 1: manual

tp <- cft[2, 2]

tn <- cft[1, 1]

fp <- cft[2, 1]

fn <- cft[1, 2]

print(accuracy <- (tp + tn)/(tp + tn + fp + fn))

## [1] 0.85

print(sensitivity <- tp/(tp + fn))

## [1] 0.8494624

print(specificity <- tn/(tn + fp))

## [1] 0.8504673

Method 2: caret

require(caret)

confusionMatrix(cft, positive = "1") #使用上面的table

## Confusion Matrix and Statistics

##

##

## pred.class 0 1

## 0 91 14

## 1 16 79

##

## Accuracy : 0.85

## 95% CI : (0.7928, 0.8965)

## No Information Rate : 0.535

## P-Value [Acc > NIR] : <2e-16

##

## Kappa : 0.6989

##

## Mcnemar's Test P-Value : 0.8551

##

## Sensitivity : 0.8495

## Specificity : 0.8505

## Pos Pred Value : 0.8316

## Neg Pred Value : 0.8667

## Prevalence : 0.4650

## Detection Rate : 0.3950

## Detection Prevalence : 0.4750

## Balanced Accuracy : 0.8500

##

## 'Positive' Class : 1

confusionMatrix(as.factor(pred.class), as.factor(ROCR.simple$labels),

positive = "1", mode = "everything")

## Confusion Matrix and Statistics

##

## Reference

## Prediction 0 1

## 0 91 14

## 1 16 79

##

## Accuracy : 0.85

## 95% CI : (0.7928, 0.8965)

## No Information Rate : 0.535

## P-Value [Acc > NIR] : <2e-16

##

## Kappa : 0.6989

##

## Mcnemar's Test P-Value : 0.8551

##

## Sensitivity : 0.8495

## Specificity : 0.8505

## Pos Pred Value : 0.8316

## Neg Pred Value : 0.8667

## Precision : 0.8316

## Recall : 0.8495

## F1 : 0.8404

## Prevalence : 0.4650

## Detection Rate : 0.3950

## Detection Prevalence : 0.4750

## Balanced Accuracy : 0.8500

##

## 'Positive' Class : 1

混淆矩阵 灵敏度 特异度 阳性预测值 阴性预测值 阳性似然比 阴性似然比相关推荐

  1. ROC/AUC 简易试验 最佳截断点 混淆矩阵 敏感度 特异度 约登指数 各个指标置信区间

    ROC/AUC 简易试验 最佳截断点 敏感度 特异度 约登 召回率 混淆矩阵 学习链接, 鼠标放这里 常规学习下面这个矩阵 金标准 + - 模型 + True Positives(TP) False ...

  2. 【模型评估】混淆矩阵(confusion_matrix)之 TP、FP、TN、FN;敏感度、特异度、准确率、精确率

    你这蠢货,是不是又把酸葡萄和葡萄酸弄"混淆"啦!!!这里的混淆,我们细品,帮助我们理解名词"混淆矩阵" 上面日常情况中的混淆就是:是否把某两件东西或者多件东西给 ...

  3. Python混淆矩阵(confusion_matrix)FP、FN、TP、TN、ROC,FROC,精确率(Precision),召回率(Recall),准确率(Accuracy),F1分数详述与实现

    目录 一.FP.FN.TP.TN 二.准确率(Accuracy).精确率(Precision).召回率(Recall).F1score 2.1.准确率(Accuracy) 2.2.召回率(Recall ...

  4. 逻辑回归模型混淆矩阵评价与ROC曲线最佳阈值的好处(附Accuracy,TPR,FPR计算函数)

    一.得到阈值在0.5和0.8下模型的混淆矩阵 y_prob=result.predict(X_test)#得到概率值y_predict1=pd.DataFrame(y_prob>0.5).ast ...

  5. 混淆矩阵中的 精确率 和 召回率 与 置信度 之间有什么关系

    True 表示正样本,False 表示负样本,Positive 表示预测为 真,Negative 表示预测为 假 -- 题记 混淆矩阵 假阳性 F P FP FP 在 左下角,假阴性 F N FN F ...

  6. 敏感性、特异度、α、β、阳性预测值(PPV)、阴性预测值(NPV)等指标及置信区间计算(附R语言代码)

    这个虽然简单但老是被绕进去,所以整理一下方便查阅. 首先画一个2×2的混淆矩阵confusion matrix: TP = True positive(真阳性) FP = False positive ...

  7. 为多模型寻找模型最优参数、多模型交叉验证、可视化、指标计算、多模型对比可视化(系数图、误差图、混淆矩阵、校正曲线、ROC曲线、AUC、Accuracy、特异度、灵敏度、PPV、NPV)、结果数据保存

    使用randomsearchcv为多个模型寻找模型最优参数.多模型交叉验证.可视化.指标计算.多模型对比可视化(系数图.误差图.classification_report.混淆矩阵.校正曲线.ROC曲 ...

  8. Python将classification_report的结论转化为字典(dict)形式并提取模型的灵敏度(sensitivity)、特异度(specificity)、PPV和NPV指标、混淆矩阵图

    Python将classification_report的结论转化为字典(dict)形式并提取模型的灵敏度(sensitivity).特异度(specificity).PPV和NPV指标.混淆矩阵图 ...

  9. 影像组学视频学习笔记(25)-查看准确度、灵敏度、特异度及混淆矩阵、Li‘s have a solution and plan.

    本笔记来源于B站Up主: 有Li 的影像组学系列教学视频 本节(25)主要讲解: 通过sklearn包输出准确度.灵敏度.特异度及混淆矩阵 基本概念 代码实现 from sklearn.metrics ...

最新文章

  1. 5教程 watchout_Unit 5单元复习学案设计
  2. 报名 | 刘鹰教授:草根创新力:中国经济快速发展的秘诀
  3. 揭秘|超乎想象!未来50年将出现的九大黑科技……
  4. 使用read_html爬取网页表哥,Python笔记:用read_html()爬取table形式表格的网络数据...
  5. 察看网页源代码全攻略
  6. bsp 总结正规流程
  7. 苹果好还是华为好_苹果手机好还是华为手机好?专业手机测评师给出答复!
  8. linux实验十shell程序设计,实验二Linux Shell编程.doc
  9. python2.7使用教程_使用模块 - 廖雪峰 Python 2.7 中文教程
  10. 放置江湖html5源码,「放置江湖」——经典文字类放置武侠手游
  11. 如何ajax上传图片,ajax上传图片文件
  12. 哪里看计算机主板,电脑主板型号和参数在哪里查看
  13. 对链表进行插入排序。从第一个元素开始,该链表可以被认为已经部分排序。每次迭代时,从输入数据中移除一个元素,并原地将其插入到已排好序的链表中。
  14. 面向Java开发者的ChatGPT提示词工程(2)
  15. 江西省省赛中职网络安全-Windows操作系统渗透测试
  16. 2、java语言基础课程2
  17. No Babel config file detected for xxx(未检测到Babel配置文件)和Already included file name解决方案
  18. 测试管理办公室TMO职责
  19. 游戏的汇总,在github和码云上找的,主要是安卓游戏,还有垃圾分类游戏
  20. 战棋游戏中的简单策略方法:基于群表示论的思想

热门文章

  1. 洛谷P1551亲戚题解
  2. 转帖:一生必读经典书籍大全,看看你读过几本?
  3. 自动控制原理(5)——结构图的绘制
  4. 我赌你的电脑里肯定有漏洞—不信测测看
  5. 初级项目管理培训心得
  6. Win10域客户端强制更改壁纸
  7. 利用计算机模拟人类的活动属于,赠书 | 计算机能模拟人类心智吗?
  8. BUUCTF web(三)
  9. —— GPS测量原理及应用复习-8 ——
  10. 强化学习-入门教程(完整版)