【发布时间】:2020-07-19 12:14:06
【问题描述】:
我想使用我的“indexer”数组对数组的第三维进行索引,该数组包含第三维的整数索引。
如何通过使用 numpy 方法避免执行此 for 循环?
import numpy as np
output = np.random.rand(3,12,40000) #batch x sequence x vocab_size, dtype float64
indexer = np.random.randint(0,40000, (3,12,52)) #batch x sequence x knowledgebase_size, dtype int64
values = np.random.rand(3,12,52) #batch x seq_len, knowledgebase_size, dtype float64
batch, sequence, kb = values.shape
for x in range(batch):
for y in range(sequence):
for z in range(kb):
output[x,y,indexer[x,y,z]] += values[x,y,z]
查看 np 文档并没有产生任何结果;也找不到这个问题的完全匹配。
【问题讨论】:
标签: python arrays numpy multidimensional-array indexing