F1 Score - Classification Error Metric With Examples

Hey, readers! Today, we will be focusing on an important error metric for Classification Algorithms โ€” F1 Score in Python. So, let us begin!

What is F1 score?

F1 score is a Classification error metric that like any other error metric helps us evaluate the performance of an algorithm. It helps us evaluate the performance of the machine learning model in terms of binary classification.

It is a combination of precision and recall metrics and is termed as the harmonic mean of precision and recall. It is basically used in cases when the data is imbalanced or there is a binary classification in the dataset.

Have a look at the below formulaโ€“

F1 score increases as the precision and recall value rises for a model.

A high score indicates that the model is well versed in terms of handling the class imbalance problem.

Let us now focus on the practical implementation of the same in the upcoming section.

Applying F1 Score on Loan Dataset

Here, we would be implementing the evaluation metrics on Loan Defaulter Prediction. You can find the dataset here.

1. Load the dataset

We have used pandas.read_csv() function to load the dataset into the environment.

2. Split the dataset

Further, we have splitted the dataset using train_test_split() function as shownโ€“

3. Defining the error metrics

Here, we have defined the confusion matrix and other error metrics using customized functions.

4. Modelling

We have applied Decision Tree algorithm on the dataset as shown belowโ€“

5. Evaluation of the model

Now, having applied the model, now we have evaluated the model with the metrics defined in the above section.


F1 Score with sklearn library

In this example, we have used the built-in function from sklearn library to calculate the f1 score of the data values. The f1_score() method is used to calculate the score value without having to explicitly make use of the precision and recall values.



By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

Till then, Stay tuned and Keep Learning!! ๐Ÿ™‚


By admin

Leave a Reply

%d bloggers like this: