[英]Deleting masked elements in numpy array
I have some arrays that contain masked elements (from Numpy.MaskedArray
), eg 我有一些包含掩码元素的数组(来自
Numpy.MaskedArray
),例如
data = [0,1,masked,3,masked,5,...]
Where the mask doesn't follow a regular pattern. 掩模不遵循规则图案的地方。
I want to iterate through the array and simply delete all elements that are masked to end up with: 我想遍历数组并简单地删除所有被屏蔽的元素以结束:
data = [0,1,3,5,...]
I've tried a loop like: 我尝试了一个循环:
for i in xrange(len(data)):
if np.ma.is_masked(data[i]):
data.pop(i)
But I get the error: local variable 'data' referenced before assignment
但是我得到了错误:
local variable 'data' referenced before assignment
Do I have to create a new array and add the unmasked elements? 我是否必须创建一个新数组并添加未屏蔽的元素? Or is there a
MaskedArray
function that can automatically do this? 或者是否有
MaskedArray
函数可以自动执行此操作? I've had a look at the documentation but it's not obvious to me. 我看过文档,但对我来说并不明显。
Thanks! 谢谢!
data.compressed()
是您正在寻找的功能
用掩码按位反转 ~
:
data = data[~data.mask]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.