How do I calculate the range of a F1-score from a confusion matrix of 3 class,A,B,C
Is there any support function to calculate the average F1-score range?
Topic mathematics confusion-matrix machine-learning
Category Data Science
Is there any support function to calculate the average F1-score range?
Topic mathematics confusion-matrix machine-learning
Category Data Science
Refer to this Github Gist.
For convenience I copy-pasted the code from the link here (comments ommited):
def get_f1_score(confusion_matrix, i):
TP = 0
FP = 0
TN = 0
FN = 0
for j in range(len(confusion_matrix)):
if (i == j):
TP += confusion_matrix[i, j]
tmp = np.delete(confusion_matrix, i, 0)
tmp = np.delete(tmp, j, 1)
TN += np.sum(tmp)
else:
if (confusion_matrix[i, j] != 0):
FN += confusion_matrix[i, j]
if (confusion_matrix[j, i] != 0):
FP += confusion_matrix[j, i]
recall = TP / (FN + TP)
precision = TP / (TP + FP)
f1_score = 2 * 1/(1/recall + 1/precision)
return f1_score
When you want to calculate F1 of the first class label, use it like: get_f1_score(confusion_matrix, 0)
.
You can then average F1 of all classes to obtain Macro-F1.
By the way, this site calculates F1, Accuracy, and several measures from a 2X2 confusion matrix easy as pie.
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)
print(report)
>> 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.
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.
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.f1_score.html
Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.