I have a one dimensional NumPy array:
a = numpy.array([2,3,3])
I would like to have the product of all elements, 18 in this case.
The only way I could find to do this would be:
b = reduce(lambda x,y: x*y, a)
Which looks pretty, but is not very fast (I need to do this a lot).
Is there a numpy method that does this? If not, what is the most efficient way of doing this? My real world arrays have 39 float elements.
In NumPy you can try:
For a larger array
numpy.arange(1,40) / 10.:your
reduce(lambda x,y: x*y, a)needs 24.2µs,numpy.prod(a)needs 3.9µs.EDIT:
a.prod()needs 2.67µs. Thanks to J.F. Sebastian!