# F1 Score

## Goal¶

This post aims to introduce one of the model evaluation metrics, called F1 score. F1 score is used to measure the overall model performance. As F1 score is higher, the model performance would be better in general.

F1 score is defined as the following equations:

$$F_1 = 2 \cdot \frac{precision \cdot recall}{precision + recall}$$

Reference

## Libraries¶

In [10]:
from sklearn.metrics import f1_score, precision_score, recall_score
import pandas as pd


## Create a prediction and ground truth¶

In [7]:
df_prediction = pd.DataFrame([0, 1, 0, 1, 0 ,1, 0, 1],
columns=['prediction'])
df_prediction

Out[7]:
prediction
0 0
1 1
2 0
3 1
4 0
5 1
6 0
7 1
In [8]:
df_groundtruth = pd.DataFrame([0, 0, 0 , 0, 1, 1, 1, 1],
columns=['gt'])
df_groundtruth

Out[8]:
gt
0 0
1 0
2 0
3 0
4 1
5 1
6 1
7 1

## Compute F1 Score¶

In [9]:
f1_score(y_true=df_groundtruth,
y_pred=df_prediction)

Out[9]:
0.5

### double check by precision and recall¶

In [11]:
precision_score(y_true=df_groundtruth,
y_pred=df_prediction)

Out[11]:
0.5
In [12]:
recall_score(y_true=df_groundtruth,
y_pred=df_prediction)

Out[12]:
0.5
In [13]:
2 * (0.5 * 0.5) / (0.5 + 0.5)

Out[13]:
0.5