【问题标题】:Masked `np.nan` in the `np.ma.array` problem in jupyter在 jupyter 中的“np.ma.array”问题中屏蔽了“np.nan”
【发布时间】:2020-06-05 20:03:12
【问题描述】:

让我们在 Anaconda Jupyter 中运行 Python3 NumPy 代码:

y = np.ma.array(np.matrix([[np.nan, 2.0]]), mask=[0, 1])
m = (y < 0.01)

我们收到警告:/.../anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in less

1.0 等替换np.nan --- 没有警告。

为什么np.nan不能被屏蔽然后比较?

【问题讨论】:

  • 但对于m = (y != 0.01)(或==y[m] 是有效的; m = (y[~np.isnan(y)] &lt; 0.01) 无效 --- 形状已更改。
  • y。这是你想要的蒙面吗?
  • 使用y = np.ma.array(np.matrix([[np.nan, 2.0]]), mask=[1, 0]) 和屏蔽m = (y &lt; 0.01) 我们有RuntimeWarning: invalid value encountered in less。更改为 m = (y != 0.01) 并添加 print(y, y.shape) ; print(y[m], y[m].shape) 我们得到预期:[[-- 2.0]] (1, 2)[[-- 2.0]] (1, 2)。但是m = np.isnan(y) ; m &amp;= (y[~m] &lt; 0.01) 给出了意想不到的print(y, y.shape) ; print(y[m], y[m].shape)[[-- 2.0]] (1, 2)[] (1, 0)——形状改变了。

标签: python numpy compare nan masked-array


【解决方案1】:

MA 有几种实现方法的策略。

1) 评估y.data 上的方法,并用y.mask 创建一个新的ma。它可能会抑制任何运行时警告。

2) 使用默认填充值评估y.filled() # 上的方法

3) 在y.filled(1)# 或其他一些无害的值上评估方法

4) 评估y.compressed()上的方法

5) 评估y.data[~y.mask]上的方法

乘法,例如使用filled(1),加法使用filled(0)

看来比较是用 1) 完成的。

我没有详细研究过ma的代码,但我认为它不会5)。

如果您使用ma 只是为了避免运行时警告,则有一些替代方法。

  • 有一组 np.nan... 函数在计算之前过滤掉 nan

  • 有一些方法可以抑制运行时警告

  • ufuncs 有一个where 参数,可用于跳过某些元素。将它与out 参数一起使用来定义跳过的参数。

===

查看np.ma.core.py 我看到类似ma.less 的函数。

In [857]: y = np.ma.array([np.nan, 0.0, 2.0], mask=[1, 0, 0])                                  
In [858]: y >1.0                                                                               
/usr/local/bin/ipython3:1: RuntimeWarning: invalid value encountered in greater
  #!/usr/bin/python3
Out[858]: 
masked_array(data=[--, False, True],
             mask=[ True, False, False],
       fill_value=True)
In [859]: np.ma.greater(y,1.0)                                                                 
Out[859]: 
masked_array(data=[--, False, True],
             mask=[ True, False, False],
       fill_value=True)

查看代码,ma.less 等是 MaskedBinaryOperation 类,并使用 1) - 使用 data 评估

np.seterr(divide='ignore', invalid='ignore')

结果掩码是参数掩码的逻辑组合。

https://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html#operations-on-masked-arrays

【讨论】:

    【解决方案2】:

    让问题更简单,让我们假设:

    y = np.ma.array([np.nan, 0.0, 2.0], mask=[1, 0, 0])
    m = (y > 1.0)
    print(y, y.shape) ; print(y[m], y[m].shape, m.shape)
    

    输出是:

    [-- 0.0 2.0] (3,)
    [2.0] (1,) (3,)
    

    带有 RuntimeWarning:/.../anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in greater

    变化:

    ...
    m = (y != 2.0)
    ...
    

    我们得到:

    [-- 0.0 2.0] (3,)
    [-- 0.0] (2,) (3,)
    

    所以我们有一个被屏蔽的元素和没有任何 RuntimeWarning 的结果。

    现在改变:

    ...
    m = y.mask.copy() ; y[np.isnan(y)] = 9.0 ; y.mask = m ; m = (y > 1.0)
    ...
    

    我们得到(没有 RuntimeWorning):

    [-- 0.0 2.0] (3,)
    [-- 2.0] (2,) (3,)
    

    然而,这种解决方法很奇怪(通过设置任意值来代替 np.nan 并保存掩码)。与masked 比较的东西应该总是masked,不是吗?

    【讨论】:

    • np.ma 具有 lessgreater 之类的函数来正确处理屏蔽。运算符重载不能处理这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 2017-01-30
    • 2021-03-27
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多