I implemented a method to calculate the evaluation index (specificity, NPV) that scikit-learn does not have

Introduction

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.

Method of calculation

Look here, high. https://en.wikipedia.org/wiki/Sensitivity_and_specificity

The relationship between terms looks like this. No need to memorize.

Source

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)

How to use

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

I implemented a method to calculate the evaluation index (specificity, NPV) that scikit-learn does not have
I tried to make a dictionary function that does not distinguish between cases
[Pyhton] I want to solve the problem that tkinter does not work on MacOS11
[Linux] How to install a package on a server that does not have a network environment (standalone)
When incrementing the value of a key that does not exist
A special Python codec that seems to know but does not know
I implemented the K-means method (clustering method)
[Python] I made a decorator that doesn't seem to have any use.
How to fix a bug that jupyter notebook does not start automatically
The module that should have been installed with pip does not run
I want to identify the alert email. --Is that x a wildcard? ---
I made a library konoha that switches the tokenizer to a nice feeling
I managed to solve the situation where Python does not work on Mac
A story that sometimes does not work if pip is up to date
Solution to the problem that build does not end when installing OpenCV (PEP517)
I tried a neural network Π-Net that does not require an activation function
I wrote a script to revive the gulp watch that will die soon
Defining a child class property or method does not override the parent class definition
I didn't have to write a decorator in the class Thank you contextmanager