Skip to content

fix: squeeze regressor scores before scalar assignment for numpy 2.x compat - #476

Open
SudipSinha wants to merge 1 commit into
interpretml:mainfrom
SudipSinha:fix/numpy2-regressor-array-scalar-assignment
Open

fix: squeeze regressor scores before scalar assignment for numpy 2.x compat#476
SudipSinha wants to merge 1 commit into
interpretml:mainfrom
SudipSinha:fix/numpy2-regressor-array-scalar-assignment

Conversation

@SudipSinha

Copy link
Copy Markdown

Summary

In numpy 2.x, PyTorch model output for single-output regressors has shape (n, 1) rather than (n,), so model_scores[i] is an array of shape (1,) rather than a scalar. Assigning a sequence to a scalar element of a numpy array raises ValueError in numpy 2.x:

ValueError: setting an array element with a sequence.
TypeError: only 0-dimensional arrays can be converted to Python scalars

Fix: np.squeeze collapses 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 from predict.

File: dice_ml/explainer_interfaces/explainer_base.py:813

# before
model_output[i] = model_scores[i]

# after
model_output[i] = float(np.squeeze(model_scores[i]))

Impact

Fixes 14 failing regression tests across dice_random, dice_KD, and dice_genetic with the PyTorch backend. No change in numeric output — np.squeeze + float extracts the identical scalar value; the model_output array dtype (float32) still governs precision.

Test plan

  • All 4 TestDiceGeneticRegressionMethods PYT tests now pass (were ValueError)
  • Full suite: 267 passed, 13 failed (down from 253 passed, 27 failed) — remaining 13 failures are a separate pre-existing pd.read_json issue unrelated to this change
  • No regressions in classifier path or sklearn regressor path

Tested on Python 3.12, 3.13, 3.14 with numpy 2.5.3.

…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
@SudipSinha

Copy link
Copy Markdown
Author

Related PRs for context:

@SudipSinha

Copy link
Copy Markdown
Author

Fixes #474 (regressor path of the numpy 2.x bug).

@SudipSinha

Copy link
Copy Markdown
Author

Hi @amit-sharma and @gaugup, can you have a look at this one-line change PR? TY!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant