2

In a single line, how can I get the product of the arrays of an array? I need it to be done for multi columns cases

2 columns example:

X = [[1 4]
     [2 3]
     [0 2]
     [1 5]
     [3 1]
     [3 6]]

sol = [4 6 0 5 3 18]

4 columns example:

X = [[1 4 2 3]
     [2 3 1 5]
     [0 2 3 4]
     [1 5 2 2]
     [3 1 1 6]
     [3 6 3 1]]

sol = [24 30 0 20 18 54]

2 Answers 2

4

This is a row-wise multiplication. You can perform this with:

X.prod(axis=1)

for example:

>>> X
array([[1, 4],
       [2, 3],
       [0, 2],
       [1, 5],
       [3, 1],
       [3, 6]])
>>> a.prod(axis=1)
array([ 4,  6,  0,  5,  3, 18])
Sign up to request clarification or add additional context in comments.

2 Comments

I don't understand the 'Note'
@Joracosu: sorry, I was talking about your second example. It is not a 4d array, it is a 2d array with four columns in your example.
0

You can also use numpy.multiply.reduce

np.multiply.reduce(x, axis=1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.