shaare.it

DeprecationWarning: numpy.matrix is deprecated

7 Dec 2025

1 min read

DeprecationWarning: numpy.matrix is deprecated

$ python -c "import numpy as np; np.matrix([[1,2],[3,4]])"
/tmp/ipykernel_12345/....py:1: DeprecationWarning: `np.matrix` is deprecated; use regular ndarrays instead
  np.matrix([[1,2],[3,4]])

Why this happens

NumPy is phasing out np.matrix because its behavior differs from ndarrays (e.g., * does matrix multiplication). Newer code should use ndarray with @ or np.matmul for matrix multiplication.

Fix

Use np.array and the @ operator or np.matmul. Update code to avoid np.matrix creation and conversion quirks.

Wrong code

import numpy as np
M = np.matrix([[1,2],[3,4]])
print(M * M)

Fixed code

import numpy as np
M = np.array([[1,2],[3,4]])
print(M @ M)
# or np.matmul(M, M)