Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
15 changes: 13 additions & 2 deletions imblearn/metrics/_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,16 +671,27 @@ 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(
# LOGIC:
# For macro and weighted, calculate per-class first, then average
calc_average = None if average in ["macro", "weighted"] else average

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 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
29 changes: 24 additions & 5 deletions imblearn/metrics/tests/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ def test_geometric_mean_multiclass(y_true, y_pred, correction, expected_gmean):
@pytest.mark.parametrize(
"y_true, y_pred, average, expected_gmean",
[
([0, 1, 2, 0, 1, 2], [0, 2, 1, 0, 0, 1], "macro", 0.471),
([0, 1, 2, 0, 1, 2], [0, 2, 1, 0, 0, 1], "macro", 0.289),
([0, 1, 2, 0, 1, 2], [0, 2, 1, 0, 0, 1], "micro", 0.471),
([0, 1, 2, 0, 1, 2], [0, 2, 1, 0, 0, 1], "weighted", 0.471),
([0, 1, 2, 0, 1, 2], [0, 2, 1, 0, 0, 1], "weighted", 0.289),
([0, 1, 2, 0, 1, 2], [0, 2, 1, 0, 0, 1], None, [0.8660254, 0.0, 0.0]),
],
)
Expand All @@ -255,7 +255,7 @@ def test_geometric_mean_average(y_true, y_pred, average, expected_gmean):
[0, 1, 1, 0, 0, 1],
[1, 2, 1, 1, 2, 1],
"weighted",
0.333,
0.236,
),
],
)
Expand All @@ -277,8 +277,8 @@ def test_geometric_mean_sample_weight(
[
("multiclass", 0.41),
(None, [0.85, 0.29, 0.7]),
("macro", 0.68),
("weighted", 0.65),
("macro", 0.61),
("weighted", 0.58),
],
)
def test_geometric_mean_score_prediction(average, expected_gmean):
Expand Down 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)