如何numpy的指令序列切片索引? [英] How does numpy order array slice indices?

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

问题描述

我有一个np.array 数据形状(28,8,20),而我只需要它的某些条目,所以我采取了片:

I have an np.array data of shape (28,8,20), and I only need certain entries from it, so I'm taking a slice:

In [41]: index = np.array([ 5,  6,  7,  8,  9, 10, 11, 17, 18, 19])
In [42]: extract = data[:,:,index]
In [43]: extract.shape
Out[43]: (28, 8, 10)

到目前为止好,一切都理所应当的。但现在我的魔杖看只是前两个项目上的第一行的最后一个索引:

So far so good, everything as it should be. But now I wand to look at just the first two entries on the last index for the first line:

In [45]: extract[0,:,np.array([0,1])].shape
Out[45]: (2, 8)

等等,这应该是(8,2)。它切换指数周围,即使它没有当我切是最后一次!据我的了解,下面应该采取同样的方式:

Wait, that should be (8,2). It switched the indices around, even though it did not when I sliced the last time! According to my understanding, the following should act the same way:

In [46]: extract[0,:,:2].shape
Out[46]: (8, 2)

...但它给了我正是我想要的!只要我有一个三维阵列,不过,这两种方法似乎是等同的:

... but it gives me exactly what I wanted! As long as I have a 3D-array, though, both methods seem to be equivalent:

In [47]: extract[:,:,np.array([0,1])].shape
Out[47]: (28, 8, 2)

In [48]: extract[:,:,:2].shape
Out[48]: (28, 8, 2)

所以,我该怎么办,如果我想不只是前两个项目,但一个不规则的列表?当然,我可以转术后矩阵但这似乎非常反直觉的。
较好地解决了我的问题是这样的(虽然有可能是一个更优雅的):

So what do I do if I want not just the first two entries but an irregular list? I could of course transpose the matrix after the operation but this seems very counter-intuitive. A better solution to my problem is this (though there might be a more elegant one):

In [64]: extract[0][:,[0,1]].shape
Out[64]: (8, 2)

这给我们带来的实际

Which brings us to the actual

我不知道对于这种现象的原因是什么?谁决定,这是它应该如何工作大概知道更多关于编程的比我还多,并认为这是在某些方面,我完全缺少一致的。我可能会继续打我的头这一点,除非我有一个方法来理解它。

I wonder what the reason for this behaviour is? Whoever decided that this is how it should work probably knew more about programming than I do and thought that this is consistent in some way that I am entirely missing. And I will likely keep hitting my head on this unless I have a way to make sense of it.

推荐答案

这是(高级)部分指数的情况。有2个索引数组,和1片

This is a case of (advanced) partial indexing. There are 2 indexed arrays, and 1 slice

如果索引子空间分离(通过切片对象),则广播的索引空间是第一,其次是x的切片子空间

If the indexing subspaces are separated (by slice objects), then the broadcasted indexing space is first, followed by the sliced subspace of x.

http://docs.scipy.org/doc/numpy-1.8.1/reference/arrays.indexing.html#advanced-indexing

高级索引的例子指出,当 ind_1 ind_2 broadcastable子空间形状(2,3,4)是:

The advanced indexing example notes, when the ind_1, ind_2 broadcastable subspace is shape (2,3,4) that:

然而,X [:,ind_1,:,ind_2]具有形状(2,3,4,10,30,50),因为没有明确的地方在所述分度子空间下降,因而它是上涨上至的开始。它始终是可能使用.transpose()来移动子空间的任何地方所需

However, x[:,ind_1,:,ind_2] has shape (2,3,4,10,30,50) because there is no unambiguous place to drop in the indexing subspace, thus it is tacked-on to the beginning. It is always possible to use .transpose() to move the subspace anywhere desired.

在换句话说,这是索引不一样的 X [:, ind_1] [:, ind_2] 。 2.阵列共同经营定义(2,3,4)子空间。

In other words, this indexing is not the same as x[:, ind_1][[:,ind_2]. The 2 arrays operate jointly to define a (2,3,4) subspace.

在你的榜样,提取[0:,np.array([0,1])] 被理解为,选择 (2)子空间(在[0]和[0,1]行为共同,不按顺序),并结合以某种方式与中间尺寸。

In your example, extract[0,:,np.array([0,1])] is understood to mean, select a (2,) subspace (the [0] and [0,1] act jointly, not sequentially), and combine that in some way with the middle dimension.

一个更复杂的例子是提取[1,0],:[[0,1],[1,0]]] ,其产生的(2,2,8)阵列。这是一个(2,2)的第一个和最后一个层面,再加上中间的一个子空间。另一方面, X [[1,0] [:,:,[0,1],[1,0]]] 产生(2,8,2,2),从1日分别选择和最后一个维度。

A more elaborate example would be extract[[1,0],:,[[0,1],[1,0]]], which produces a (2,2,8) array. This is a (2,2) subspace of the 1st and last dimensions, plus the middle one. On the other hand, X[[1,0]][:,:,[[0,1],[1,0]]] produces a (2,8,2,2), selecting from the 1st and last dimensions separately.

的主要区别是索引选择是否操作顺序或联合。该`[...] [...]语法已经提供给操作顺序。高级索引给你一个方法索引联合。

The key difference is whether the indexed selections operate sequential or jointly. The `[...][...] syntax is already available to operate sequentially. Advanced indexing gives you a way indexing jointly.

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

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