numpy中的广播索引操作

时间:2018-02-16 14:15:02

标签: numpy numpy-broadcasting numpy-indexing

如果多个索引数组有广播,我如何从NumPy数组中获取元素?或者:我如何简化/矢量化这个循环:

elems = np.random.rand(3, 10, 7) # shape N x I x M
ind = np.array([[1, 2], [3, 4], [0, 9]]) # shape N x J
res = np.stack([elems[i, ind[i]] for i in range(len(elems))]) # shape N x J x M

1 个答案:

答案 0 :(得分:2)

将循环索引转换为arange并使用braodcasting:

event-propagation-path