fix: normalize category labels in functional nominal metrics for non-zero-based labels - #3454
Open
xiaoyaoqilan wants to merge 6 commits into
Open
Conversation
xiaoyaoqilan
requested review from
SkafteNicki and
justusschock
as code owners
August 13, 2026 09:32
for more information, see https://pre-commit.ci
4 tasks
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.
Problem
Functional nominal metrics (cramers_v, pearsons_contingency_coefficient, theils_u, tschuprows_t) crash when category labels are not zero-based and contiguous:
Nominal association is invariant to renaming categories — labels [0,1] and [1,2] describe the same two categories and should produce the same result.
Root Cause
num_classes is inferred as the count of unique values (2), but the confusion-matrix updater uses raw label values as bin indices. Label 2 creates a bin outside the expected 2x2 range.
Fix
Added
_normalize_categorical_labelsto nominal/utils.py which maps observed labels to contiguous 0-based IDs when they aren't already. Called from all 4 update functions after NaN handling:Stateful metrics with explicit num_classes are unaffected (they already use stable encoding).
Verification
Fixes #3446