Skip to content

fix: introduce explicit conversion to scalar to avoid implicit conversion issues after numpy 2.0.0#475

Open
marat-davudov wants to merge 1 commit into
interpretml:mainfrom
marat-davudov:fix/numpy-2-scalar-conversion
Open

fix: introduce explicit conversion to scalar to avoid implicit conversion issues after numpy 2.0.0#475
marat-davudov wants to merge 1 commit into
interpretml:mainfrom
marat-davudov:fix/numpy-2-scalar-conversion

Conversation

@marat-davudov
Copy link
Copy Markdown

@marat-davudov marat-davudov commented May 21, 2026

Fix: NumPy 2.0.0 Compatibility - Explicit Scalar Conversion. Issue #474

Summary

This PR fixes compatibility issues with NumPy 2.0.0 by adding explicit scalar conversions before type casting operations.

Problem

Starting with NumPy 2.0.0, implicit conversion of NumPy scalar types to Python built-in types has been deprecated and raises errors. Operations like int(np.array([1])[0]) or int(np.argmax(...)) no longer work automatically.

Error example:

# Before NumPy 2.0.0 (works)
result = np.argmax([0.2, 0.8])
value = int(result)  # OK

# After NumPy 2.0.0 (fails)
result = np.argmax([0.2, 0.8])
value = int(result)  # TypeError

Solution

Use the .item() method to explicitly convert NumPy scalars to Python scalars before type conversion:

result = np.argmax([0.2, 0.8])
if hasattr(result, "item"):
    result = result.item()
value = int(result)  # Now works with NumPy 2.0.0+

Changes Made

Fixed 5 locations across 2 files where NumPy scalars were implicitly converted:

dice_ml/explainer_interfaces/explainer_base.py

  1. Line ~694: np.argmax(original_pred) → add .item() before int() conversion
  2. Line ~672: Array indexing self.target_cf_class[0][0] → add .item() before int() conversion
  3. Line ~769: Array indexing in is_cf_valid() → add .item() before int() conversion

dice_ml/explainer_interfaces/dice_genetic.py

  1. Line ~461: Array indexing tup[0] from population_fitness → add .item() before int() conversion
  2. Line ~471: Array indexing tup[0] from population_fitness → add .item() before int() conversion

All changes use defensive programming with hasattr() checks to maintain backward compatibility with older NumPy versions.

Testing

  • Changes maintain backward compatibility with NumPy < 2.0.0
  • No functional changes to the algorithm logic
  • Only affects type conversion operations

Related

This addresses the NumPy 2.0 migration guide recommendations:
https://numpy.org/devdocs/numpy_2_0_migration_guide.html#changes-to-numpy-data-type-promotion

@marat-davudov marat-davudov force-pushed the fix/numpy-2-scalar-conversion branch from 89ffedb to 311c897 Compare May 21, 2026 21:02
@marat-davudov marat-davudov force-pushed the fix/numpy-2-scalar-conversion branch from 311c897 to 09307b1 Compare May 21, 2026 21:10
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