[Numpy-discussion] confusion on importing numpy

Anne Archibald peridot.faceted@gmail....
Wed Aug 6 16:10:34 CDT 2008


2008/8/6 Eric Firing <efiring@hawaii.edu>:

> While I agree with the other posters that "import *" is not preferred,
> if you want to use it, the solution is to use amin and amax, which are
> provided precisely to avoid the conflict.  Just as arange is a numpy
> analog of range, amin and amax are numpy analogs of min and max.

There are actually several analogs of min and max, depending on what
you want. np.minimum(a,b) takes the elementwise minimum, that is,
np.minimum([1,2,3],[3,2,1]) is [1,2,1]. np.amin(a) finds the minimum
value in the array a. (It's equivalent to np.minimum.reduce(a).)

One reason automatically overriding min is a Bad Idea is that you will
certainly shoot yourself in the foot:

In [4]: numpy.min(2,1)
Out[4]: 2

In [5]: min(2,1)
Out[5]: 1

Note that this does not raise any kind of error, and it sometimes
returns the right value. It's especially bad if you do, say,
numpy.max(a,0) and expect the result to be always positive: now you
get a bug in your program that only turns up with exceptional values.
numpy.min is not the same as python's min, and using it as a
substitute will get you in trouble.

Anne


More information about the Numpy-discussion mailing list