How to do the product of arrays of an array in Python

Joracosu :

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]
Willem Van Onsem :

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])

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=8192&siteId=1