Is there any support function to calculate the average F1-score range?

from sklearn.metrics import f1_score

y_true = [0, 1, 2, 0, 1, 2]
y_pred = [0, 2, 1, 0, 0, 1]

f1_score(y_true, y_pred, average='weighted')

From documentation

Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters ‘macro’ to account for label imbalance; it can result in an F-score that is not between precision and recall.


|improve this answer|||||

One function from Scikit-learn that you can use is the classification_report (docs).

Here is an example:

from sklearn.metrics import classification_report

y_true = ["A", "B", "C", "A", "A", "B", "A", "A", "C", "B", "A", "A", "B", "A", "C", "C"]
y_pred = ["A", "B", "C", "A", "B", "C", "C", "B", "C", "B", "A", "A", "B", "C", "C", "C"]

report = classification_report(y_true=y_true, y_pred=y_pred)

>>               precision    recall  f1-score   support
>>            A       1.00      0.50      0.67         8
>>            B       0.60      0.75      0.67         4
>>            C       0.57      1.00      0.73         4
>>    micro avg       0.69      0.69      0.69        16
>>    macro avg       0.72      0.75      0.69        16
>> weighted avg       0.79      0.69      0.68        16

From this, you can extract the F1-score per class. This can be useful because you can see in more details where your model isn't performing well.

You can also see the micro, macro and weighted averages.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.