| def calculate_precision_recall(df, gt_col, pred_col): | |
| true_positives = 0 | |
| false_positives = 0 | |
| false_negatives = 0 | |
| for index, row in df.iterrows(): | |
| gt_value = row[gt_col] | |
| pred_value = row[pred_col] | |
| if gt_value and pred_value: | |
| true_positives += 1 | |
| elif not gt_value and pred_value: | |
| false_positives += 1 | |
| elif gt_value and not pred_value: | |
| false_negatives += 1 | |
| precision = true_positives / (true_positives + false_positives) if (true_positives + false_positives) > 0 else 0 | |
| recall = true_positives / (true_positives + false_negatives) if (true_positives + false_negatives) > 0 else 0 | |
| return precision, recall | |