[Numpy-discussion] argmin and argmax without nan

Keith Goodman kwgoodman@gmail....
Thu Mar 24 08:51:22 CDT 2011


On Thu, Mar 24, 2011 at 6:19 AM, Ralf Gommers
<ralf.gommers@googlemail.com> wrote:
> 2011/3/24 Dmitrey <tmp50@ukr.net>:
>> hi,
>> is there any way to get argmin and argmax of an array w/o nans?
>> Currently I have
>>>>> from numpy import *
>>>>> argmax([10,nan,100])
>> 1
>>>>> argmin([10,nan,100])
>> 1
>> But it's not the values I would like to get.
>>
>> The walkaround I use: get all indeces of nans, replace them by -inf, get
>> argmax, replace them by inf, get argmin.
>> Is there any better way? (BTW, I invoke argmin/argmax along of a chosen
>> axis)
>> D.
>
> In [3]: np.nanargmax([10, np.nan, 100])
> Out[3]: 2
>
> In [4]: np.nanargmin([10, np.nan, 100])
> Out[4]: 0

And if speed is an issue (it usually isn't) you can use the nanargmax
from Bottleneck:

>> a = np.random.rand(10000)
>> a[a > 0.5] = np.nan
>> timeit np.nanargmax(a)
10000 loops, best of 3: 127 us per loop
>> import bottleneck as bn
>> timeit bn.nanargmax(a)
100000 loops, best of 3: 12.4 us per loop


More information about the NumPy-Discussion mailing list