It is an index of the balance between Recall (recall rate, sensitivity) and Precision (goodness of fit, accuracy) indicated by. Since it is a harmonic mean, the score will be low if either one is extremely low.
For the sake of simplicity, classify into 3 classes (4 or more classes can be considered in the same way)
Expected class | ||||
---|---|---|---|---|
a | b | c | ||
a | 10 | 3 | 5 | |
Correct answer class | b | 4 | 20 | 3 |
c | 4 | 3 | 15 |
When there is a confusion matrix like this, TP, FP, and FN are defined as follows.
class | TP | FP | FN |
---|---|---|---|
a | 10 | 8 | 8 |
b | 20 | 6 | 7 |
c | 15 | 8 | 7 |
FP is the sum of vertical elements and FN is the sum of horizontal elements other than diagonal elements.
sklearn.metrics.f1_score
[https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html] If you look at (), you can see the options " binary "
, " micro "
, " macro "
, There are " weighted "
, " samples "
. (Same for recall_score and precision_score)
" binary "
is used in binary classification.
Others are described below.
"micro"
Calculate TP, FP, FN as a whole.
TP | FP | FN |
---|---|---|
45 | 22 | 22 |
Calculate with the obtained TP, FP.FN
"macro"
Calculate Recall and Precision for each class
class | Recall | Precision |
---|---|---|
a | ||
b | ||
c |
Calculate the average Recall and Precision
Recall | Precision |
---|---|
Calculate f1 with the calculated average
"weighted"
Multiply the number of data in each class by the individual Recall, Precision
class | Recall | Precision |
---|---|---|
a | ||
b | ||
c |
Divide the sum of Recall and Precision by the total number of data
Recall | Precision |
---|---|
Calculate f1 with the calculated average
"samples"
I'm not sure, so I'll add it as soon as I understand it.
Recommended Posts