1

We can compute the element-wise maximum of 3 numpy arrays:

import numpy as np
A = np.arange(20).reshape((4, 5))       # any 4x5 array
B = np.maximum(A, A+7, A+2)     # working

But why doesn't np.maximum accept multiple arrays from an "unpacking"?

L = [np.roll(A, k, axis=0) for k in range(4)]     # 4 arrays: A shifted with different k
np.maximum(*L)

Error:

ValueError: invalid number of arguments

After all, L is a Python list of Numpy array objects, so *L should unpack it for the np.maximum function call. Why doesn't it work?

PS: I also tried with L = (...) (which gives a generator) or L = tuple(...), but we have the same error.

0

1 Answer 1

0

As pointed out in a comment, reduce is the solution here:

np.maximum.reduce([np.roll(A, k, axis=0) for k in range(4)])
Sign up to request clarification or add additional context in comments.

2 Comments

Yes the question was already answered & I am glad that it helped you. Pardon me for Flagging your question ^_^.
No problem for flagging as duplicate @Rocky_Mental, it's perfectly fine :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.