我怎样才能索引这个numpy数组? [重复]

时间:2022-05-02 08:30:54

This question already has an answer here:

这个问题在这里已有答案:

Given a numpy matrix a of shape (5,3), and a index vector b of shape (5,), where each entry in the index vector is between 0 to 2, how can I create a new vector c based on a and its index vector b.

给定形状(5,3)的numpy矩阵a和形状(5,)的索引向量b,其中索引向量中的每个条目在0到2之间,如何基于a和0创建新的向量c它的索引向量b。

1 个解决方案

#1


1  

Use arange for the other dimension:

将范围用于其他维度:

c = a[np.arange(5), b]

#1


1  

Use arange for the other dimension:

将范围用于其他维度:

c = a[np.arange(5), b]