Numpy np.multiply vs *-Operator

There is no difference. However, the np.multiply function can take in additional, optional arguments, making it more versatile. See the docs.

Saying that * is overwritten with np.multiply would not be very precise. Generally, * maps to calls to the __mul__ and __rmul__ methods on the objects on which it acts. Thus, * is rather "overwritten" with np.ndarray.__mul__.


speed differences - none:

In [65]: timeit c = np.multiply(a,b)
4.95 ms ± 10.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [66]: timeit c = a*b
5.06 ms ± 180 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

For smaller arrays we might see differences due to a different calling stack, but with these, the actual computation dominates the time.

But as you can see from the docs, np.multiply is a ufunc with access to all the machinery that that implies.

For np.matrix objects, * is matrix product, np.multiply is element multiplication.

Tags:

Python

Numpy