Spaces:
Build error
Build error
# After review with respect to equations | |
def hierarchical_precision_recall_fmeasure( | |
true_labels, predicted_labels, ancestors, beta=1.0 | |
): | |
# Initialize counters for true positives, predicted, and true conditions | |
true_positive_sum = predicted_sum = true_sum = 0 | |
# Process each instance | |
for true, predicted in zip(true_labels, predicted_labels): | |
# Extend the sets with ancestors | |
extended_true = true.union( | |
*[ancestors[label] for label in true if label in ancestors] | |
) | |
extended_predicted = predicted.union( | |
*[ancestors[label] for label in predicted if label in ancestors] | |
) | |
# Update counters | |
true_positive_sum += len(extended_true.intersection(extended_predicted)) | |
predicted_sum += len(extended_predicted) | |
true_sum += len(extended_true) | |
# Calculate hierarchical precision and recall | |
hP = true_positive_sum / predicted_sum if predicted_sum else 0 | |
hR = true_positive_sum / true_sum if true_sum else 0 | |
# Calculate hierarchical F-measure | |
hF = ((beta**2 + 1) * hP * hR) / (beta**2 * hP + hR) if (hP + hR) else 0 | |
return hP, hR, hF | |
# Example usage: | |
true_labels = [{"G"}] # The true class for the instance | |
predicted_labels = [{"F"}] # The predicted class for the instance | |
ancestors = { # The ancestors for each class, excluding the root | |
"G": {"B", "C", "E"}, | |
"F": {"C"}, | |
} | |
# Calculate hierarchical measures | |
hP, hR, hF = hierarchical_precision_recall_fmeasure( | |
true_labels, predicted_labels, ancestors | |
) | |
print(f"Hierarchical Precision (hP): {hP}") | |
print(f"Hierarchical Recall (hR): {hR}") | |
print(f"Hierarchical F-measure (hF): {hF}") | |