有些東西雖然簡單,但是還是要過個7、8遍纔不容易記混記亂。
-
Accuracy
… -
Sensitivity
在患病的所有人中(已經明確知道數據的分佈了!
),預測正確(判斷爲有病)的有多少?
-
Specificity
在未患病的所有人中(已經明確知道數據的分佈了!
),預測正確(判斷爲沒病)的有多少?
-
Recall
Sensitivity = Recall;再提一遍——已經明確知道數據的分佈了!
-
Precision
預測的都是有病的人中,真正有病的人有多少——查準率。
-
PPV-positive predictive value
預測的都是有病的人中,真正有病的人有多少——查準率。
⚠️:PPV = Precision
-
NPV-negative predictive value
在預測的所有沒有病的人中,真正沒病的人所佔的比例。
-
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