目录
一、概念整理
第一个字母T/F代表预测结果y_pred是否和实际情况y_test相符
第二个字母P/N代表预测结果的正负
- TP:true positive,被判定为正样本,实际上也是正样本
- TN:true negative,被判定为负样本,实际上也是负样本
- FP:false positive,被判定为正样本,实际上是负样本
- FN:false negative,被判定为负样本,实际上是正样本
二、应用指标
三、代码实现
confusion_matrix = confusion_matrix(y_test, y_pred)
[返回值]: ndarray of shape (n_classes, n_classes)
Confusion matrix whose i-th row and j-th column entry indicates the number of samples with true label being i-th class and predicted label being j-th class.例如:confution_matrix = array([[28, 7], [ 3, 23]])其中:n_classes = 2,class=0为负样本N,class=1为正样本P
- 28 -> 真值为0,预测为0 的样本数量 -> TN
- 7 -> 真值为0,预测为1 的样本数量 -> FP
- 3 -> 真值为1,预测为0 的样本数量 -> FN
- 23 -> 真值为1,预测为1 的样本数量 -> TP
confusion_matrix = confusion_matrix([0, 1, 0, 1], [1, 1, 1, 0])
# confusion_matrix = [[0 2],
# [1 1]]
tn, fp, fn, tp = confusion_matrix([0, 1, 0, 1], [1, 1, 1, 0]).ravel()
# (tn, fp, fn, tp) = (0,2,1,1)
sensitivity = confusion_matrix[1,1]/(confusion_matrix[1,1]+confusion_matrix[1,0])
print('Sensitivity : ', sensitivity )
specificity = confusion_matrix[0,0]/(confusion_matrix[0,0]+confusion_matrix[0,1])
print('Specificity : ', specificity)