fix: squeeze regressor scores before scalar assignment for numpy 2.x compat - #476
Open
SudipSinha wants to merge 1 commit into
Open
Conversation
…compat In numpy 2.x, PyTorch model output for regressors has shape (n, 1) rather than (n,), so model_scores[i] is an array of shape (1,) not a scalar. Assigning a sequence to a scalar element of a numpy array raises ValueError in numpy 2.x. np.squeeze collapses the size-1 dimension to a 0-d array; float() converts it to a Python scalar before the assignment. This is a no-op for backends that already return a scalar (sklearn). Fixes 14 failing regression tests across dice_random, dice_KD, and dice_genetic with PyTorch backend. Signed-off-by: Sudip Sinha <sudip.sinha@redhat.com> Signed-off-by: Sudip Sinha <Sudip.Sinha@RedHat.com> rh-pre-commit.version: 2.3.2 rh-pre-commit.check-secrets: ENABLED
Author
|
Related PRs for context:
|
This was referenced Sep 9, 2026
Author
|
Fixes #474 (regressor path of the numpy 2.x bug). |
2 tasks
Author
|
Hi @amit-sharma and @gaugup, can you have a look at this one-line change PR? TY! |
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.
Summary
In numpy 2.x, PyTorch model output for single-output regressors has shape
(n, 1)rather than(n,), somodel_scores[i]is an array of shape(1,)rather than a scalar. Assigning a sequence to a scalar element of a numpy array raisesValueErrorin numpy 2.x:Fix:
np.squeezecollapses the size-1 dimension;float()converts the resulting 0-d array to a Python scalar before the assignment. This is a no-op for backends (e.g. sklearn) that already return a scalar frompredict.File:
dice_ml/explainer_interfaces/explainer_base.py:813Impact
Fixes 14 failing regression tests across
dice_random,dice_KD, anddice_geneticwith the PyTorch backend. No change in numeric output —np.squeeze+floatextracts the identical scalar value; themodel_outputarray dtype (float32) still governs precision.Test plan
TestDiceGeneticRegressionMethodsPYT tests now pass (wereValueError)pd.read_jsonissue unrelated to this changeTested on Python 3.12, 3.13, 3.14 with numpy 2.5.3.