Reputation: 4776
I have two matrices
a = np.matrix([[1,2], [3,4]])
b = np.matrix([[5,6], [7,8]])
and I want to get the element-wise product, [[1*5,2*6], [3*7,4*8]]
, which equals
matrix([[5, 12], [21, 32]])
I have tried np.dot(a,b)
and a*b
but both give the result matrix([[19, 22], [43, 50]])
which is the matrix product, not the element-wise product. How can I get the the element-wise product (aka Hadamard product) using built-in functions?
Upvotes: 185
Views: 512612
Reputation: 23429
For ndarrays, *
is elementwise multiplication (Hadamard product) while for numpy matrix objects, it is wrapper for np.dot
(source code).
As the accepted answer mentions, np.multiply
always returns an elementwise multiplication. Notably, it preserves the type of the object, if a matrix object is passed, the returned object will be matrix; if ndarrays are passed, an ndarray is returned.
If you have a np.matrix
object, then you can convert it into an ndarray (via .A
attribute) and use *
for elementwise multiplication. However, note that unlike np.multiply
which preserves the matrix
type, it returns an ndarray
(because we converted to ndarray before multiplication).
a = np.matrix([[1, 2], [3, 4]])
b = np.matrix([[5, 6], [7, 8]])
c = a.A * b.A
# array([[ 5, 12],
# [21, 32]])
Then again, matrix
is not recommended by the library itself and once it's removed from numpy, this answer (and arguably the question as well) will probably be obsolete.
Upvotes: 1
Reputation: 510
import numpy as np
x = np.array([[1,2,3], [4,5,6]])
y = np.array([[-1, 2, 0], [-2, 5, 1]])
x*y
Out:
array([[-1, 4, 0],
[-8, 25, 6]])
%timeit x*y
1000000 loops, best of 3: 421 ns per loop
np.multiply(x,y)
Out:
array([[-1, 4, 0],
[-8, 25, 6]])
%timeit np.multiply(x, y)
1000000 loops, best of 3: 457 ns per loop
Both np.multiply
and *
would yield element wise multiplication known as the Hadamard Product
%timeit
is ipython magic
Upvotes: 19
Reputation: 16081
For elementwise multiplication of matrix
objects, you can use numpy.multiply
:
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
np.multiply(a,b)
Result
array([[ 5, 12],
[21, 32]])
However, you should really use array
instead of matrix
. matrix
objects have all sorts of horrible incompatibilities with regular ndarrays. With ndarrays, you can just use *
for elementwise multiplication:
a * b
If you're on Python 3.5+, you don't even lose the ability to perform matrix multiplication with an operator, because @
does matrix multiplication now:
a @ b # matrix multiplication
Upvotes: 260
Reputation: 2085
Try this:
a = np.matrix([[1,2], [3,4]])
b = np.matrix([[5,6], [7,8]])
#This would result a 'numpy.ndarray'
result = np.array(a) * np.array(b)
Here, np.array(a)
returns a 2D array of type ndarray
and multiplication of two ndarray
would result element wise multiplication. So the result would be:
result = [[5, 12], [21, 32]]
If you wanna get a matrix, the do it with this:
result = np.mat(result)
Upvotes: 1
Reputation: 3084
just do this:
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
a * b
Upvotes: 39