简体   繁体   English

将 2D 或 1D 掩码 arrays 索引到 Numpy 中的 1D arrays 的有效通用代码

[英]Valid generic code to index 2D or 1D masked arrays into 1D arrays in Numpy

I would like to have a valid code for either 2D or 1D masked array to extract a 1D array from it.我想为 2D 或 1D 掩码数组提供有效代码,以从中提取 1D 数组。 In the 2D case, one column would be entirely masked and should be removed (this can be done as shown in this question for example ).在 2D 情况下,一列将被完全屏蔽并且应该被删除(这可以按照这个问题中的示例来完成)。

import numpy as np

a = np.ma.masked_array(range(10*2), mask=[True, False]*10).reshape(10,2)
a = np.ma.masked_equal(a, 13)
b = np.ma.masked_equal(np.array(range(10)), 3)

print(a)
print(b)
# [[-- 1]
#  [-- 3]
#  [-- 5]
#  [-- 7]
#  [-- 9]
#  [-- 11]
#  [-- --]
#  [-- 15]
#  [-- 17]
#  [-- 19]]
# [0 1 2 -- 4 5 6 7 8 9]

# HERE I would like the same indexing valid for both (2D and 1D) situations:
a = a[:, ~np.all(a.mask, axis=0)].squeeze()
b = b[:] # I am not supposed to know that b is actually 1D and not a problematic 2D array

print(a)
print(b)
# [1 3 5 7 9 11 -- 15 17 19]
# [0 1 2 -- 4 5 6 7 8 9]
print(a-b)
# [1 2 3 -- 5 6 -- 8 9 10]

What would be a valid, pythonic code to achieve this?什么是有效的pythonic代码来实现这一点?

Sub-question: to my surprise, during my attempts the following did work:子问题:令我惊讶的是,在我的尝试中,以下确实有效:

b = b[:, ~np.all(b.mask, axis=0)].squeeze()
print(b)
# [1 3 5 7 9 11 -- 15 17 19]

Why don't I get a IndexError: too many indices for array error while I use 2D indexing for this 1D array?为什么我没有收到IndexError: too many indices for array error 当我对此一维数组使用二维索引时?

Is there any better option to address the original question?有没有更好的选择来解决原始问题? Thanks!谢谢!

You can use a = a[:, ~np.all(a.mask, axis=0)].squeeze() for both cases (1D and 2D).您可以将a = a[:, ~np.all(a.mask, axis=0)].squeeze()用于两种情况(一维和二维)。

In the 1D case of your example you get b[:, ~np.all(b.mask, axis=0)] which is b[:, True] .在您的示例的一维情况下,您会得到b[:, ~np.all(b.mask, axis=0)] ,即b[:, True] It seems that this should throw an indexing error but True behaves like np.newaxis in this case, ie the result of b[:, True] is an array of shape (10,1) .似乎这应该引发索引错误,但在这种情况下True的行为类似于np.newaxis ,即b[:, True]的结果是一个形状为(10,1)的数组。 See this SO answer for why this is so and what's the motivation behind it (the answer pertains to the 0-dimensionsal case but it turns out to work for higher dimensions the same way).请参阅此 SO 答案,了解为什么会这样以及其背后的动机是什么(答案与 0 维情况有关,但事实证明它同样适用于更高维度)。 squeeze then removes this additional dimension so that you didn't notice it.然后squeeze会删除这个额外的维度,这样你就不会注意到它。

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

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