strange behavior of np.minimum and np.maximum
Hello,
a, b, c = np.array([10]), np.array([2]), np.array([7]) min_val = np.minimum(a, b, c) min_val array([2]) max_val = np.maximum(a, b, c) max_val array([10]) min_val array([10])
(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 behavior (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? Cheers, Emmanuelle
a, b, c = np.array([10]), np.array([2]), np.array([7]) min_val = np.minimum(a, b, c) min_val array([2]) max_val = np.maximum(a, b, c) max_val array([10]) min_val array([10])
(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 behavior (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?
Read the documentation for numpy.minimum and numpy.maximum: they give you element-wise minimum values from two arrays passed as arguments. E.g.:
numpy.minimum([1,2,3],[3,2,1]) array([1, 2, 1])
The optional third parameter to numpy.minimum is an "out" array - an array to place the results into instead of making a new array for that purpose. (This can save time / memory in various cases.) This should therefore be enough to explain the above behavior. (That is, min_val and max_val wind up being just other names for the array 'c', which gets modified in-place by the numpy.minimum and numpy.maximum.) If you want the minimum value of a sequence of arbitrary length, use the python min() function. If you have a numpy array already and you want the minimum (global, or along a particular axis), use the min() method of the array, or numpy.min(arr). Zach
Hi Zach and Derek, thank you very much for your quick and clear answers. Of course the third parameter is the out array, I was just being very stupid! (I had read the documentation though, but somehow it didn't make it to my brain :-) Sorry...
Read the documentation for numpy.minimum and numpy.maximum: they give you element-wise minimum values from two arrays passed as arguments. E.g.:
numpy.minimum([1,2,3],[3,2,1]) array([1, 2, 1])
The optional third parameter to numpy.minimum is an "out" array - an array to place the results into instead of making a new array for that purpose. (This can save time / memory in various cases.)
Thanks again, Emmanuelle
Hi Emmanuelle,
a, b, c = np.array([10]), np.array([2]), np.array([7]) min_val = np.minimum(a, b, c) min_val array([2]) max_val = np.maximum(a, b, c) max_val array([10]) min_val array([10])
(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 behavior (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_ with syntax 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 if you need the min/max of several arrays. See also np.info(np.minimum) HTH, Derek
participants (3)
-
Derek Homeier
-
Emmanuelle Gouillart
-
Zachary Pincus