Numpy确实将float('nan')和float区别对待-转换为None [英] Numpy does treat float('nan') and float differently - convert to None

查看:619
本文介绍了Numpy确实将float('nan')和float区别对待-转换为None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从普通数组中创建一个Numpy数组,并将nan值转换为None-但是成功与否取决于天气,第一个值是普通"浮点数或float('nan').

I want to create a Numpy array form a normal array and convert nan values to None - but the success depends on weather the first value is a "normal" float, or a float('nan').

这是我的代码,从初始数组开始:

Here is my code, starting with the initial array:

print(a)
array('d', [3.2345, nan, 2.0, 3.2, 1.0, 3.0])
print(b)
array('d', [nan, nan, 2.0, 3.2, 1.0, 3.0])

现在,我想通过矢量化函数将所有nan值交换到Python None:

Now I would like to swap all nan values to Python None via a vectorized function:

def convert(x):
    if x != x:
        return None
    else:
        return x

convert_vec = numpy.vectorize(convert)

简单,但是会导致两个不同的结果:

Simple, but leads to two different results:

numpy.asarray(convert_vec(a))

array([[ 3.2345,  2.    ,  1.    ], [    nan,  3.2   ,  3.    ]])

numpy.asarray(convert_vec(b))
array([[None, 2.0, 1.0], [None, 3.2, 3.0]], dtype=object)

这是为什么?是的,我可以看到一个很小的区别-第二个将object作为dtype.但是使用numpy.asarray(convert_vec(a), dtype=object)修复了问题-两者都将object作为dtype-但不会改变结果的差异.

Why is this? Yes, I can see a small difference - the second one has object as dtype. But using numpy.asarray(convert_vec(a), dtype=object) fixed it - both have object as dtype - but it doesn't change the difference in results.

推荐答案

np.nan是浮点值,None不是数字.

np.nan is a float value, None is not numeric.

In [464]: np.array([1,2,np.nan,3])
Out[464]: array([  1.,   2.,  nan,   3.])

In [465]: np.array([1,2,None,3])
Out[465]: array([1, 2, None, 3], dtype=object)

In [466]: np.array([1,2,None,3],dtype=float)
Out[466]: array([  1.,   2.,  nan,   3.])

如果您尝试创建一个包含None的数组,结果将是一个dtype=object数组.如果您坚持使用float dtype,则None将转换为nan.

If you try to create an array that contains None, the result will be a dtype=object array. If you insist on a float dtype, the None will be converted to nan.

vectorize情况下,如果不指定return dtype,它将从第一个元素推导出.

In the vectorize case, if you don't specify the return dtype, it deduces it from the first element.

您的示例有些混乱(您需要对其进行编辑),但我认为

Your examples are a bit confusing (you need to edit them), but I think that

convert(np.nan) => None
convert(123) => 123

如此

convert_vec([123,nan,...]) => [123, nan, ...],dtype=float
convert_vec([nan,123,...]) => [None, 123,...],dtype=object

  • 尝试将np.nan转换为None是一个坏主意,除非出于显示目的.

    • trying to convert np.nan to None is a bad idea, except maybe for display purposes.

      vectorize没有明确的结果dtype规范是一个坏主意

      vectorize without explicit result dtype specification is a bad idea

      这可能不是vectorize的好用法.

      这是转换nan值的另一种方法:

      Here's an alternative way of converting the nan values:

      In [467]: a=np.array([1,2,np.nan,34,np.nan],float)    
      In [468]: a
      Out[468]: array([  1.,   2.,  nan,  34.,  nan])
      In [471]: ind=a!=a   
      In [472]: ind
      Out[472]: array([False, False,  True, False,  True], dtype=bool)
      
      In [473]: a[ind]=0   # not trying None
      In [474]: a
      Out[474]: array([  1.,   2.,   0.,  34.,   0.])
      

      或使用屏蔽数组:

      In [477]: am=np.ma.masked_invalid(a)
      
      In [478]: am
      Out[478]: 
      masked_array(data = [1.0 2.0 -- 34.0 --],
                   mask = [False False  True False  True],
             fill_value = 1e+20)
      
      In [479]: am.filled(0)
      Out[479]: array([  1.,   2.,   0.,  34.,   0.])
      

      这篇关于Numpy确实将float('nan')和float区别对待-转换为None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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