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]:
In [8]:
df_groundtruth = pd.DataFrame([0, 0, 0 , 0, 1, 1, 1, 1],
columns=['gt'])
df_groundtruth
Out[8]:
Compute F1 Score¶
In [9]:
f1_score(y_true=df_groundtruth,
y_pred=df_prediction)
Out[9]:
double check by precision and recall¶
In [11]:
precision_score(y_true=df_groundtruth,
y_pred=df_prediction)
Out[11]:
In [12]:
recall_score(y_true=df_groundtruth,
y_pred=df_prediction)
Out[12]:
In [13]:
2 * (0.5 * 0.5) / (0.5 + 0.5)
Out[13]: