173

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]], equaling

[[5,12], [21,32]]

I have tried

print(np.dot(a,b)) 

and

print(a*b)

but both give the result

[[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?

cameronroytaylor
  • 1,578
  • 1
  • 17
  • 18
Malintha
  • 4,512
  • 9
  • 48
  • 82
  • 7
    Are you sure `a` and `b` aren't NumPy's matrix type? With this class, `*` returns the inner product, not element-wise. But for the usual `ndarray` class, `*` means element-wise product. – bnaecker Oct 14 '16 at 04:42
  • are `a` and `b` numpy arrays? Also, in your question above, you are using `x` and `y` for computation instead of `a` and `b`. Is that just a typo? – jtitusj Oct 14 '16 at 04:50
  • a and b are numpy matrix type elements – Malintha Oct 14 '16 at 04:51
  • 9
    Always use numpy arrays, and not numpy matrices. See [what the numpy docs say](http://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html#array-or-matrix-which-should-i-use) about this. Also note that from python 3.5+, you can use [`@` for matrix multiplication](https://www.python.org/dev/peps/pep-0465/) with numpy arrays, which means there should be absolutely no good reason to use matrices over arrays. – Praveen Oct 14 '16 at 05:03
  • 3
    To be picky, `a` and `b` are lists. They will work in `np.dot`; but not in `a*b`. If you use `np.array(a)` or `np.matrix(a)`, `*` works but with different results. – hpaulj Oct 14 '16 at 05:31
  • If you use `np.array(a)` and `np.array(b)`. Then, simple `a*b` works. Note that if the numbers are large, you should use `np.array(a, dtype=np.int64)` to have right outputs. – Ehsan Tabatabaei Dec 26 '20 at 08:47

4 Answers4

248

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
user2357112
  • 260,549
  • 28
  • 431
  • 505
Rahul K P
  • 15,740
  • 4
  • 35
  • 52
  • 32
    Just to add a little context: in Algebra, this operation is known as the **Hadamard Product**, and it is different from the more common matrix product. https://en.wikipedia.org/wiki/Hadamard_product_(matrices) – FaCoffee Feb 22 '17 at 12:39
40

just do this:

import numpy as np

a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])

a * b
jtitusj
  • 3,046
  • 3
  • 24
  • 40
18
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

Chiel
  • 6,006
  • 2
  • 32
  • 57
4rshdeep
  • 490
  • 4
  • 11
1

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)
amrezzd
  • 1,787
  • 15
  • 38