Model Evaluation - F1 score

First published on February 9, 2022

Last updated at March 28, 2022

 

3 minute read

Nathaniel Tjandra

TLDR

In this Mage Academy lesson on model evaluation, we’ll learn how to calculate F1 score and what it means.

Glossary

  • Definition

  • Calculation

  • How to code

Definition

The F1-Score is an advanced metric that merges together the results of the 

precision

and 

recall

to form a better estimation for a classification model.

F1 Score ranges from 0 to 1. As for whether values are good or bad for a model, it depends on your model’s situation and what your use case will be satisfied with. As a golden rule, we know that “the higher the better”. But to assess F1-Score, we’ll need to think about what getting a “1” means, getting a “0” means, and what any numbers in between may mean. 

A “1” means the model has 

classified all objects, having zero false results. (Source: Ed Sheeran)

On the other hand, a 0 means the model has blundered every single result.

When you blunder you miss everything. (Source: Chess.com)

With that being said, the F1-Score is a good indicator of which models, if you have retrained any or are comparing them, is better. In this case, select the higher F1-Score.

Outstanding models will stand out (Source: Prolifiko)

Calculation

F1-Score is calculated by taking the harmonic mean of the precision and recall values. The harmonic mean is the total number of values divided by the reciprocal of the sums of both values. 

We’ll reuse our values from our precision and recall lesson from this 

.

Our calculated values for precision and recall were (515/520) and (515/547) respectively. Next, we’ll take the reciprocal by dividing 1 by each value, thereby flipping the fraction. (520/515) + (547/515). Finally, we’ll divide 2 by the result to get 0.9655.

From this value we can tell that we have a pretty good precision and recall score for an excellent model.

How to code

F1 Score can be calculated from scratch by coding the harmonic mean function, or can also be done with the SKLearn library. 

From scratch

From our previous lessons we calculated Precision and Recall as 0.5 each.

Now we’ll use those values and calculate the harmonic mean of the two.

With SKLearn

Want to learn more about machine learning (ML)? Visit 

! ✨🔮