We could use NumPy's built-in np.maximum , made just for this purpose -
np.maximum(array1, array2)
Another way would be to use NumPy ufunc np.max in the 2D stack and max-reduce along the first axis (axis=0) -
np.max([array1,array2],axis=0)
Timing for 1 million datasets -
In [271]: array1 = np.random.randint(0,9,(1000000)) In [272]: array2 = np.random.randint(0,9,(1000000)) In [274]: %timeit np.maximum(array1, array2) 1000 loops, best of 3: 1.25 ms per loop In [275]: %timeit np.max([array1, array2],axis=0) 100 loops, best of 3: 3.31 ms per loop
Divakar
source share