Add hierarchical precision, recall, and F-measure calculation function
Browse files
ham.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# After review with respect to equations
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def hierarchical_precision_recall_fmeasure(
|
| 5 |
+
true_labels, predicted_labels, ancestors, beta=1.0
|
| 6 |
+
):
|
| 7 |
+
# Initialize counters for true positives, predicted, and true conditions
|
| 8 |
+
true_positive_sum = predicted_sum = true_sum = 0
|
| 9 |
+
|
| 10 |
+
# Process each instance
|
| 11 |
+
for true, predicted in zip(true_labels, predicted_labels):
|
| 12 |
+
# Extend the sets with ancestors
|
| 13 |
+
extended_true = true.union(
|
| 14 |
+
*[ancestors[label] for label in true if label in ancestors]
|
| 15 |
+
)
|
| 16 |
+
extended_predicted = predicted.union(
|
| 17 |
+
*[ancestors[label] for label in predicted if label in ancestors]
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# Update counters
|
| 21 |
+
true_positive_sum += len(extended_true.intersection(extended_predicted))
|
| 22 |
+
predicted_sum += len(extended_predicted)
|
| 23 |
+
true_sum += len(extended_true)
|
| 24 |
+
|
| 25 |
+
# Calculate hierarchical precision and recall
|
| 26 |
+
hP = true_positive_sum / predicted_sum if predicted_sum else 0
|
| 27 |
+
hR = true_positive_sum / true_sum if true_sum else 0
|
| 28 |
+
|
| 29 |
+
# Calculate hierarchical F-measure
|
| 30 |
+
hF = ((beta**2 + 1) * hP * hR) / (beta**2 * hP + hR) if (hP + hR) else 0
|
| 31 |
+
|
| 32 |
+
return hP, hR, hF
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# Example usage:
|
| 36 |
+
true_labels = [{"G"}] # The true class for the instance
|
| 37 |
+
predicted_labels = [{"F"}] # The predicted class for the instance
|
| 38 |
+
ancestors = { # The ancestors for each class, excluding the root
|
| 39 |
+
"G": {"B", "C", "E"},
|
| 40 |
+
"F": {"C"},
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
# Calculate hierarchical measures
|
| 44 |
+
hP, hR, hF = hierarchical_precision_recall_fmeasure(
|
| 45 |
+
true_labels, predicted_labels, ancestors
|
| 46 |
+
)
|
| 47 |
+
print(f"Hierarchical Precision (hP): {hP}")
|
| 48 |
+
print(f"Hierarchical Recall (hR): {hR}")
|
| 49 |
+
print(f"Hierarchical F-measure (hF): {hF}")
|