FIX sensitivity_specificity_support: correct specificity with sample_weight (#1180)#1181
Open
immu4989 wants to merge 2 commits into
Open
Conversation
…weight (scikit-learn-contrib#1180) When sample_weight is provided, tp_sum/pred_sum/true_sum are weighted sums but the true-negative count was formed as y_true.size - (pred_sum + true_sum - tp_sum), mixing a raw sample count with weighted sums. This makes tn_sum wrong (it can go negative), so the resulting specificity is incorrect and can exceed 1. specificity_score, geometric_mean_score and classification_report_imbalanced all delegate here, so their weighted results were affected too. Use the total sample weight as the population size when sample_weight is given, falling back to y_true.size otherwise (unweighted path unchanged). The existing test_geometric_mean_sample_weight parametrization asserted the weighted value produced under the buggy behaviour (0.333); it is corrected to the right value (0.609). A dedicated non-regression test is added that checks specificity never exceeds 1 and that integer weights match repeated samples.
…t-learn-contrib#1180) Audit of the metrics module showed the specificity weighting bug propagated through every metric that delegates to sensitivity_specificity_support: specificity_score, geometric_mean_score, and the make_index_balanced_accuracy wrappers were all affected, while sensitivity and macro_averaged_mean_absolute_error were already correct. Add a parametrized test asserting that integer sample_weight equals physically repeating each sample, across all of these metrics and every average mode, plus the rate-bound [0, 1] check. This locks in the fix across the whole delegation chain rather than only the function where the bug originated.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reference Issues/PRs
Fixes #1180.
What does this implement/fix? Explain your changes.
sensitivity_specificity_supportcomputes an incorrect specificity whensample_weightis provided — the value can even exceed1, which is impossible for a rate. Becausespecificity_score,geometric_mean_score,classification_report_imbalancedand themake_index_balanced_accuracywrappers all delegate to it, the bug propagates through the whole metrics module.Root cause. When
sample_weightis given,tp_sum,pred_sumandtrue_sumare weighted sums, but the true-negative count was formed using the raw sample count:Mixing a count (
y_true.size) with weighted sums makestn_sumwrong — it can go negative — and the downstreamspecificity = tn_sum / (tn_sum + pred_sum - tp_sum)is then wrong as well.Fix. Use the total sample weight as the population size when
sample_weightis provided, falling back toy_true.sizeotherwise. The unweighted path is unchanged (every term scales uniformly there, so it was already correct).Scope: a module-wide audit, single root cause
I audited every
sample_weight-aware metric inimblearn.metricsusing the invariant that integersample_weightmust equal physically repeating each sample. The results:sensitivity_specificity_support(sensitivity)sensitivity_specificity_support(specificity)specificity_scoregeometric_mean_scorenan(delegates)make_index_balanced_accuracy(...)wrappersclassification_report_imbalancedmacro_averaged_mean_absolute_errorSo the entire weighted-metric bug class traces to this one line; there are no other independent weighting bugs in the module.
Tests
test_geometric_mean_sample_weightparametrization asserted the weighted value produced under the buggy behaviour (0.333). I corrected it to the right value (0.609), verified by an independent hand-computation of the weighted confusion matrix (per-class specificity[0.667, 0.5], sensitivity[1.0, 0.5]).test_sensitivity_specificity_support_sample_weight(the [BUG] sensitivity_specificity_support returns wrong specificity with sample_weight (can exceed 1) #1180 reproducer; asserts specificity ≤ 1; integer weights == repeated samples).test_metrics_sample_weight_repeat_equivalencethat enforces the repeat-equivalence invariant across the whole delegation chain (sensitivity,specificity,specificity_score,geometric_mean_score, the IBA wrappers) and every average mode (None/macro/weighted/micro), plus a[0, 1]rate-bound check — 28 cases. This locks the fix in across the module, not just where the bug originated.pytest imblearn/metrics/→ 236 passed (scikit-learn 1.9.0).