For linear algebra, using matrices might be more straightforward. The matrix object in NumPy inherits all the attributes and methods from ndarray
, but it's strictly two-dimensional, while ndarray
can be multi-dimensional. The well-known advantage of using NumPy matrices is that they provide matrix multiplication as the *
notation; for example, if x
and y
are matrices, x * y
is their matrix product. However, starting from Python 3.5/NumPy 1.10, native matrix multiplication is supported with the new operator "
However, starting from Python 3.5/NumPy 1.10, native matrix multiplication is supported with the new operator "@
". So that is one more good reason to use ndarray
(
https://docs.python.org/3/whatsnew/3.5.html#whatsnew-pep-465
).
However, matrix objects still provide convenient conversion such as inverse and conjugate transpose while an ndarray
does not. Let's start by creating NumPy matrices:
In [1]: import numpy as np In [2]: ndArray = np.arange(9).reshape(3,3) ...