python confusion matrix (confusion_matrix) FP, FN, TP, TN, accuracy rate (Precision), recall (Recall), accuracy (Accuracy) detailed

table of Contents

A, FP, FN, TP, TN

Second, the exact ratio (Precision), recall (the Recall), accuracy (the Accuracy)


A, FP, FN, TP, TN

You idiot, is not sour grapes and grape sour again get "confused" "friends !!!

Confusion in everyday situations above is: whether the one or two things and more things to confused, confused.

In machine learning, confusion matrix is an error matrix, used to visually evaluate the performance of supervised learning algorithm. . Confusion matrix size (n_classes, n_classes) phalanx, wherein n_classes represents the number of classes.

Wherein the line of this matrix shows an example of class prediction (prediction model can be understood as an output, Predict), another column showing the determination results of the model prediction is correct, the correct prediction result is True tag (Ground Truth), and vice versa to False.

In machine learning ground truth expressed supervised learning the training set of classification accuracy for proving a hypothesis or overthrow. Supervised machine learning approach to training data marking, labeling Imagine if the training error, it will have an impact prediction of test data, so there will be those marking the correct data to be ground truth.

At this time, on the introduction of FP, FN, TP, TN and precision ratio (Precision), recall (the Recall), accuracy (Accuracy).

In two cats classified as an example, assuming cat positive cases -Positive , Dog negative embodiment -Negative ; predicted correctly is True, otherwise it is False . We can get below this one for FP, FN, TP, TN table:

 At this time, as shown in the following code, wherein the confusion matrix scikit-learn function sklearn.metrics.confusion_matrix API interfaces, confusion matrix can be used to draw

skearn.metrics.confusion_matrix(
    y_true,   # array, Gound true (correct) target values
    y_pred,  # array, Estimated targets as returned by a classifier
    labels=None,  # array, List of labels to index the matrix.
    sample_weight=None  # array-like of shape = [n_samples], Optional sample weights
)

Complete sample code is as follows:

__author__ = "lingjun"
# E-mail: [email protected]
# welcome to attention:小白CV

import seaborn as sns
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
sns.set()

f, (ax1,ax2) = plt.subplots(figsize = (10, 8),nrows=2)
y_true = ["dog", "dog", "dog", "cat", "cat", "cat", "cat"]
y_pred = ["cat", "cat", "dog", "cat", "cat", "cat", "cat"]
C2= confusion_matrix(y_true, y_pred, labels=["dog", "cat"])
print(C2)
print(C2.ravel())
sns.heatmap(C2,annot=True)

ax2.set_title('sns_heatmap_confusion_matrix')
ax2.set_xlabel('Pred')
ax2.set_ylabel('True')
f.savefig('sns_heatmap_confusion_matrix.jpg', bbox_inches='tight')

 Saved image as follows:

This time we still do not know skearn.metrics.confusion_matrix done, this time print (C2), C2 print look exactly what it contains. The final print result is as follows:

[[1 2]
 [0 4]]
[1 2 0 4]

Under the above interpretation of the meaning of these numbers:

C2= confusion_matrix(y_true, y_pred, labels=["dog", "cat"])中的labels的顺序就分布是0、1,negative和positive

注:labels=[]可加可不加,不加情况下会自动识别,自己定义
  • cat is a 1-positive, of which there are four real cat values, is predicted to cat 4, prediction is correct T, 0 th Dog is predicted, the prediction error F.;
  • dog is 0-negative, which has three dog real values, is predicted to be a dog, correctly predict T, 2 th CAT is predicted, the prediction error F.

Therefore: TN = 1, FP = 2, FN = 0, TP = 4.

  • TN = 1: negative forecast for the dog in a predicted correctly
  • FP = 2: positive forecast for the cat in two predicted mistake
  • FN = 0: negative forecast for the dogs 0 is the prediction error
  • TP = 4: positive forecast for the cats in four predicted correctly

This time the above predictions cats and dogs and then let's see, six were predicted to cat, but only four are true cat, this time on the right side and the red circle on the correspondence.

y_pred = ["cat", "cat", "dog", "cat", "cat", "cat", "cat"]

y_true = ["dog", "dog", "dog", "cat", "cat", "cat", "cat"]

Second, the exact ratio (Precision), recall (the Recall), accuracy (the Accuracy)

With these above values, can be calculated as follows work

Accuracy (Accuracy) : these three indicators is the most intuitive accuracy: a scale model to determine the correct data (TP + TN) of the total data

"Accuracy: "+str(round((tp+tn)/(tp+fp+fn+tn), 3))

Recall (the Recall) : for a data set of all positive examples label (TP + FN), the positive cases (TP) determines that the model accounts for the correct data set of positive examples all proportions ; FN represents a negative model of Example mistaken but the actual data is positive examples ; recall also called the recall to object detection, for example, we tend to picture objects as a positive example, high recall at this time represents the model can find more objects in the picture!

"Recall: "+str(round((tp)/(tp+fn), 3))

Precise ratio (Precision) : For all positive cases (TP + FP) is judged in terms of the model , wherein the real cases (TP) proportion. Accuracy rate, also known as precision, or to object detection, for example, a high precision model representing the detected object is indeed a most object, the object is not only a small number of objects are treated as objects.

"Precision: "+str(round((tp)/(tp+fp), 3))

and also:

("Sensitivity: "+str(round(tp/(tp+fn+0.01), 3)))
("Specificity: "+str(round(1-(fp/(fp+tn+0.01)), 3)))
("False positive rate: "+str(round(fp/(fp+tn+0.01), 3)))
("Positive predictive value: "+str(round(tp/(tp+fp+0.01), 3)))
("Negative predictive value: "+str(round(tn/(fn+tn+0.01), 3)))

References:

https://baijiahao.baidu.com/s?id=1619821729031070174&wfr=spider&for=pc

https://www.cnblogs.com/klchang/p/9608412.html

https://blog.csdn.net/littlehaes/article/details/83278256


White CV : No. designed to focus public CV (computer vision), AI (artificial intelligence) technology-related fields, the main content of the article around the C ++, Python programming techniques, machine learning (ML), the depth of learning (DL), OpenCV image processing, etc. technology, explore the depth of technical points, study and work record common operations, problems do you learn to work assistant. Only concerned with technology, the professional knowledge sharing platform CV field.

 

发布了74 篇原创文章 · 获赞 64 · 访问量 13万+

Guess you like

Origin blog.csdn.net/wsLJQian/article/details/99435808