如果多个索引数组有广播,我如何从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
答案 0 :(得分:2)
将循环索引转换为arange并使用braodcasting:
event-propagation-path