目录
- 1 混淆矩阵定义( C o n f u s i o n M a t r i x \rm{}Confusion~Matrix Confusion Matrix)
- 2 统计指标
- 3 P y t h o n \rm{}Python Python计算 A U R O C \rm{}AUROC AUROC和 A U P R \rm{}AUPR AUPR值
- 4 P y t h o n \rm{}Python Python绘制 A U R O C \rm{}AUROC AUROC和 A U P R \rm{}AUPR AUPR曲线
- 5 深入理解 A U R O C \rm{}AUROC AUROC和 A U P R \rm{}AUPR AUPR曲线
- 6 参考文献
1 混淆矩阵定义( C o n f u s i o n M a t r i x \rm{}Confusion~Matrix Confusion Matrix)
在分类统计指标的学习过程中,对混淆矩阵的学习是绕不过的一环。只有理解好了混淆矩阵才能对
F
p
r
、
T
p
r
、
R
e
c
a
l
l
、
P
r
e
c
i
s
i
o
n
、
A
U
R
O
C
、
A
P
U
R
\rm{}Fpr、Tpr、Recall、Precision、AUROC、APUR
Fpr、Tpr、Recall、Precision、AUROC、APUR等概念有更深刻的认识。混淆矩阵是通过模型在测试集上的预测来计算的,通过观察混淆矩阵可以更好的理解模型的优缺点。
如上图所示,列表示 A c t u a l l y P o s i t i v e a n d A c t u a l l y N e g a t i v e \rm{}Actually~Positive~ and~Actually ~Negative Actually Positive and Actually Negative指的是数据集中的真实标签( g r o u n d t r u t h l a b e l s \rm{ground~truth~labels} ground truth labels),行表示 A c t u a l l y P o s i t i v e a n d A c t u a l l y N e g a t i v e \rm{}Actually~ Positive~ and ~Actually~Negative Actually Positive and Actually Negative,指的是模型预测的结果,即模型认为标签是什么。
2 统计指标
混淆矩阵Python代码
from sklearn.metrics import confusion_matrix
confusion_matrix = confusion_matrix(y_test, y_predict)
2.1 混淆矩阵的分类统计指标
- T r u e P o s i t i v e s ( T P s ) : \rm{True~Positives~(TPs):} True Positives (TPs): 模型正确分类正样本的数量。
- T r u e N e g a t i v e s ( T N s ) \rm{True~Negatives ~(TNs)} True Negatives (TNs): 模型正确分类负样本的数量。
- F a l s e P o s i t i v e s ( F P s ) \rm{False~Positives ~(FPs)} False Positives (FPs): 模型将负样本错误的预测为正样本的数量。
- F a l s e N e g a t i v e s ( F N s ) \rm{False~ Negatives~ (FNs)} False Negatives (FNs): 模型将正样本错误的预测为负样本的数目。
2.1.1 T r u e P o s i t i v e R a t e ( T P R ) \rm{True ~Positive~ Rate(TPR)} True Positive Rate(TPR)
我们可以用混淆矩阵计算
T
r
u
e
P
o
s
i
t
i
v
e
R
a
t
e
(
T
P
R
)
a
n
d
F
a
l
s
e
P
o
s
i
t
i
v
e
R
a
t
e
(
F
P
R
)
\rm{} True~Positive ~Rate ~(TPR) ~and~ False~ Positive~ Rate ~(FPR)
True Positive Rate (TPR) and False Positive Rate (FPR),并且可以用
T
P
R
和
F
P
R
\rm{}TPR 和FPR
TPR和FPR 计算
A
r
e
a
U
n
d
e
r
t
h
e
R
e
c
e
i
v
e
r
O
p
e
r
a
t
i
n
g
C
h
a
r
a
c
t
e
r
i
s
t
i
c
(
A
U
R
O
C
)
\rm{}Area ~Under ~the~ Receiver ~Operating ~Characteristic (AUROC)
Area Under the Receiver Operating Characteristic(AUROC)
T
r
u
e
P
o
s
i
t
i
v
e
R
a
t
e
=
T
r
u
e
P
o
s
i
t
i
v
e
s
A
l
l
P
o
s
i
t
i
v
e
s
=
T
r
u
e
P
o
s
i
t
i
v
e
s
(
T
r
u
e
P
o
s
i
t
i
v
e
s
+
F
a
l
s
e
N
e
g
a
t
i
v
e
s
)
\begin{aligned} True ~Positive~ Rate = \cfrac {True~ Positives}{All~Positives} = \cfrac{True~Positives}{(True~Positives + False~Negatives)} \end{aligned}
True Positive Rate=All PositivesTrue Positives=(True Positives+False Negatives)True Positives
除此之外,TPR还有别的名称如
S
e
n
s
i
t
i
v
i
t
y
、
R
e
a
c
l
l
、
H
i
t
r
a
t
e
\rm{}Sensitivity、Reacll、Hit~rate
Sensitivity、Reacll、Hit rate 更多细节见参考文献[1]。
2.1.1 F a l s e P o s i t i v e R a t e ( F P R ) \rm{False ~Positive~ Rate(FPR)} False Positive Rate(FPR)
F
a
l
s
e
P
o
s
i
t
i
v
e
R
a
t
e
=
F
a
l
s
e
P
o
s
i
t
i
v
e
s
A
l
l
N
e
g
a
t
i
v
e
s
=
F
a
l
s
e
P
o
s
i
t
i
v
e
s
F
a
l
s
e
P
o
s
i
t
i
v
e
s
+
T
r
u
e
N
e
g
a
t
i
v
e
s
False ~Positive~ Rate =\cfrac{False ~Positives}{All~Negatives} = \cfrac{False ~Positives}{False~ Positives + True~ Negatives}
False Positive Rate=All NegativesFalse Positives=False Positives+True NegativesFalse Positives
2.2 准确率( A c c u r a c y \rm{Accuracy} Accuracy)
所有样本中被预测正确的样本的比率,分类模型总体判断的准确率(包括了所有class的总体准确率)。
from sklearn.metrics import accuracy
accuracy = accuracy_score(y_test, y_predict)
2.3 精确率( P r e c i s i o n \rm{Precision} Precision)
预测为正类的准确率:
T
P
T
P
+
F
P
\cfrac{TP}{TP + FP}
TP+FPTP
from sklearn.metrics import precision_score
precision = precision_score(y_test, y_predict)
2.4 召回率( R e c a l l \rm{Recall} Recall)
from sklearn.metrics import recall_score
recall = recall_score(y_test, y_predict)
2.5 F 1 \rm{}F_1 F1 值( F 1 − S c o r e \rm{F_1-Score} F1−Score)
用来衡量二分类模型精确度的一种指标。它同时兼顾了分类模型的准确率和召回率。F1分数可以看作是模型准确率和召回率的一种加权平均,它的最大值是1,最小值是0。
from sklearn.metrics import f1_score
f1_score(y_test, y_predict)
3 P y t h o n \rm{}Python Python计算 A U R O C \rm{}AUROC AUROC和 A U P R \rm{}AUPR AUPR值
4 P y t h o n \rm{}Python Python绘制 A U R O C \rm{}AUROC AUROC和 A U P R \rm{}AUPR AUPR曲线
5 深入理解 A U R O C \rm{}AUROC AUROC和 A U P R \rm{}AUPR AUPR曲线
6 参考文献
[1]MACHINE LEARNING Measuring Performance: The Confusion Matrix
[2]混淆矩阵、准确率、精确率、召回率、F值、ROC曲线、AUC、PR曲线-Sklearn.metrics评估方法