how does multiplication differ for NumPy Matrix vs Array classes?


The numpy docs recommend using array instead of matrix for working with matrices. However, unlike octave (which I was using till recently), * doesn't perform matrix multiplication, you need to use the function matrixmultipy(). I feel this makes the code very unreadable.

Does anybody share my views, and has found a solution?

12/13/2014 7:24:22 AM

The main reason to avoid using the matrix class is that a) it's inherently 2-dimensional, and b) there's additional overhead compared to a "normal" numpy array. If all you're doing is linear algebra, then by all means, feel free to use the matrix class... Personally I find it more trouble than it's worth, though.

For arrays (prior to Python 3.5), use dot instead of matrixmultiply.


import numpy as np
x = np.arange(9).reshape((3,3))
y = np.arange(3)


Or in newer versions of numpy, simply use

Personally, I find it much more readable than the * operator implying matrix multiplication...

For arrays in Python 3.5, use x @ y.

1/25/2015 1:57:33 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow