[英]Numpy nonzero/flatnonzero index order; order of returned elements in boolean indexing
I'm wondering about the order of indices returned by numpy.nonzero / numpy.flatnonzero. 我想知道numpy.nonzero / numpy.flatnonzero返回的索引的顺序。
I couldn't find anything in the docs about it. 我在文档中找不到任何关于它的内容。 It just says: 它只是说:
A[nonzero(flag)] == A[flag]
While in most cases this is enough, there are some when you need a sorted list of indices. 虽然在大多数情况下这已足够,但有些时候需要一个排序的索引列表。 Is it guaranteed that returned indices are sorted in case of 1-D or I need to sort them explicitly? 是否保证返回的索引在1-D的情况下排序,或者我需要明确地对它们进行排序? (A similar question is the order of elements returned simply by selecting with a boolean array (A[flag]) which must be the same according to the docs.) (类似的问题是简单地通过选择一个布尔数组(A [flag])返回的元素的顺序,根据文档必须相同。)
Example: finding the "gaps" between True elements in flag: 示例:查找标志中True元素之间的“间隙”:
flag=np.array([True,False,False,True],dtype=bool)
iflag=flatnonzero(flag)
gaps= iflag[1:] - iflag[:-1]
Thanks. 谢谢。
Given the specification for advanced (or "fancy") indexing with integers , the guarantee that A[nonzero(flag)] == A[flag]
is also a guarantee that the values are sorted low-to-high in the 1-d case. 给定带有整数的高级(或“花式”)索引的规范, A[nonzero(flag)] == A[flag]
的保证也保证了值在1-d中从低到高排序。案件。 However, in higher dimensions, the result (while "sorted") has a different structure than you might expect. 但是,在更高的维度中,结果(虽然“已排序”)具有与您预期不同的结构。
In short, given a 1-dimensional array of integers ind
and a 1-dimensional array x
to be indexed, we have the following for all valid i
defined for ind
: 简而言之,给定一个整数ind
的一维数组和一个要索引的一维数组x
,我们为ind
定义的所有有效i
都有以下内容:
result[i] = x[ind[i]]
result
takes the shape of ind
, and contains the values of x
at the indices indicated by ind
. result
采用ind
的形状,并包含由ind
指示的索引处的x
值。 This means that we can deduce that if x[flag]
maintains the original order of x
, and if x[nonzero(flag)]
is the same as x[flag]
, then nonzero(flag)
must always produce indices in sorted order. 这意味着我们可以推断,如果x[flag]
保持了原来的顺序x
,并且如果x[nonzero(flag)]
是相同的x[flag]
,然后nonzero(flag)
必须始终产生以排序的顺序索引。
The only catch is that for multidimensional arrays, the indices are stored as distinct arrays for each dimension being indexed. 唯一的问题是,对于多维数组,索引存储为每个被索引的维度的不同数组。 So in other words, 换句话说,
x[array([0, 1, 2]), array([0, 0, 0])]
is equal to 等于
array([x[0, 0], x[1, 0], x[2, 0]])
The values are still sorted, but each dimension is broken out into its own array. 值仍然是排序的,但每个维度都会分解为自己的数组。 (You can do interesting things with broadcasting as a result; but that's beyond the scope of this answer.) (你可以通过播放来做有趣的事情;但这超出了这个答案的范围。)
The only problem with this line of reasoning is that -- to my great surprise -- I can't find an explicit statement guaranteeing that boolean indexing preserves the original order of the array. 这种推理的唯一问题是 - 令我惊讶的是 - 我找不到一个明确的语句来保证布尔索引保留数组的原始顺序。 Nonetheless, I'm quite certain from experience that it does. 尽管如此,我从经验中确信它确实如此。 More generally, it would be unbelievably perverse to have x[[True, True, True]]
return a reversed version of x
. 更一般地说,让x[[True, True, True]]
返回x
的反转版本是令人难以置信的反常 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.