简体   繁体   English

多维索引切片后的数组尺寸

[英]Dimensions of array after multidimensional index slicing

I want to slice a multidimensional numpy array (>2 dimensions) along 2 of its axes using index slicing. 我想使用索引切片沿其2个轴切片多维numpy数组(> 2个维度)。 What are the rules for where each of its original dimensions end up? 每个原始尺寸在何处终止的规则是什么?


To illustrate my problem, let me provide an example. 为了说明我的问题,让我提供一个示例。 Say we have a 4D array: 假设我们有一个4D阵列:

import numpy as np

a = np.arange(2*3*4*5).reshape(2,3,4,5)

I'll create a tuple of indices using numpy.where, for slicing along axes 1 and 3: 我将使用numpy.where创建一个索引元组,以沿轴1和3进行切片:

mask = np.where(np.random.rand(3,5) > 0.5)

This will pick out random slices from my array a . 这将从我的数组a选择随机切片。 Let's say it returned tuples of length 7. To preserve the remaining dimensions I will use slice(None) objects: 假设它返回了长度为7的元组。要保留其余尺寸,我将使用slice(None)对象:

b = a[(slice(None), mask[0], slice(None), mask[1])]

This changed the shape: 这改变了形状:

>>> a.shape
(2, 3, 4, 5)
>>> b.shape
(7, 2, 4)

The axes that were untouched (ie sliced using the slice(None) object) appear to have been preserved, whereas the sliced axes are destroyed and the resulting axis is moved to the front. 未触摸的轴(即使用slice(None)对象进行slice(None) )似乎已保留,而切片的轴被破坏,结果轴移到了前面。

However, this is not always the case. 然而,这并非总是如此。 When I apply a mask to axes 1 and 2: 当我在轴1和2上应用遮罩时:

mask2 = np.where(np.random.rand(3,4) > 0.5)
c = a[(slice(None), mask[0], mask[1], slice(None))]

I observe the following (numpy.where has returned tuples of length 7 again): 我观察到以下情况(numpy.where又返回了长度为7的元组):

>>> c.shape
(2, 7, 5)

The axis resulting from the axes that have been destroyed by the slicing did not move to the front this time. 这次被切片破坏的轴所产生的轴没有移到前面。

My guess is that it is related to whether the sliced axes are adjacent or not, but I want to know from what rules this behavior emerges. 我的猜测是,这与切片轴是否相邻有关,但是我想知道从什么规则中会出现这种现象。

https://docs.scipy.org/doc/numpy-1.15.4/reference/arrays.indexing.html#combining-advanced-and-basic-indexing https://docs.scipy.org/doc/numpy-1.15.4/reference/arrays.indexing.html#combining-advanced-and-basic-indexing

Your where masks will produce a 1d (7,) shape array if applied to a 2d array, the values where the condition is true. where将条件蒙版应用于2d数组,则where遮罩将产生1d (7,)形状数组。 You phrase that as 'destroying' a pair of axes. 您可以说这是在“消灭”一对轴。

In the second case that 7 can be placed between the 2 and 5 . 在第二种情况下,可以在25之间放置7

But in the first it's ambiguous because of the slice in the middle (the non adjacency) - the fall back rule is to put it first, and order the slices after. 但是在第一个中它是模棱两可的,因为中间是切片(非邻接)-后退规则是将其放在第一位,然后对切片排序。 In other words, instead of trying to choose between a (2,7,4) and (2,4,7) order, it chooses (7,2,4). 换句话说,它没有选择(2,7,4)和(2,4,7)顺序,而是选择(7,2,4)。

The ambiguity is clear in this case, and the default reasonable. 在这种情况下,歧义很明显,默认情况下是合理的。 It's more complicated with one or more of the dimensions is eliminated by a scalar index. 由于标量索引消除了一个或多个维度,因此更为复杂。

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

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