1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

scikit-learnにない評価指標 (specificity, NPV) を計算するメソッドを実装してみた

Last updated at Posted at 2020-09-06

はじめに

とある論文に記載されている指標について、scikit-learn で計算しようとしたところ、sensitivity, recallについては単独で計算するメソッドが用意されていたが、specicity、NPVの2つについては、classification_reportは出力されるものの、単独で計算するメソッドが用意されていなかったので実装してみたメモ。

計算方法

ぶっちゃけ、ここ見てください、ハイ。
https://en.wikipedia.org/wiki/Sensitivity_and_specificity

用語の関係を整理するとこんな感じ。暗記の必要はなし。

  • PPV = precision
  • TPR = sensitivity = recall
  • TNR = specificity
  • NPV = とくに他の呼び方なし

ソース

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)

#使い方
こんな感じ。

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])
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?