numpy高级索引功能或错误? [英] numpy advanced indexing feature or bug?

查看:65
本文介绍了numpy高级索引功能或错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

numpy高级索引异常.我在为CS 190.1X进行分配时遇到了此问题.我花了一点时间来掌握尺寸.

numpy advanced indexing anomalies. I encountered this while working on an assignment for CS 190.1X. Took a while for me to grasp the dimensions.

>>> a = np.ndarray([3,3],int)
>>> a[:,1]
array([-1,0,0])
>>> a[:,[1]]
array([[-1],[0],[0]])

这是功能还是错误?:)

is this a feature or a bug ? :)

推荐答案

a [:, 1] a [:, [1]]

a[:, [1]] is an example of combining advanced and basic indexing. The : is a basic slice, but the [1] triggers advance integer indexing.

由于高级索引彼此相邻,因此

Since the advanced indexes are all next to each other, the rules for combining advance and basic indexing say,

将来自高级索引操作的维插入到结果数组中,并将它们插入到与初始数组相同的位置上(后一种逻辑使简单的高级索引行为像切片一样).

the dimensions from the advanced indexing operations are inserted into the result array at the same spot as they were in the initial array (the latter logic is what makes simple advanced indexing behave just like slicing).

因此,整数索引 [1] (其形状为数组的形状为(1,))会导致插入相同形状的额外尺寸结果.

Thus, the integer index, [1], whose shape as an array would be (1,), causes an extra dimension of the same shape to be inserted in the result.

In [12]: a = np.arange(9).reshape(3,3)

In [13]: a[:,1].shape
Out[13]: (3,)

In [14]: a[:,[1]].shape
Out[14]: (3, 1)

In [15]: a[:,[1,2]].shape
Out[15]: (3, 2)

In [16]: a[:,[[1,2],[0,1]]].shape
Out[16]: (3, 2, 2)

这篇关于numpy高级索引功能或错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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