简体   繁体   English

在 jupyter 中的 `np.ma.array` 问题中屏蔽了 `np.nan`

[英]Masked `np.nan` in the `np.ma.array` problem in jupyter

Let's run in the Anaconda Jupyter the Python3 NumPy code:让我们在 Anaconda Jupyter 中运行 Python3 NumPy 代码:

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

and we have the warning: /.../anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in less .我们有警告: /.../anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in less

Substituting np.nan with 1.0 etc. --- no warning.1.0等替换np.nan --- 没有警告。

Why the np.nan can not be masked and then compared?为什么np.nan不能屏蔽再比较?

MA has several strategies to implementing methods. MA 有几种实施方法的策略。

1) evaluate the method on y.data , and make a new ma with y.mask . 1) 在y.data上评估该方法,并使用y.mask制作一个新的ma It may suppress any runtime warnings.它可能会抑制任何运行时警告。

2) evaluate the method on y.filled() # with the default fill value 2) 使用默认填充值评估y.filled() # 上的方法

3) evaluate the method on y.filled(1) # or some other innocuous value 3) 在y.filled(1) # 或其他一些无害的值上评估该方法

4) evaluate the method on y.compressed() 4) 评估y.compressed()上的方法

5) evaluate the method on y.data[~y.mask] 5) 在y.data[~y.mask]上评估该方法

multiplication, for example use filled(1) , and addition uses filled(0) .乘法,例如使用filled(1) ,加法使用filled(0)

It appears that the comparisons are done with 1).看来比较是用 1) 完成的。

I haven't studied the ma code in detail, but I don't think it does 5).我没有详细研究过ma代码,但我认为它没有 5)。

If you are using ma just to avoid the runtime warning, there are some alternatives.如果您使用ma只是为了避免运行时警告,还有一些替代方法。

  • there's a collection of np.nan... functions that filter out nan before calculating有一组np.nan...在计算之前过滤掉nan函数

  • there are ways of surpressing runtime warnings有多种方法可以抑制运行时警告

  • ufuncs have a where parameter that can be used to skip some elements. ufuncs有一个where参数,可用于跳过某些元素。 Use it with an out parameter to define the skipped ones.将它与out参数一起使用来定义跳过的参数。

=== ===

Looking a np.ma.core.py I see functions like ma.less .看着np.ma.core.py我看到像ma.less这样的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)

Looking at the code, ma.less and such are a MaskedBinaryOperation class, and use 1) - evaluate on the data with查看代码, ma.less等是一个MaskedBinaryOperation类,并使用 1) - 对data进行评估

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

The result mask is logical combination of the arguments' masks.结果掩码是参数掩码的逻辑组合。

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

Making the issue more simple, let's assume:让问题更简单,让我们假设:

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)

and the output is:输出是:

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

with the RuntimeWarning: /.../anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in greater .与 RuntimeWarning: /.../anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in greater

Changing:改变:

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

We get:我们得到:

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

so we have a masked element and the result without any RuntimeWarning.所以我们有一个被屏蔽的元素和没有任何 RuntimeWarning 的结果。

Changing now:现在改变:

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

We get (without RuntimeWorning):我们得到(没有 RuntimeWorning):

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

This work-around is however strange (by setting arbitrary value in the place of np.nan and mask saving).然而,这种解决方法很奇怪(通过在np.nan和掩码保存的位置设置任意值)。 Comparing something with masked should be always masked , shouldn't it?将某些内容与masked进行比较应该始终是masked ,不是吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM