Weighted Averages of Precision, Recall and F-Score | Model Evaluation Metrics
So if you have two classes, the weighted precision would be:
Where pc1 and pc2 are the precisions for class 1 and class 2, and |𝑐1| and |𝑐2| are the number of instances in class 1 and class 2.
For example, we have two classes 0 and 1
from sklearn.metrics import classification_report
print(classification_report(y_test,y_pred))
Calculate Weighted Recall, Precision and F1 score:
1. Weighted Recall:
weighted_avg_recall = recall _0 * support_0 + recall _1 * support_1 / total support
2. Weighted Precision:
weighted_avg_precision = precision_0 * support_0 + precision_1 * support_1 / total support
3. Weighted F1 score:
weighted_avg_f1_score = f1_score_0 * support_0 + f1_score_1 * support_1 / total support
INPUT:
weighted_avg_precision = ((0.86 * 63) + (0.93 * 37) ) / 100
print("weighted_avg_precision: ",weighted_avg_precision)
weighted_avg_f1_score = ((0.91 * 63) + (0.82 * 37) ) / 100
print("weighted_avg_f1_score: ", weighted_avg_f1_score)
OUTPUT:
weighted_avg_precision: 0.8859weighted_avg_f1_score: 0.8767
Here support is the number of instances in a particular class
You can use Scikit learn methods to get those:
f1_score() method to get f1 score
INPUT:
print("weighted avg f1 score using f1_score method: ", f1_score(y_test,y_pred, average="weighted"))
OUTPUT:
weighted avg f1 score using f1_score method: 0.8763093622795115