2017-09-25 16 views
-1

我想为下面每行的特定列编号。 d是类似于(embedding_id x n_vocab)t的数据是正确embedding_id的列表。numpy中每一行的特定列

然后,我可以创建result像下面一样。数据很好,但我认为这并不聪明。如何以其他智能方式创建result

t = np.random.randint(10, size=(32,))           
d = np.random.randn(30, 32)              

result = []                  
for a,b in zip(d.transpose(), t):            
    print(a[b])                 
    result.append(a[b]) # I don't think this is good way             
result = np.array(result).astype(float)           
print(result)                 
print(result.shape) # (32,). 

回答

3

你可以这样index;使用numpy.arrange创建列索引,并与t使用它(这就是所谓的高级索引):

d[t, np.arange(len(t))] 

(d[t, np.arange(len(t))] == result).all() 
# True 
+0

这是伟大的。可以肯定的是,第一个元素“t”是一个特定的列号,第二个元素是特定的行。所以我想要做的是为每一行提取特定列的值。我会接受你的回答。谢谢! – jef

+0

你的理解是正确的。除了我会说't'是行索引,而第二个列索引按照惯例。 – Psidom

相关问题