Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions imblearn/metrics/_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,16 +671,40 @@ class is unrecognized by the classifier, G-mean resolves to zero. To
array([0.866..., 0. , 0. ])
"""
if average is None or average != "multiclass":
sen, spe, _ = sensitivity_specificity_support(
# Check if we are doing binary classification
# Safe check: infer from data first
unique_y = np.unique(np.hstack((y_true, y_pred)))
is_binary = len(unique_y) <= 2

# If labels are explicitly provided and indicate multiclass (>2), respect that
if labels is not None and len(labels) > 2:
is_binary = False

# LOGIC:
# If Binary + Macro/Weighted: Use your FIX (calculate per-class first)
# If Multiclass: Keep OLD behavior (calculate average first) to pass existing tests
calc_average = average
if is_binary and average in ["macro", "weighted"]:
calc_average = None

sen, spe, sup = sensitivity_specificity_support(
y_true,
y_pred,
labels=labels,
pos_label=pos_label,
average=average,
average=calc_average,
warn_for=("specificity", "specificity"),
sample_weight=sample_weight,
)

# If we forced None (Binary fix), calculate mean of G-means
if calc_average is None and average in ["macro", "weighted"]:
gmean_per_class = np.sqrt(sen * spe)
if average == "macro":
return np.mean(gmean_per_class)
else: # weighted
return np.average(gmean_per_class, weights=sup)

return np.sqrt(sen * spe)
else:
present_labels = unique_labels(y_true, y_pred)
Expand Down
19 changes: 19 additions & 0 deletions imblearn/metrics/tests/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,22 @@ def test_macro_averaged_mean_absolute_error_sample_weight():
)

assert ma_mae_unit_weights == pytest.approx(ma_mae_no_weights)

def test_geometric_mean_score_binary_macro_vs_per_class():
"""Check that binary + macro average matches mean of per-class scores.

Regression test for issue where binary macro was computed as
sqrt(mean_tpr * mean_tnr) instead of mean(sqrt(tpr*tnr)).
"""
y_true = [0, 0, 1, 0, 1, 1]
y_pred = [0, 0, 0, 0, 0, 1]

# Calculate per-class scores manually
per_class_scores = geometric_mean_score(y_true, y_pred, average=None)
expected_macro = np.mean(per_class_scores)

# Calculate the library's macro score
library_macro = geometric_mean_score(y_true, y_pred, average="macro")

# They should be equal
assert library_macro == pytest.approx(expected_macro)