[Numpy-discussion] strange behavior of np.minimum and np.maximum
derek at astro.physik.uni-goettingen.de
Wed Apr 6 08:00:26 EDT 2011
>>>> a, b, c = np.array(), np.array(), np.array()
>>>> min_val = np.minimum(a, b, c)
>>>> max_val = np.maximum(a, b, c)
> (I'm using numpy 1.4, and I observed the same behavior with numpy
> 2.0.0.dev8600 on another machine). I'm quite surprised by this
> (It took me quite a long time to figure out what was happening in a
> script of mine that wasn't giving what I expected, because of
> np.maximum changing the output of np.minimum). Is it a bug, or am I
> missing something?
you're just missing that np.minimum/np.maximum are _binary ufuncs_
np.minimum(X, Y, out=None)
i.e. you were telling np.minimum to store it's output in array c and
then return min_val, obviously as a reference, not a copy of it.
Thus when storing the output of np.maximum in c as well, the contents
of c also changed again.
Being binary ufuncs, I think you'll have to apply them consecutively
need the min/max of several arrays.
See also np.info(np.minimum)
More information about the NumPy-Discussion