当传递列表而不是numpy中的元组时进行高级切片 [英] Advanced slicing when passed list instead of tuple in numpy

查看:93
本文介绍了当传递列表而不是numpy中的元组时进行高级切片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文档中,它说(强调我的意思):

In the docs, it says (emphasis mine):

高级索引的被触发时选择对象obj是一个 非元组序列对象,一个ndarray(数据类型为integer或bool), 或具有至少一个序列对象或ndarray(数据类型)的元组 整数或布尔值).有两种类型的高级索引:整数 和布尔值.

Advanced indexing is triggered when the selection object, obj, is a non-tuple sequence object, an ndarray (of data type integer or bool), or a tuple with at least one sequence object or ndarray (of data type integer or bool). There are two types of advanced indexing: integer and Boolean.

< snip>

<snip>

还认识到x[[1,2,3]]将触发高级索引编制, x[[1,2,slice(None)]]将触发基本切片.

Also recognize that x[[1,2,3]] will trigger advanced indexing, whereas x[[1,2,slice(None)]] will trigger basic slicing.

我知道为什么x[(1, 2, slice(None))]触发基本切片.但是,当[1,2,slice(None)]满足非元组序列的条件时,为什么x[[1,2,slice(None)]]会触发基本切片?

I know why x[(1, 2, slice(None))] triggers basic slicing. But why does x[[1,2,slice(None)]] trigger basic slicing, when [1,2,slice(None)] meets the condition of being a non-tuple sequence?

在相关说明中,为什么会发生以下情况?

On a related note, why does the following occur?

>>> a = np.eye(4)
>>> a[(1, 2)]  # basic indexing, as expected
0.0
>>> a[(1, np.array(2))] # basic indexing, as expected
0.0

>>> a[[1, 2]]  # advanced indexing, as expected
array([[ 0.,  1.,  0.,  0.],
   [ 0.,  0.,  1.,  0.]])
>>> a[[1, np.array(2)]]  # basic indexing!!??
0.0

推荐答案

该规则有一个例外. 高级索引"文档部分没有提及它,而是在

There's an exception to that rule. The Advanced Indexing documentation section doesn't mention it, but up above, near the start of the Basic Slicing and Indexing section, you'll see the following text:

为了保持与数字通用用法的向后兼容性,如果选择对象是任何包含切片对象的非ndarray序列(例如列表),则也会启动基本切片对象或newaxis对象,但不适用于整数数组或其他嵌入式序列.

In order to remain backward compatible with a common usage in Numeric, basic slicing is also initiated if the selection object is any non-ndarray sequence (such as a list) containing slice objects, the Ellipsis object, or the newaxis object, but not for integer arrays or other embedded sequences.


a[[1, np.array(2)]]不会完全触发基本索引.如注释在源代码中:


a[[1, np.array(2)]] doesn't quite trigger basic indexing. It triggers an undocumented part of the backward compatibility logic, as described in a comment in the source code:

    /*
     * Sequences < NPY_MAXDIMS with any slice objects
     * or newaxis, Ellipsis or other arrays or sequences
     * embedded, are considered equivalent to an indexing
     * tuple. (`a[[[1,2], [3,4]]] == a[[1,2], [3,4]]`)
     */

列表中的np.array(2)导致列表被视为元组,但是结果a[(1, np.array(2))]仍然是高级索引操作.与a[[1, 2]]不同,它最终将12应用于单独的轴,结果最终看起来与a[1, 2]相同,但是如果使用3D a进行尝试,它会产生一个副本而不是视图.

The np.array(2) inside the list causes the list to be treated as if it were a tuple, but the result, a[(1, np.array(2))], is still an advanced indexing operation. It ends up applying the 1 and the 2 to separate axes, unlike a[[1, 2]], and the result ends up looking identical to a[1, 2], but if you try it with a 3D a, it produces a copy instead of a view.

这篇关于当传递列表而不是numpy中的元组时进行高级切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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