我如何索引这个numpy数组? [英] How can I index this numpy array?

查看:61
本文介绍了我如何索引这个numpy数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个形状为(5,3)的numpy矩阵a和一个形状为(5,)的索引向量b,其中索引向量中的每个条目都介于0到2之间,我如何创建一个新的向量c基于a及其索引向量b.

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.

推荐答案

arange用于其他维度:

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

这篇关于我如何索引这个numpy数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆