Machine-learning-with-python-confusion-matrix

提供:Dev Guides
移動先:案内検索

機械学習-混同マトリックス

出力が2つ以上のタイプのクラスである可能性がある分類問題のパフォーマンスを測定する最も簡単な方法です。 混同マトリックスは、2次元のテーブルにすぎません。 「実際」と「予測」、さらに、両方の次元には、以下に示すように「真のポジティブ(TP)」、「真のネガティブ(TN)」、「偽のポジティブ(FP)」、「偽のネガティブ(FN)」があります-

混同マトリックス

混同マトリックスに関連する用語の説明は次のとおりです-

  • * True Positives(TP)*-データポイントの実際のクラスと予測クラスの両方が1の場合です。
  • * True Negatives(TN)*-データポイントの実際のクラスと予測クラスの両方が0の場合です。
  • * False Positives(FP)*-データポイントの実際のクラスが0で、データポイントの予測クラスが1の場合です。
  • * False Negatives(FN)*-データポイントの実際のクラスが1で、データポイントの予測クラスが0の場合です。

sklearnの_confusion_matrix()_関数の助けを借りて混同行列を見つけることができます。 次のスクリプトの助けを借りて、上記の構築されたバイナリ分類の混同行列を見つけることができます-

from sklearn.metrics import confusion_matrix

出力

[[Accuracy

It may be defined as the number of correct predictions made by our ML model. We can easily calculate it by confusion matrix with the help of following formula −

$$Accuracy = \frac\{TP+TN}\{TP+FP+FN+TN}$$

For above built binary classifier, TP + TN = 73+144 = 217 and TP+FP+FN+TN = 73+7+4+144=228.

Hence, Accuracy = 217/228 = 0.951754385965 which is same as we have calculated after creating our binary classifier.

=== Precision

Precision, used in document retrievals, may be defined as the number of correct documents returned by our ML model. We can easily calculate it by confusion matrix with the help of following formula −

$$Precision = \frac\{TP}\{TP+FP}$$

For the above built binary classifier, TP = 73 and TP+FP = 73+7 = 80.

Hence, Precision = 73/80 = 0.915

=== Recall or Sensitivity

Recall may be defined as the number of positives returned by our ML model. We can easily calculate it by confusion matrix with the help of following formula −

$$Recall = \frac\{TP}\{TP+FN}$$

For above built binary classifier, TP = 73 and TP+FN = 73+4 = 77.

Hence, Precision = 73/77 = 0.94805

=== Specificity

Specificity, in contrast to recall, may be defined as the number of negatives returned by our ML model. We can easily calculate it by confusion matrix with the help of following formula −

$$Specificity = \frac\{TN}\{TN+FP}$$

For the above built binary classifier, TN = 144 and TN+FP = 144+7 = 151.

Hence, Precision = 144/151 = 0.95364