Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does np.multiply work?

I am trying to implement np.multiply in Java and I am confused about what it is actually doing. The documentation simply states it does element-wise multiplication. It does not match any mathematical matrix product I can find. It partially matches the element-wise Hadamard product but doesn't need the same number of rows and columns. Does anyone know what mathematical product np.multiply performs and have any more information into EXACTLY how it is working?

Here are the different outputs I've been getting. These seem like very different functionality.

a = np.array([[1,1,1],[2,2,2],[2,3]])
b = np.array([[2,3,4]])
print(np.multiply(a,b))
#output
[[1, 1, 1, 1, 1, 1] [2, 2, 2, 2, 2, 2, 2, 2, 2] [2, 3, 2, 3, 2, 3, 2, 3]]]

and

a = np.array([[1,1,1],[2,2,2]])
b = np.array([[2,3,4]])
print(np.multiply(a,b))
#output
[[2 3 4]
 [4 6 8]]
like image 617
Hans.Gundlach Avatar asked Dec 14 '15 21:12

Hans.Gundlach


People also ask

What does NP multiply do?

multiply() function is used when we want to compute the multiplication of two array. It returns the product of arr1 and arr2, element-wise.

Is NP multiply same as *?

multiply(x1, x2) array([[ 0., 1., 4.], [ 0., 4., 10.], [ 0., 7., 16.]]) The * operator can be used as a shorthand for np. multiply on ndarrays.

How does element-wise multiply NumPy?

multiply() technique will be used to do the element-wise multiplication of matrices in Python. The NumPy library's np. multiply(x1, x2) method receives two matrices as input and executes element-wise multiplication over them before returning the resultant matrix. We must send the two matrices as input to the np.


1 Answers

It's doing elementwise multiplication just like the documentation says. note, in your first example,

a = np.array([[1,1,1],[2,2,2],[2,3]])
b = np.array([[2,3,4]])

You have an array of objects (lists) because all of the sub-lists don't have the same length.

>>> a = np.array([[1,1,1],[2,2,2],[2,3]])
>>> a
array([[1, 1, 1], [2, 2, 2], [2, 3]], dtype=object)

So when you multiply them, you're multiplying a list by an integer -- which what you got in the result.

e.g. if c = np.multiply(a, b), then:

c[0] == [1, 1, 1] * 2
c[1] == [2, 2, 2] * 3
c[2] == [2, 3] * 4

So far, we see that multiplying arrays of the same shape produces the Handamard product. What about when they aren't the same shape? In the case, numpy tries to "broadcast" them to the same shape. The rules can be somewhat complex, so I won't try to reproduce them here, but they can be found at http://docs.scipy.org/doc/numpy-1.10.1/user/basics.broadcasting.html. Scalar-array multiplication works the same as scalar-matrix multiplication works in mathematics. For arrays which aren't the same shape, the trailing dimensions must match and the array with fewer dimensions is repeated as many times as necessary to fill out the missing dimensions and then the Handamard product is performed.

e.g.

a = np.array([[1, 2, 3], [1, 2, 3]])
b = np.array([3, 2, 1])
c = np.array([[3, 2, 1], [3, 2, 1]])

In this case, a * b and a * c will give the same result.


Obviously, the way I'm describing it isn't the way it's implemented (that would be seriously inefficient), but it helps as a way of thinking about it.


like image 115
mgilson Avatar answered Nov 02 '22 20:11

mgilson