numpy高级索引功能还是bug?

2024-09-30 16:33:41 发布

您现在位置:Python中文网/ 问答频道 /正文

numpy高级索引异常。我在为CS 190.1X做作业时遇到了这个问题。我花了一段时间来掌握尺寸。在

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

这是一个特性还是一个bug?:)


Tags: numpy尺寸np特性csarraybugint
1条回答
网友
1楼 · 发布于 2024-09-30 16:33:41

a[:, 1]是{a1}的一个例子。在

a[:, [1]]combining advanced and basic indexing的一个例子。:是一个基本片,但是[1]触发了高级整数索引。在

由于高级索引都是相邻的,the rules用于组合高级索引和基本索引,例如

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,),它会导致插入同一形状的额外维度 结果呢。在


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)

相关问题 更多 >