Difference between Numpy Matrices and Numpy Arrays in Python

The main difference between Numpy matrices and Numpy arrays is that Numpy matrices are strictly two-dimensional, while numpy arrays (ndarrays) are N-dimensional. Matrix objects are the subclass of the ndarray, so they inherit all the attributes and methods of ndarrays.

The main advantage of numpy matrices is that they provide a convenient notation for matrix multiplication: if x and y are matrices, then x*y is their matrix product.

On the other hand, as of Python 3.5, Numpy supports infix matrix multiplication using the @ operator so that you can achieve the same convenience of the matrix multiplication with ndarrays in Python >= 3.5.

Both matrix objects and ndarrays have .T to return the transpose, but the matrix objects also have .H for the conjugate transpose and I for the inverse.

In contrast, numpy arrays consistently rule that operations are applied element-wise (except for the new @ operator). Thus, if x and y are numpy arrays, then x*y is the array formed by multiplying the components element-wise.

I hope your doubt about the Numpy array and Numpy Matrix will be clear.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.