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