python - 使用两个 bool 数组索引 2D np.array 时出现意外行为

标签 python arrays numpy indexing slice

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) 处的值; 39。请注意,如果两个数组的真值数量不同,则会引发错误!

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 function ix_ 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/

相关文章:

python - Pandas+seaborn 多维数据框分面

python - 存储用户信息 Django

java 用给定的信息声明并实例化一个二维数组

php - 在 PHP 中合并文件和发布数据

javascript - 如何向数组键添加新值

python - 填充 numpy 数组

python - 使用自定义谓词对 numpy 数组进行排序

python - 我的聊天消费者无法使用 django-channels?

python - 线程应用程序的信号量同步失败,Python

python - 双循环优化