有没有做numpy.argmin()忽略NaN值的更好的方法 [英] Is there a better way of making numpy.argmin() ignore NaN values

查看:2155
本文介绍了有没有做numpy.argmin()忽略NaN值的更好的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个包含NaN的一个numpy的数组中的最小值的指标,我想他们忽略

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. ])  

如果我跑argmin,它返回第一个楠指数

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

>>> a.argmin()  
0  

我代替非数字与INFS然后argmin

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  

我的困境是以下内容:我宁愿不改变非数字为INF文件,然后我与argmin完成复出后(因为非数字在code有后来就一个意思) 。有没有更好的方式来做到这一点?

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?

有也是应该的结果是什么,如果所有的原始值的 为NaN的问题?我在执行答案是0

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

推荐答案

当然!使用 nanargmin

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

还有 nansum nanmax nanargmax nanmin

scipy.stats ,还有 nanmean nanmedian

更多的方式可忽略 S,看看蒙面阵列的。

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

这篇关于有没有做numpy.argmin()忽略NaN值的更好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆