I know I can do matrix multiplication using numpy arrays by using the .dot
syntax. The regular *
multiplication does element-wise multiplication.
a = np.array([[1,2],[3,4]])
print 'matrix multiplication', a.dot(a)
print 'element-wise multiplication', a * a
> matrix multiplication [[ 7 10] [15 22]]
> element-wise multiplication [[ 1 4] [ 9 16]]
This works fine, but it's the opposite of all matrix operations I've ever learnt (i.e. the "dot product" is typically element-wise, and the regular product is typically a full matrix multiplication.)
So I'm investigating np.matrix
. The nice thing is that matrix multiplication uses the *
operator, but I'm to understand how to do element-wise multiplication.
m = np.matrix(a)
print 'matrix multiplication', m * m
print 'more matrix multiplication? ', m.dot(m)
> matrix multiplication [[ 7 10] [15 22]]
> more matrix multiplication? [[ 7 10] [15 22]]
I understand what's happening - there is not .dot
operator for numpy matrix, so it falls through to the base np.array
implementation. But does this mean that there's no way to calculate a dot product using np.matrix
?
Is this just another argument for avoiding np.matrix
and instead sticking with np.array
?