python数组维度的顺序_python–Numpy多维数组索引交换轴顺序

python数组维度的顺序_python–Numpy多维数组索引交换轴
顺序
手机图铃
从语法上讲,这看起来像是⼀种不⼀致,但从语义上讲,你在这⾥做了两件截然不同的事情.在您对a和b的定义中,您正在执⾏
advanced indexing,有时称为椭偏仪
fancy indexing,它返回数据的副本.在你的c定义中,你正在做
basic slicing,它返回⼀个数据视图.
法国七月革命
为了区分它,有助于理解索引如何传递给python对象.这⾥有些例⼦:
>>> class ShowIndex(object):
... def __getitem__(self, index):
anarchy in the uk
... print index
...
>>> ShowIndex()[:,:]
(slice(None, None, None), slice(None, None, None))
>>> ShowIndex()[...,:]
(Ellipsis, slice(None, None, None))
>>> ShowIndex()[0:5:2,::-1]
(slice(0, 5, 2), slice(None, None, -1))
>>> ShowIndex()[0:5:2,np.arange(3)]
(slice(0, 5, 2), array([0, 1, 2]))
>>> ShowIndex()[0:5:2]
slice(0, 5, 2)
>>> ShowIndex()[5, 5]
氢氧化铁
(5, 5)
>>> ShowIndex()[5]
5
>>> ShowIndex()[np.arange(3)]
[0 1 2]
金纯魅惑如您所见,有许多不同的可能配置.⾸先,可以传递单个项⽬,或者可以传递项⽬元组.其次,元组可能包含切⽚对象,省略号对象,普通整数或numpy数组.
仅传递int,slice或Ellipsis对象或None(与waxis相同)等对象时,将激活基本切⽚.这些可以单独传递或者在元组中传递.以下是⽂档关于如何激活基本切⽚的说法:
Basic slicing occurs when obj is a slice object (constructed by start:stop:step notation inside of brackets), an integer, or a tuple of slice objects and integers. Ellipsis and newaxis objects can be interspersed with these as well. In order to remain backward compatible with a common usage in Numeric, basic slicing is also initiated if the selection object is any sequence (such as a list) containi
ng slice objects, the Ellipsis object, or the newaxis object, but no integer arrays or other embedded sequences.
传递numpy数组,仅包含整数或包含任何类型⼦序列的⾮元组序列或包含数组或⼦序列的元组时,将激活⾼级索引.
有关⾼级索引和基本切⽚如何不同的详细信息,请参阅⽂档(链接到上⾯).但在这种特殊情况下,我很清楚发⽣了什么.使⽤部分索引时,它与以下⾏为有关:
The rule for partial indexing is that the shape of the result (or the interpreted shape of the object to be used in setting) is the shape of x with the indexed subspace replaced with the broadcasted indexing subspace. If the index subspaces are right next to each other, then the broadcasted indexing space directly replaces all of the indexed subspaces in x. If the indexing subspaces are separated (by slice objects), then the broadcasted indexing space is first, followed by the sliced subspace of x.
在您使⽤⾼级索引的a的定义中,您有效地将序列[0,1]作为元组的第三项传递,并且由于没有⼴播发⽣(因为没有其他序列),所有内容都按预期发⽣.
在b的定义中,也使⽤⾼级索引,您可以有效地传递两个序列[0],第⼀个项⽬(转换为intp数组)和[0,1],第三个项⽬.这两个项⽬⼀起⼴播,结果与第三个项⽬具有相同的形状.然⽽,由于⼴播已经发⽣,我们⾯临⼀个问题:在新的形状元组中我们插⼊⼴播的形状?正如⽂档所说,
there is no unambiguous place to drop in the indexing subspace, thus it is tacked-on to the beginning.
因此,⼴播产⽣的2被移动到形状元组的开头,产⽣明显的转置.

本文发布于:2024-09-21 00:29:53,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/117654.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:元组   序列   数组   包含   传递   形状   请参阅   对象
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议