What are the advantages and disadvantages of each?
From what I've seen, either one can work as a replacement for the other if need be, so should I bother using both or should I stick to just one of them?
Will the style of the program influence my choice? I am doing some machine learning using numpy, so there are indeed lots of matrices, but also lots of vectors (arrays).
Numpy matrices are strictly 2-dimensional, while numpy arrays (ndarrays) are N-dimensional. Matrix objects are a subclass of 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 a and b are matrices, then a*b is their matrix product.
On the other hand, as of Python 3.5, NumPy supports infix matrix multiplication using the
@
operator, so you can achieve the same convenience of matrix multiplication with ndarrays in Python >= 3.5.Both matrix objects and ndarrays have
.T
to return the transpose, but matrix objects also have.H
for the conjugate transpose, and.I
for the inverse.In contrast, numpy arrays consistently abide by the rule that operations are applied element-wise (except for the new
@
operator). Thus, ifa
andb
are numpy arrays, thena*b
is the array formed by multiplying the components element-wise:To obtain the result of matrix multiplication, you use
np.dot
(or@
in Python >= 3.5, as shown above):The
**
operator also behaves differently:Since
a
is a matrix,a**2
returns the matrix producta*a
. Sincec
is an ndarray,c**2
returns an ndarray with each component squared element-wise.There are other technical differences between matrix objects and ndarrays (having to do with np.ravel, item selection and sequence behavior).
The main advantage of numpy arrays is that they are more general than 2-dimensional matrices. What happens when you want a 3-dimensional array? Then you have to use an ndarray, not a matrix object. Thus, learning to use matrix objects is more work -- you have to learn matrix object operations, and ndarray operations.
Writing a program that uses both matrices and arrays makes your life difficult because you have to keep track of what type of object your variables are, lest multiplication return something you don't expect.
In contrast, if you stick solely with ndarrays, then you can do everything matrix objects can do, and more, except with slightly different functions/notation.
If you are willing to give up the visual appeal of NumPy matrix product notation (which can be achieved almost as elegantly with ndarrays in Python >= 3.5), then I think NumPy arrays are definitely the way to go.
PS. Of course, you really don't have to choose one at the expense of the other, since
np.asmatrix
andnp.asarray
allow you to convert one to the other (as long as the array is 2-dimensional).There is a synopsis of the differences between NumPy
arrays
vs NumPymatrix
es here.As others have mentioned, perhaps the main advantage of
matrix
was that it provided a convenient notation for matrix multiplication.However, in Python 3.5 there is finally a dedicated infix operator for matrix multiplication:
@
.With recent NumPy versions, it can be used with
ndarray
s:So nowadays, even more, when in doubt, you should stick to
ndarray
.Just to add one case to unutbu's list.
One of the biggest practical differences for me of numpy ndarrays compared to numpy matrices or matrix languages like matlab, is that the dimension is not preserved in reduce operations. Matrices are always 2d, while the mean of an array, for example, has one dimension less.
For example demean rows of a matrix or array:
with matrix
with array
I also think that mixing arrays and matrices gives rise to many "happy" debugging hours. However, scipy.sparse matrices are always matrices in terms of operators like multiplication.
Scipy.org recommends that you use arrays: