fix: handle mixed str/int label types in calculate_matrix#1840
Open
Aftabbs wants to merge 1 commit intoevidentlyai:mainfrom
Open
fix: handle mixed str/int label types in calculate_matrix#1840Aftabbs wants to merge 1 commit intoevidentlyai:mainfrom
Aftabbs wants to merge 1 commit intoevidentlyai:mainfrom
Conversation
When a DataFrame with dtype="string" contains numeric-looking label
values (e.g. "101", "102"), newer NumPy can coerce those strings to
integers during np.union1d / np.unique, producing a labels list that
mixes Python str and int. Python 3 does not support '<' between str
and int, so sorted(labels) raises TypeError inside calculate_matrix.
The fix catches the TypeError and falls back to:
- Converting all labels to str for consistent sorting.
- Casting the target and prediction Series to str so that
sklearn.metrics.confusion_matrix receives a homogeneous label set
(sklearn calls np.sort internally, which also fails on mixed types).
This restores the expected behaviour for string-typed columns with
numeric-looking label names without changing the code path for
all-string or all-integer label sets.
Adds five tests to CalculateMatrixMixedTypeLabelsTest:
- all-string labels: regression guard
- all-integer labels: regression guard
- mixed str/int labels do not raise TypeError
- confusion matrix has the correct shape for mixed labels
- end-to-end test reproducing the exact scenario from issue evidentlyai#1085
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.
Problem
Closes #1085.
When a
DataFramewithdtype="string"contains numeric-looking label values (e.g."101","102"), newer NumPy can coerce those strings to integers duringnp.union1d/np.unique, producing alabelslist that mixes Pythonstrandint. Python 3 does not support<betweenstrandint, sosorted(labels)raisesTypeErrorinsidecalculate_matrix:Reproduction (from the issue):
Root cause
Newer NumPy (≥ 2.0) uses hash-based deduplication in
np.unique, which can process object arrays with mixed types without raising an error. The resulting Python list from.tolist()then contains bothstrandintobjects.sorted()on that list fails in Python 3.Additionally,
sklearn.metrics.confusion_matrixalso callsnp.sorton the labels internally, so simply usingkey=strin our sort is not sufficient — the internal numpy sort would still fail on a mixed-type array.Fix
Catch the
TypeErrorand fall back to:strfor a consistent, sortable representation.targetandpredictionSeries tostrso thatsklearn.metrics.confusion_matrixreceives a homogeneous label set.The homogeneous (
all-strorall-int) paths are unchanged.Tests
Five new tests added to
CalculateMatrixMixedTypeLabelsTestintests/calculations/test_classification_performance.py:test_all_string_labels_return_correct_matrixtest_all_integer_labels_return_correct_matrixtest_mixed_str_int_labels_do_not_raiseTypeErrortest_mixed_str_int_labels_confusion_matrix_shapetest_string_dtype_dataframe_end_to_endAll 9 tests in the file pass.