0

I've build an array like:

a = [1,2,3,4]
b = eye(4)
c = np.where(b*(1/a) == 0,1,b*(1/a))
r = c*a
# Print Result:
array([[1., 2., 3., 4.],
       [1., 1., 3., 4.],
       [1., 2., 1., 4.],
       [1., 2., 3., 1.]])

So at next step I'd like to perform something like row wise multiplication to get something like:

array([[24.], # This means 1*2*3*4
       [12.], # 1*1*3*4
       [8.],  # 1*2*1*4
       [6.]]) # 1*2*3*1
# Or concisely:
array([24,12,8,6])
# In general, for n elements:
array([[x_11., --------- x_1n.],
       [x_21., --------- x_2n.],
       [                      ],
       [x_n1., --------- x_nn.]])

And what would be better option in time terms.

Karl
  • 53
  • 3
  • 3
    `np.multiply.reduce(array, axis=1)` – Frank Yellin Jan 22 '22 at 06:31
  • `np.prod(array, axis=1)` is `np.multiply.reduce(array, axis=1)` under the hood. Note that `np.prod(array, axis=0)` [might be](https://stackoverflow.com/a/26999092/3044825) 15% faster – mathfux Jan 22 '22 at 07:04
  • 1
    Btw, `numpy` is just a starting point of better performance. You can go further with `numexpr`, `numba` or `cython`. – mathfux Jan 22 '22 at 07:08

0 Answers0