When I tried to calculate the index described in a certain paper with scikit-learn, there was a method to calculate sensitivity and recall independently, but classification_report is output for both specicity and NPV. Although it is done, a method to calculate independently was not prepared, so I implemented it.
Look here, high. https://en.wikipedia.org/wiki/Sensitivity_and_specificity
The relationship between terms looks like this. No need to memorize.
Calculate using the confusion matrix.
from sklearn import metrics
def calc_metrics_derived_from_confusion_matrix(metrics_name, y_true, y_predict):
tn, fp, fn, tp = metrics.confusion_matrix(y_true, y_predict).ravel()
# PPV, precision
# TP / TP + FP
if metrics_name in ["PPV", "precision"]:
return tp / (tp + fp)
# NPV
# TN / TN + FN
if metrics_name in ["NPV"]:
return tn / (tn + fn)
# sensitivity, recall, TPR
# TP / TP + FN
if metrics_name in ["sensitivity", "recall", "TPR"]:
return tp / (tp + fn)
# specificity
# TN / TN + FP
if metrics_name in ["specificity"]:
return tn / (tn + fp)
Like this.
calc_metrics_derived_from_confusion_matrix("sensitivity",[0,0,1], [1,0,1])
calc_metrics_derived_from_confusion_matrix("PPV",[0,0,1], [1,0,1])
calc_metrics_derived_from_confusion_matrix("specificity",[0,0,1], [1,0,1])
calc_metrics_derived_from_confusion_matrix("NPV",[0,0,1], [1,0,1])
Recommended Posts