Ya = [False, False, True, True, True, False, True, False, False, True]
And the calculated or predicted output is:
Y = [True, False, True, True, False, False, True, False, False, True]
So, in this example, the total number of:
True Positive = output labels that are predicted to be True and they are actually True = 4
True Negative = the output labels that are predicted to be False and they are actually False also = 4
False Positive = the output labels that are predicted to be True but they are actually False = 1
False Negative = output labels that are predicted to be False but they are actually True = 1
So, in our example, TP = 4, FP = 1, FN = 1
So,
And
Therefore,
How to calculate the F1 score using the sklearn library in Python?
We can use the following Python code to calculate the F1 score of a machine learning model.
from sklearn.metrics import precision_score, recall_score, f1_score
Ya = [False, False, True, True, True, False, True, False, False, True]
Y = [True, False, True, True, False, False, True, False, False, True]
precision = precision_score(Ya, Y)
recall = recall_score(Ya, Y)
f1 = f1_score(Ya, Y)
print("Precision: ", precision)
print("Recall: ", recall)
print("F1 Score: ", round(f1, 2))
Please note that here Ya is the actual or expected output. And Y is the predicted or calculated output. The output of the above program will be:
Precision: 0.8 Recall: 0.8 F1 Score: 0.8








































0 Comments