often when working with numpy I find the distinction annoying - when I pull out a vector or a row from a matrix and then perform operations with np.array
s there are usually problems.
to reduce headaches, I've taken to sometimes just using np.matrix
(converting all np.arrays to np.matrix
) just for simplicity. however, I suspect there are some performance implications. could anyone comment as to what those might be and the reasons why?
it seems like if they are both just arrays underneath the hood that element access is simply an offset calculation to get the value, so I'm not sure without reading through the entire source what the difference might be.
more specifically, what performance implications does this have:
v = np.matrix([1, 2, 3, 4])
# versus the below
w = np.array([1, 2, 3, 4])
thanks
timeit
a try?matrix
class is a subclass of numpy'sndarray
object, implemented entirely in Python. So every call you make on amatrix
object is going to require a few extra Python calls, mostly to make sure that the object always remains 2D. So it may be a little slower than anndarray
, although the differences are very likely negligible