有些东西虽然简单,但是还是要过个7、8遍才不容易记混记乱。
-
Accuracy
… -
Sensitivity
在患病的所有人中(已经明确知道数据的分布了!
),预测正确(判断为有病)的有多少?
S e n s i t i v i t y = T P T P + F N Sensitivity = \frac{TP}{TP + FN} Sensitivity=TP+FNTP -
Specificity
在未患病的所有人中(已经明确知道数据的分布了!
),预测正确(判断为没病)的有多少?
S p e c i f i c i t y = T N T N + F P Specificity=\frac{TN}{TN + FP} Specificity=TN+FPTN -
Recall
Sensitivity = Recall;再提一遍——已经明确知道数据的分布了!
-
Precision
预测的都是有病的人中,真正有病的人有多少——查准率。
P r e c i s i o n = T P T P + F P Precision = \frac{TP}{TP + FP} Precision=TP+FPTP -
PPV-positive predictive value
预测的都是有病的人中,真正有病的人有多少——查准率。
P P V = T P T P + F P PPV = \frac{TP}{TP + FP} PPV=TP+FPTP
⚠️:PPV = Precision
-
NPV-negative predictive value
在预测的所有没有病的人中,真正没病的人所占的比例。
N P V = T N T N + F N NPV = \frac{TN}{TN + FN} NPV=TN+FNTN -
AUC - ROC曲线下方的面积大小
ROC:参考维基百科
从AUC判断分类器(预测模型)优劣的标准:
- AUC = 1,是完美分类器,采用这个预测模型时,存在至少一个阈值能得出完美预测。绝大多数预测的场合,不存在完美分类器。
- 0.5 < AUC < 1,优于随机猜测。这个分类器(模型)妥善设定阈值的话,能有预测价值。
- AUC = 0.5,跟随机猜测一样(例:丢铜板),模型没有预测价值。
- AUC < 0.5,比随机猜测还差;但只要总是反预测而行,就优于随机猜测。
CODE
Sensitivity(Recall):
def Sensitivity(y_true, y_pred):
"""
param:
y_pred - Predicted labels
y_true - True labels
Returns:
Sensitivity score
"""
neg_y_true = 1 - y_true
neg_y_pred = 1 - y_pred
fn = np.sum(neg_y_pred * y_true)
tp = np.sum(y_true * y_pred)
sensitivity = tp / (tp + fn)
return sensitivity
Specificity:
def specificity(y_true, y_pred):
"""
param:
y_pred - Predicted labels
y_true - True labels
Returns:
Specificity score
"""
neg_y_true = 1 - y_true
neg_y_pred = 1 - y_pred
fp = np.sum(neg_y_true * y_pred)
tn = np.sum(neg_y_true * neg_y_pred)
specificity = tn / (tn + fp)
return specificity
PPV(Precision):
def PPV(y_true, y_pred):
"""
param:
y_pred - Predicted labels
y_true - True labels
Returns:
PPV score
"""
neg_y_true = 1 - y_true
neg_y_pred = 1 - y_pred
fp = np.sum(neg_y_pred * y_true)
tp = np.sum(y_true * y_pred)
PPV = tp / (tp + fp)
return PPV
NPV:
def NPV(y_true, y_pred):
"""
param:
y_pred - Predicted labels
y_true - True labels
Returns: NPV
"""
neg_y_true = 1 - y_true
neg_y_pred = 1 - y_pred
tn = np.sum(neg_y_true * neg_y_pred)
NPV = tn / np.sum(neg_y_pred)
return NPV