fix: return 0 for MCC when denominator is zero (align with sklearn)#3371
Open
jashshah999 wants to merge 1 commit intoLightning-AI:masterfrom
Open
fix: return 0 for MCC when denominator is zero (align with sklearn)#3371jashshah999 wants to merge 1 commit intoLightning-AI:masterfrom
jashshah999 wants to merge 1 commit intoLightning-AI:masterfrom
Conversation
When any row or column of the confusion matrix sums to zero, the MCC denominator is zero and the metric is mathematically undefined. The previous code had complex special-casing with eps perturbations that produced values like 1.0 for all-TN cases, inconsistent with sklearn. Replace with the sklearn convention: return 0 when denominator is zero. This is simpler, consistent with the reference implementation, and avoids producing misleading non-zero values for degenerate cases. Fixes Lightning-AI#3355.
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.
What does this PR do?
When any row or column of the confusion matrix sums to zero, the MCC denominator is zero and the metric is undefined. The previous code had complex special-casing with eps perturbations that produced inconsistent results (e.g. 1.0 when all predictions and targets are 0, while sklearn returns 0.0).
Replace with the sklearn convention: return 0 when denominator is zero.
Before (inconsistent with sklearn)
After (matches sklearn)
The fix removes ~25 lines of complex special-casing and replaces with a 1-line return.
Fixes #3355.
📚 Documentation preview 📚: https://torchmetrics--3371.org.readthedocs.build/en/3371/