前言
深度模型的评估方法
一、混淆矩阵
正确预测与错误预测的四种结果组成混淆矩阵
正例 | 假例 | |
---|---|---|
正例 | 真正例TP | 伪反例FN |
假例 | 伪正例FP | 真反例TN |
二、精确率与召回率
1.精确率
预测结果为正例样本中真是为正例的比例(查的准不准)
T
P
T
P
+
F
P
\frac{TP}{TP+FP}
TP+FPTP
2.召回率
真实为正例的样本中预测结果为正例的比例(查的全,对正样本的区分能力)
T
P
T
P
+
F
N
\frac{TP}{TP+FN}
TP+FNTP
三、F1-score
反应了模型的稳健性
F
1
=
2
T
P
2
T
P
+
F
N
+
F
P
=
2
∗
P
r
e
c
i
s
i
o
n
∗
R
e
c
a
l
l
P
r
e
c
i
s
i
o
n
+
R
e
c
a
l
l
F1=\frac{2TP}{2TP+FN+FP}=\frac{2*Precision*Recall}{Precision+Recall}
F1=2TP+FN+FP2TP=Precision+Recall2∗Precision∗Recall
四、案例
1.代码
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
if __name__ == '__main__':
#获取数据
names = ['Sample code number', 'Clump Thickness', 'Uniformity of Cell Size',
'Uniformity of Cell Shape', 'Marginal Adhesion', 'Single Epithelial Cell Size',
'Bare Nuclei', 'Bland Chromatin', 'Normal Nucleoli', 'Mitoses', 'Class']
data = pd.read_csv('E:/breast+cancer+wisconsin/breast-cancer-wisconsin.data',names=names)
print(data.head())
# 基本数据处理
# 缺失值处理
data = data.replace(to_replace='?', value=np.NaN)
data = data.dropna()
#取出特征值和目标值
data_x = data.iloc[:, 1:-1]
# print(data_x)
data_y = data['Class']
# print(data_y)
# 分割数据
x_train,x_test,y_train,y_test = train_test_split(data_x,data_y,random_state=2,test_size=0.2)
# 标准化
tranfer = StandardScaler()
x_train = tranfer.fit_transform(x_train)
x_test = tranfer.fit_transform(x_test)
# 逻辑回归
estimator = LogisticRegression()
estimator.fit(x_train,y_train)
# 预测
y_pre = estimator.predict(x_test)
print(y_pre)
score = estimator.score(x_test,y_test)
print(score)
ret = classification_report(y_test,y_pre,target_names={'良性','恶性'})
print(ret)
2.输出结果
precision recall f1-score support
良性 0.94 0.95 0.95 83
恶性 0.92 0.91 0.92 54
avg / total 0.93 0.93 0.93 137
五、TPR与FPR
1.TPR
真实类别是1的预测类别是1的比例
T
P
T
P
+
F
N
\frac{TP}{TP+FN}
TP+FNTP
2.FPR
真是类别是0的预测类别是1的比例
F
P
F
P
+
T
N
\frac{FP}{FP+TN}
FP+TNFP
六、ROC曲线
ROC曲线之间没有交点
如下图所示,A,B,C三个模型对应的ROC曲线之间交点,且AUC值是不相等的,此时明显更靠近( 0 , 1 ) (0,1)(0,1)点的A模型的分类性能会更好。
ROC曲线之间存在交点
如下图所示,模型A、B对应的ROC曲线相交却AUC值相等,此时就需要具体问题具体分析:当需要高Sensitivity值时,A模型好过B;当需要高Specificity值时,B模型好过A。
七、AUC指标
auc指标的意义指随机取一对正负样本,正样本的得分大于负样本的概率,auc的最小值为0.5,最大值为1,取值越高越好。