Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除每个值都被屏蔽的列_Python_Numpy_Masking - Fatal编程技术网

Python 删除每个值都被屏蔽的列

Python 删除每个值都被屏蔽的列,python,numpy,masking,Python,Numpy,Masking,我想从屏蔽数组中删除列,其中列中的每个值都被屏蔽。在下面的例子中: >>> import numpy as np >>> test = np.array([[1,0,0],[0,3,0],[1,4,0]]) >>> test = np.ma.masked_equal(test,0) >>> test [[1 -- --] [-- 3 --] [1 4 --]], >>> np.somefunction

我想从屏蔽数组中删除列,其中列中的每个值都被屏蔽。在下面的例子中:

>>> import numpy as np
>>> test = np.array([[1,0,0],[0,3,0],[1,4,0]])
>>> test = np.ma.masked_equal(test,0)
>>> test
[[1 -- --]
[-- 3  --]
[1  4  --]],
>>> np.somefunction(test)
[[1  --]
 [-- 3 ]
 [1  4 ]]

要获得给定的输出,np.somefunction()应该是什么

您可以使用奇特的索引:

test[:, ~np.all(test == 0, axis=0)]
test[:, ~np.all(test.mask, axis=0)]
#masked_array(data =
# [[1 --]
# [-- 3]
# [1 4]],
#             mask =
# [[False  True]
# [ True False]
# [False False]],
#       fill_value = 0)
In [13]: test
Out[13]:
masked_array(data =
 [[1 -- --]
 [-- 3 --]
 [1 4 --]],
             mask =
 [[False  True  True]
 [ True False  True]
 [False False  True]],
       fill_value = 0)


In [14]: test[:, :2]
Out[14]:
masked_array(data =
 [[1 --]
 [-- 3]
 [1 4]],
             mask =
 [[False  True]
 [ True False]
 [False False]],
       fill_value = 0)