python中带掩码数组的If语句

2024-04-19 02:31:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用屏蔽数组运行(嵌套条件)程序。我使用了相同的函数,没有屏蔽数组,工作正常。函数如下所示:

import numpy as np
x = np.random.rand(100)
x = x.reshape(4,25)
x = np.ma.masked_less(x,0.2) 
y = np.random.rand(100)
y = y.reshape(4,25)
y = np.ma.masked_less(y,0.2)
z = np.zeros_like(x)

#say that both arrays are masked in the same positions.

for i in range(len(x)):
    for j in range(len(y)):
        if x[i,j] >= y[i,j]:
            if (x[i,j]-y[i,j]) > (z[i,j-1]):
               z[i,j] = 0.
            else:
               z[i,j] = 1.
        else:
            z[i,j] = z[i,j-1] - (x[i,j]-y[i,j])

我确实希望得到一个与输入数据(在本例中为x,y)具有相同特性(即也被屏蔽)的数组。但是,我得到的结果是,或者是一个完全屏蔽的数组,或者是一个填充了没有掩码的值的数组,如我在这里所示:

^{pr2}$

当我真的想要这样的东西时:

z = 
masked_array(data =
[[9.0 -- -- ..., -- -- --]
[8.7 -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]
..., 
[1.0 -- -- ..., -- -- --]
[-- 3.6 -- ..., -- -- --]
[-- -- -- ..., -- -- --]],
         mask =
[[ False  True  True ...,  True  True  True]
[ False  True  True ...,  True  True  True]
[ True  True  True ...,  True  True  True]
..., 
[ False  True  True ...,  True  True  True]
[ True  False  True ...,  True  True  True]
[ True  True  True ...,  True  True  True]],
   fill_value = 9.96920996839e+36)

我已经阅读了关于蒙版数组的可用信息,以及类似的问题,但没有一个对此有令人满意的解释。我想知道是否有可能条件语句的工作方式与纽比。在哪里从某种意义上说,它们只显示了这些条件的指数?在


Tags: 函数infalsetruefornprandom数组
3条回答

还有另一种方法可以进行计算,而不必屏蔽数组。 如果您愿意,您仍然可以在最后屏蔽z数组。在

    x = np.random.rand(100)
    x = x.reshape(4,25)
    y = np.random.rand(100)
    y = y.reshape(4,25)

    # first if- first if
    idxs_1 = ( x >= y) & ((x-y) > (z-1)) 
    z[idxs_1] = 0

    # second if-else
    idxs_2 = (x>=y) & ((x-y) <= z-1)
    z[idxs_2] = 1

    # final else
    idxs_3 = x < y 
    idxs_3_p = np.hstack((idxs_3[:, 1:], idxs_3[:,0][:,None])) # reshape so that we shift z by one column left        

    z[idxs_3] = z[idxs_3_p] - (x[idxs_3] - y[idxs_3])

您需要再次检查一些测试数据的布尔索引的正确性。在

我用“外部”循环解决了我的问题。我的意思是我只在核心函数中使用了一个循环,而不是根据数据(1D到3D数组)使用2个或3个循环,即

def func(x,y):
    import numpy as np
    x = np.random.rand(100)
    x = x.reshape(4,25)
    x = np.ma.masked_less(x,0.2) 
    y = np.random.rand(100)
    y = y.reshape(4,25)
    y = np.ma.masked_less(y,0.2)
    z = np.zeros_like(x)

#say that both arrays are masked in the same positions.

    for i in range(len(x)):
        z[i] =  x[i] >= y[i]:
            if (x[i]-y[i]) > (z[i-1]):
               z[i] = 0.
            else:
               z[i] = 1.
        else:
            z[i] = z[i-1] - (x[i]-y[i])

return z

然后,如果有,比如一个3D数组netCDF文件,我将函数应用为:

^{pr2}$

请注意使用纽比。在哪里也是一个好的和更快的选择。然而,由于我在python(以及一般编程)方面的短暂经验,我并没有利用numpy机制来向量化我的函数。目前,我的功能似乎如我所料。在

  1. 第二个if-语句中可能有打字错误。应该是的

    for i in range(len(x)):
        for j in range(len(y)):
            if x[i,j] >= y[i,j]:
                if (x[i,j]-y[i,j]) > (z[i,j-1]): # not x - y[i,j], but x[i,j]-y[i,j] ??
                    # and further down 
    
  2. 在我的计算机上,它实际工作并产生预期的结果,即掩码/非掩码混合在一起的掩码数组:

     In [2]: z
     Out[2]: 
     masked_array(data =
      [[0.34864202355178786 1.0 1.0 1.6118423555903627   0.0 0.0 0.0 0.0 0.0 0.0
       0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
      [0.32457641778594915 1.0 1.0     0.0   0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
       0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
      [  1.0 1.541983077540757 1.0 0.0 0.0   0.0 0.0 0.0   0.0 0.0   0.0 0.0
       0.0 0.0 0.0 0.0 0.0   0.0 0.0  ]
      [    1.0   0.0 0.0 0.0 0.0 0.0   0.0   0.0 0.0 0.0   0.0   0.0  
         0.0 0.0 0.0 0.0]],
                  mask =
      [[False False False False  True False False False False False False False
       False False False False False False False False False False False False
       False]
      [False False False  True  True False  True False False False False False
       False False False False False False False False False False False False
       False]
      [ True False False False False False  True False False False  True False
       False  True False False False False False False False  True False False
        True]
      [ True  True False  True False False False False False  True False  True
       False False False  True False  True False  True  True False False False
       False]],
            fill_value = 1e+20)
    

相关问题 更多 >