简体   繁体   English

用元组进行花式索引

[英]Numpy fancy indexing with tuples

In the numpy tutorial from Scipy , while teaching fancy indexing of numpy arrays, I got the following explanation diagram. Scipynumpy教程中 ,在教授numpy数组的奇特索引时,我得到了以下说明图。

在此处输入图片说明

As no other explanation about this particular array is given there, I have created the array using 由于此处未提供有关此特定数组的其他说明,因此我使用

a = np.array([[j+i for i in range(0,6)] for j in range(0, 60, 10)])

If I run a[(0,1,2,3,4),(1,2,3,4,5)] I am getting array([ 1, 12, 23, 34, 45]) , consistent with the picture. 如果我运行a[(0,1,2,3,4),(1,2,3,4,5)]我得到array([ 1, 12, 23, 34, 45]) ,与图片。 But I can not understand how the tuples are getting unpacked to a[0,1] and so on. 但是我不明白元组如何解包到a [0,1],依此类推。

I am trying to understand the mechanism of this. 我正在尝试了解这种机制。 An in-depth answer will be much appreciated. 深入的答案将不胜感激。

All fancy indexing does is essentially give you a list of co-ordinates in the larger array. 花式索引的全部作用实际上是为您提供较大数组中的坐标列表。 So think of that picture as a big grid and your two tuples as x-coordinates and y-coordinates (it generalizes to higher dimensions too). 因此,将该图片视为一个大网格,并将您的两个元组视为x坐标和y坐标(它也可以推广到更高的维度)。 So if you zip them together you get: 因此,如果将它们压缩在一起,则会得到:

(x=0, y=1), (x=1, y=2), (x=2, y=3), (x=3, y=4), (x=4, y=5)

which if you read off the image you will see gives you: 如果您阅读图像,将会看到以下内容:

(1, 12, 23, 34, 45)

as expected 符合预期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM