Is there a method in numpy to multiply every element in an array?
Asked Answered
P

3

11

I want to multiply all elements in a numpy array. If there's an array like [1, 2, 3, 4, 5], I want to get value of 1 * 2 * 3 * 4 * 5.

I tried this by making my own method, but size of array is very large, it takes very longs time to calculate because I'm using numpy it would be helpful if numpy supports this operation.

I tried to find out through numpy documents, but I failed. Is there a method which does this operation? If there is, is there a way to get values along a rank in an matrix?

Palaeolithic answered 27/5, 2017 at 4:3 Comment(0)
W
15

I believe what you need is, numpy.prod.

From the documentation:

Examples

By default, calculate the product of all elements:

>>> np.prod([1.,2.])
2.0

Even when the input array is two-dimensional:

>>> np.prod([[1.,2.],[3.,4.]])
24.0

But we can also specify the axis over which to multiply:

>>> np.prod([[1.,2.],[3.,4.]], axis=1)
array([  2.,  12.])

So for your case, you need:

>>> np.prod([1,2,3,4,5])
120
Waves answered 27/5, 2017 at 4:10 Comment(0)
I
1

You can use something like this:

import numpy as np
my_array = np.array([1,2,3,4,5])
result = np.prod(my_array)
#Prints 1*2*3*4*5
print(result)

Here is the documentation of numpy.prod
Below is a excerpt from the link above:

By default, calculate the product of all elements:

>>> np.prod([1.,2.])
2.0

Even when the input array is two-dimensional:

>>> np.prod([[1.,2.],[3.,4.]])
24.0

But we can also specify the axis over which to multiply:

>>> np.prod([[1.,2.],[3.,4.]], axis=1)
array([  2.,  12.])
Irrefragable answered 27/5, 2017 at 4:18 Comment(3)
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.Gelding
I'm not sure it's appropriate that you copy-paste from the accepted answer. I understand that the accepted answer is copy-pasted from the documentation, but that you also copy-paste the exact same thing and therefore making your answer almost identical to the accepted answer doesn't seem appropriate. It wasn't so clear that the accepted answer was copy-pasted from the documentation so I edited it to make it clear. But you should only post your own answer if your solution is different from the other answer.Gelding
"If you check his edits, he did not include any example code when I posted my answer." That's not true. This is the original version of the other answer. Since then, all he added was the last paragraph ("so in your case you need...") and then I edited it to format it as a quote. You can check that here. Also, for your answer, it would have been enough to add something like "You can use numpy.prod" before your code and nothing after it.Gelding
S
0

In addition to other answers, every numpy.array has got prod() method you can use.

An example is following:

>>> np.array([6, 2, 3]).prod()
36
Shorten answered 17/9, 2021 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.