如何在numpy数组中沿特定轴的蒙版下获取标准偏差?

data = array([[ 0,  1,  2,  3,  4],
              [ 5,  6,  7,  8,  9],
              [10, 11, 12, 13, 14],
              [15, 16, 17, 18, 19],
              [20, 21, 22, 23, 24]])

M = array([[0, 1, 0, 0, 0],
           [1, 1, 1, 1, 1],
           [1, 1, 0, 1, 1],
           [0, 0, 1, 0, 0],
           [0, 0, 0, 0, 0]])


结果数组应为:

masked_std = std( data, axis=0, mask=M )
[ std([5,10]), std([1,6,11]), std([7,17]), std([8,13], std([9,14]) ]

最佳答案

您可以使用numpy masked array

In [19]: from numpy import ma

In [20]: data
Out[20]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

In [21]: M
Out[21]:
array([[0, 1, 0, 0, 0],
       [1, 1, 1, 1, 1],
       [1, 1, 0, 1, 1],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0]])

In [22]: mdata = ma.masked_array(data, mask=~M.astype(bool))

In [23]: mdata
Out[23]:
masked_array(data =
 [[-- 1 -- -- --]
 [5 6 7 8 9]
 [10 11 -- 13 14]
 [-- -- 17 -- --]
 [-- -- -- -- --]],
             mask =
 [[ True False  True  True  True]
 [False False False False False]
 [False False  True False False]
 [ True  True False  True  True]
 [ True  True  True  True  True]],
       fill_value = 999999)


In [24]: mdata.std(axis=0)
Out[24]:
masked_array(data = [2.5 4.08248290464 5.0 2.5 2.5],
             mask = [False False False False False],
       fill_value = 999999)

关于python - mask 下的小块标准偏差,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12961279/

10-12 22:00