Sklearn f1 Score Multiclass Implementation with examples

Sklearn f1 Score Multiclass Implementation with examples

Sklearn f1 score multiclass is average of f1 scores from each classes. The sklearn provide the various methods to do the averaging. We may provide the averaging methods as parameters in the f1_score() function. You may choose any o the value from this list {‘micro’, ‘macro’, ‘samples’,’weighted’, ‘binary’} and parameterize into the function. The default value is None. This default value works in the case of the binary classification.


Sklearn f1 score multiclass Implementation :

In order to demonstrate the sklearn f1 score multiclass Implementation we need a trained model. But it will not be relevant to create a dummy model. Hence we will mock the actual outcome array and predicted array. Lets directly jump into the coding part.

from sklearn.metrics import f1_score
y_true = [1, 1, 2, 3, 1, 2]
y_pred = [0, 1, 1, 3, 1, 2]
f1_score(y_true, y_pred, average='weighted')

1. Here the first thing we do is importing. All the evaluation matrices for down streaming tasks is mostly available in sklearn.metrics python package.
2. After it, as I have already discussed the dummy array creation for demo of the concept.
3. Finally, we will invoke the f1_score() with the above value as a parameters. Here we will also define the averaging technique.


Why f1 score multiclass is Required  over Accuracy ?

See ! Accuracy works well with balance dataset. I mean the data set where the different classes have same distribution and frequencies.  In the opposite side the precision and recall are useful when our dataset is not balance.  Now lets go ahead, In some real scenarios where precision and recall both becomes necessary for the model evaluation, Here we need f_score(). Actually if you observe it carefully you will find its the hormonic mean of the precision and recall parameters.

I hope this article must be a short and  informative source for you. Still if you have any concerns please comment below in the comment section. Please provide your feedbacks.



Data Science Learner Team


Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages, where he and his team share knowledge and help others learn more about data science.
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner