Skip to content

fix: NumPy 2.4+ compatibility in feature selection fitting paths#11

Open
KenyaOtsuka wants to merge 1 commit into
KamitaniLab:devfrom
KenyaOtsuka:fix/numpy-2-4-compatibility
Open

fix: NumPy 2.4+ compatibility in feature selection fitting paths#11
KenyaOtsuka wants to merge 1 commit into
KamitaniLab:devfrom
KenyaOtsuka:fix/numpy-2-4-compatibility

Conversation

@KenyaOtsuka
Copy link
Copy Markdown
Contributor

Summary

Fix NumPy 2.4+ compatibility issues in the feature-selection fitting paths of src/fastl2lir/fastl2lir.py.

Background

In the feature-selection paths of __sub_fit and __sub_fit_save_select_feat, the right-hand side passed to np.linalg.solve() was reshaped to a column vector using .reshape(-1, 1).

As a result, the solution Wb also became a 2D column vector. Expressions such as Wb[i] therefore returned a length-1 array rather than a scalar. Assigning such a length-1 array to a scalar element of W or b was deprecated in NumPy 1.25 and raises a TypeError in NumPy 2.4+.

Changes

  • In __sub_fit, update both feature-selection branches to pass a 1D RHS to np.linalg.solve().

  • Replace the per-feature assignment loop with a vectorized assignment:

    W[index_outputDim, I[:-1]] = Wb[:-1]
  • In __sub_fit_save_select_feat, apply the same 1D-RHS pattern and replace the assignment loop with:

    W[index_outputDim, I] = Wb[:-1]
  • Replace dtype=np.bool with dtype=np.bool_ for compatibility with NumPy versions where np.bool was removed.

The no-feature-selection multi-target solve path in __sub_fit is left unchanged, since that path intentionally solves a multi-target system.

Tests

I ran the following tests and confirmed that all tests pass, including with NumPy deprecation warnings treated as errors:

uv run pytest                               # 7 passed
uv run pytest -W error::DeprecationWarning   # 7 passed

Previously, the right-hand side passed to `np.linalg.solve()` was
reshaped to a column vector in the feature-selection paths of
`__sub_fit` and `__sub_fit_save_select_feat`. The resulting solution
`Wb` was a 2D column vector, so `Wb[i]` returned a length-1 array
rather than a scalar. Assigning that length-1 array to a scalar
element of `W` or `b` was deprecated in NumPy 1.25 and raises a
`TypeError` in NumPy 2.4+.

Changes:
- `__sub_fit` (both threadpool and fallback branches): pass 1D RHS to
  `np.linalg.solve()`; replace the per-feature loop with a vectorized
  assignment `W[..., I[:-1]] = Wb[:-1]`.
- `__sub_fit_save_select_feat`: same pattern; also replaces the
  `range(n_feat)` loop with `W[..., I] = Wb[:-1]`, which is safe for
  both feature-selection and use-all-features paths.
- Replace `dtype=np.bool` (removed in NumPy 1.24) with `dtype=np.bool_`.

The no-feature-selection multi-target solve path is left unchanged.

Co-authored-by: Claude <claude@anthropic.com>
@KenyaOtsuka KenyaOtsuka requested a review from ganow May 29, 2026 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant