fix: sort labels by str() key to handle mixed int/str label types#1872
Open
YousefZahran1 wants to merge 1 commit into
Open
fix: sort labels by str() key to handle mixed int/str label types#1872YousefZahran1 wants to merge 1 commit into
YousefZahran1 wants to merge 1 commit into
Conversation
When classification labels contain values like '101' or '102', the internal label_key_valudator coerces them to int while other labels (e.g. 'foo', 'bar') remain as str. Calling sorted() on this mixed-type list raises TypeError in Python 3 because int and str are not orderable. Fix: use sorted(labels, key=str) in calculate_matrix so that labels are always sorted by their string representation, making the sort stable regardless of whether individual labels happen to be int or str. Fixes evidentlyai#1085
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
ClassificationQualityMetricandClassificationConfusionMatrixcrash withTypeError: '<' not supported between instances of 'str' and 'int'when classification labels include strings that look like integers (e.g.'101','102').Why
Evidently's internal
label_key_valudatorcoerces any label value that can be cast toint— including string labels like'101'— into an actualint. When other labels remain asstr(e.g.'foo','bar'), thelabelslist contains mixed types. Python 3'ssorted()cannot compareintandstr, socalculate_matrixcrashes on line 324.Reproduce
Fix
Use
sorted(labels, key=str)instead ofsorted(labels)incalculate_matrix. This sorts labels by their string representation, avoiding the type comparison entirely while preserving a consistent, deterministic order. No API or behaviour change for datasets with homogeneous label types.Testing
Fixes #1085