| 预测为真 | 预测为假 | |
|---|---|---|
| 真实为真 | TP (True Positive) | FN (False Negative) |
| 真实为假 | FP (False Positive) | TN (True Negative) |
案例:
真实值:0 1 1 0 1 1 0 0 1 0 1 0 1 0 0
预测值:1 1 1 1 1 0 0 0 0 0 1 1 1 0 1

(1)精确率(precision、positive predictive value、ppv):
TP / (TP + FP)
即鉴定为阳性的所有数据中,确实为阳性的数据所占比。衡量算法的精确度。
(2)召回率(recall、sensitivity(敏感度)、True Positive Rate(真阳性率/TPR)):
TP / (TP + FN)
即所有确实为阳性的数据中被鉴定出来的比率。
(3)特异度(specificity、True Negative Rate(真阴性率/TNR)):
TN / (TN + FP)
即所有确实为阴性的数据中被鉴定出来的比率。
(4)假阳性率(False Positive Rate(FPR)):
FP / (FP + TN)
即所有确实为阴性的数据中被鉴定成阳性的比率。
(5)F1值:精确率和召回率的调和平均数。F1值越接近1,模型输出越好。
2 1 p r e c i s i o n + 1 r e c a l l \Large \frac 2 {\frac 1 {precision} + \frac 1 {recall}} precision1+recall12
在已经通过算法计算得到预测概率的前提下,每确定一个概率阈值,都可以产生一组新的预测值,每组新的预测值都可以和测试集中的真实值计算出一个新的混淆矩阵和对应的TPR、FPR。改变阈值,得到很多对不同的TPR、FPR后,以FPR为X坐标,TPR为Y坐标绘制成折线图,即得到ROC曲线。ROC曲线和X轴与X=1这两条线围成的图形面积叫AUC值,Area Under Curve。

在sklearn中绘制ROC曲线(数据事先准备):
# 导入metrics
from sklearn import metrics
# 定义一个绘图函数
def show_roc(y_test, scores, pos_label):
# 得到FPR、TPR和阈值
fpr, tpr, threshold = metrics.roc_curve(y_test, scores, pos_label=pos_label)
plt.plot(fpr, tpr, color='green')
# 绘制对角线
plt.plot(np.linspace(0,1,10),np.linspace(0,1,10),color='red',ls='--')
# 加X轴Y轴标签
plt.xlabel('FPR')
plt.ylabel('TPR')
# 加名字
plt.title('ROC space')
plt.show()
# 调用函数
show_roc(y_test, scores, 1)
