This question already has an answer here:
这个问题在这里已有答案:
- using an numpy array as indices of the 2nd dim of another array? [duplicate] 2 answers
使用numpy数组作为另一个数组的第二个dim的索引? [重复] 2个答案
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]