two_d = np.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]])
first = np.array((True, True, False, False, False))
second = np.array((False, False, False, True, True))
现在,当我输入时:
two_d[first, second]
我得到:
array([3,9])
这对我来说意义不大。谁能简单解释一下?
最佳答案
当给定多个 bool 数组作为索引时,NumPy 会将 True 值的索引配对。 first
中的第一个真值与 second
中的第一个真值配对,依此类推。 NumPy 然后在每个 (x, y) 索引处获取元素。
这意味着 two_d[first, second]
等同于:
two_d[[0, 1], [3, 4]]
换句话说,您正在检索索引 (0, 3) 和索引 (1, 4) 处的值; 3
和 9
。请注意,如果两个数组的真值数量不同,则会引发错误!
documents on advanced indexing简要提及此行为并建议将 np.ix_
作为“不那么令人惊讶”的替代方案:
Combining multiple Boolean indexing arrays or a Boolean with an integer indexing array can best be understood with the
obj.nonzero()
analogy. The functionix_
also supports boolean arrays and will work without any surprises.
因此您可能正在寻找:
>>> two_d[np.ix_(first, second)]
array([[3, 4],
[8, 9]])
关于python - 使用两个 bool 数组索引 2D np.array 时出现意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34877314/