且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

多个维度中的numpy数组整数索引

更新时间:2022-11-14 08:39:05

您还可以选择在索引数组之间使用广播,这是我通常要做的,而不是两次索引,从而创建您的中间副本数据:

You also have the option of using broadcasting among the indexing arrays, which is what I would normally do, rather than indexing twice, which creates an intermediate copy of your data:

>>> x[[[1], [2]],[[3, 4, 5]]]
array([[ 9, 10, 11],
       [15, 16, 17]])

要更好地了解正在发生的事情以及如何处理大量索引:

To see a little better what is going on and how to handle larger numbers of indices:

>>> row_idx = np.array([1, 2])
>>> col_idx = np.array([3, 4, 5])
>>> x[row_idx.reshape(-1, 1), col_idx]
array([[ 9, 10, 11],
       [15, 16, 17]])