About the average option in sklearn.metrics.f1_score

What is f1 score?

f1 = \frac{2 \times Recall \times Precision}{Recall + Precision} = \frac{1}{\frac{1}{Recall} \times \frac{1}{Precison}} = \frac{2 \times TP}{2 \times TP + FP + FN}

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.

Multi-class classification

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"

  1. Calculate TP, FP, FN as a whole.

    TP FP FN
    45 22 22
  2. Calculate with the obtained TP, FP.FN $f1 = \frac{2 \times TP}{2 \times TP + FP + FN} = \frac{90}{90+22+22} = 0.67164179104\dots$

"macro"

  1. Calculate Recall and Precision for each class

    class Recall Precision
    a \frac{10}{18} \frac{10}{18}
    b \frac{20}{27} \frac{20}{26}
    c \frac{15}{22} \frac{15}{23}
  2. Calculate the average Recall and Precision

    Recall Precision
    \frac{1}{3}\sum{Recall} \frac{1}{3}\sum{Precision}
  3. Calculate f1 with the calculated average

    \frac{1}{\frac{1}{\frac{1}{3}\sum{Recall}} \times \frac{1}{\frac{1}{3}\sum{Precision}}}

"weighted"

  1. Multiply the number of data in each class by the individual Recall, Precision

    class Recall Precision
    a \frac{10}{18} \times 18 \frac{10}{18}\times 18
    b \frac{20}{27}\times 18 \frac{20}{26}\times 18
    c \frac{15}{22}\times 18 \frac{15}{23}\times 18
  2. Divide the sum of Recall and Precision by the total number of data

    Recall Precision
    \frac{1}{67}\sum{Recall} \frac{1}{67}\sum{Precision}
  3. Calculate f1 with the calculated average

    \frac{1}{\frac{1}{\frac{1}{67}\sum{Recall}} \times \frac{1}{\frac{1}{67}\sum{Precision}}}

"samples" I'm not sure, so I'll add it as soon as I understand it.

Recommended Posts

About the average option in sklearn.metrics.f1_score
About the test
About the queue
About the difference between "==" and "is" in python
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
Sort in Python. Next, let's think about the algorithm.
About testing in the implementation of machine learning models
About the inefficiency of data transfer in luigi on-memory
About the Unfold function
About the service command
About the uncluttered arrangement in the import order of flake8
About the --enable-shared option when building Python on Linux
About the confusion matrix
About the Visitor pattern
About __all__ in python
A reminder about the implementation of recommendations in Python
About the garbled Japanese part of pandas-profiling in Jupyter notebook
About the number (section number) in () displayed by the Linux man command
Find the average / standard deviation of the brightness values in the image
Download the file in Python
Find the difference in Python
About the Python module venv
About the ease of Python
About the enumerate function (python)
About the traveling salesman problem
Methods available in the list
About understanding the 3-point reader [...]
About the components of Luigi
About the features of Python
About "for _ in range ():" in python
I compared the calculation time of the moving average written in Python
How to give and what the constraints option in scipy.optimize.minimize is
Think about why Kubernetes is described as "Linux in the cloud"