np.matrix still present in NumPy 2.x despite deprecation: safe to keep using it?
0 reputation · 07 Sept 2024, 10:05 UTC
0 reputation · 07 Sept 2024, 10:05 UTC
NumPy's np.matrix class, a subclass of ndarray that overloads * to mean matrix multiplication instead of element-wise multiplication, has carried a deprecation warning since NumPy 1.19. Despite that, it continues to ship in the 2.x series, and no firm removal timeline appears to have been settled.
I maintain a library that inherited a large amount of linear algebra code written around np.matrix, including expressions that mix it with plain ndarray objects. Because the * operator behaves differently for the two types, a future removal would not just break imports; it would force a careful audit of every multiplication to avoid silently changing results. I am trying to decide between migrating everything to ndarray plus @ now, or waiting for an official removal announcement.
Assume NumPy 2.x as the current baseline.
Has the NumPy project published a target release for removing np.matrix, or is the removal decision still open? Is there a documented, supported migration path for code that mixes np.matrix and ndarray in the same expressions? Short of removal, are there known behavioral differences in 2.x compared with 1.x that existing np.matrix users should verify against?
33275 reputation · 07 Sept 2024, 15:22 UTC
No removal target for np.matrix has been published. It still ships in NumPy 2.x, and the removal decision remains open. You can keep using it now, but the supported path forward is migrating to ndarray plus the @ operator.
Confirmed: The class is present in the 2.x series. The deprecation warning introduced around NumPy 1.19 has not been converted into a scheduled removal.
Likely explanation: The project appears to be weighing ecosystem compatibility rather than treating this as a routine cleanup. Because np.matrix is an ndarray subclass, a hard removal would ripple through libraries that expose it in public signatures.
There is no documented automated migration tool. The supported approach is manual and expression-level:
np.matrix instances to plain ndarray.* with @.np.matrix and ndarray operands meet, because * dispatches to matrix multiplication only when the left operand is a matrix.The subclass relationship creates a specific hazard: many NumPy functions return plain ndarray when given a matrix, so an expression can silently shift from matrix semantics to element-wise semantics without an obvious type change at the call site.
NumPy 2.0 changed scalar promotion and copy semantics. For existing np.matrix code, verify these against your 1.x baseline:
python -W error::PendingDeprecationWarning -m pytest
PendingDeprecationWarning to errors to inventory all construction and usage sites.ndarray plus @, then compare numerical outputs and shapes against the original..A, .A1, .H, .I—that vanish after conversion.Release plans can shift, so re-confirm the current np.matrix status against the latest NumPy release notes before locking your timeline.
Does your library return np.matrix to downstream consumers, or is it internal? If it is public API, an internal-only migration leaves consumers exposed to the same future risk and argues for a coordinated deprecation cycle. If it is internal, you can migrate module-by-module without a breaking release.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.