Is there a better way of making numpy.argmin() ignore NaN values

ε祈祈猫儿з 提交于 2019-11-29 17:32:14

问题


I want to get the index of the min value of a numpy array that contains NaNs and I want them ignored

>>> a = array([ nan,   2.5,   3.,  nan,   4.,   5.])  
>>> a  
array([ NaN,  2.5,  3. ,  NaN,  4. ,  5. ])  

if I run argmin, it returns the index of the first NaN

>>> a.argmin()  
0  

I substitute NaNs with Infs and then run argmin

>>> a[isnan(a)] = Inf  
>>> a  
array([ Inf,  2.5,  3. ,  Inf,  4. ,  5. ])  
>>> a.argmin()  
1  

My dilemma is the following: I'd rather not change NaNs to Infs and then back after I'm done with argmin (since NaNs have a meaning later on in the code). Is there a better way to do this?

There is also a question of what should the result be if all of the original values of a are NaN? In my implementation the answer is 0


回答1:


Sure! Use nanargmin:

import numpy as np
a = np.array([ np.nan,   2.5,   3.,  np.nan,   4.,   5.])
print(np.nanargmin(a))
# 1

There is also nansum, nanmax, nanargmax, and nanmin,

In scipy.stats, there is nanmean and nanmedian.

For more ways to ignore nans, check out masked arrays.



来源:https://stackoverflow.com/questions/2821072/is-there-a-better-way-of-making-numpy-argmin-ignore-nan-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!