numpy数组的形状 [英] Shapes of numpy arrays

查看:120
本文介绍了numpy数组的形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在开始与numpy一起工作了一段时间.不过,就在我认为自己想出数组的时候,它又给我带来了另一条曲线.例如,我构造3D阵列pltz,然后

Been working with numpy for a while now. Just when I think I have arrays figured out, though, it throws me another curve. For instance, I construct the 3D array pltz, and then

>>> gridset2 = range(0, pltx.shape[2], grdspc)
>>> pltz[10,:,gridset2].shape
(17, 160)
>>> pltz[10][:,gridset2].shape
(160, 17)

为什么在地球上这两种形状不同?

Why on Earth are the two shapes different?

推荐答案

由于索引表达式中同时包含:和列表,因此NumPy需要同时应用基本和高级索引规则以及它们之间的交互方式有点奇怪.相关文档为

Since your indexing expression has both a : and a list in it, NumPy needs to apply both the basic and advanced indexing rules, and the way they interact is kind of weird. The relevant documentation is here, and you should consult it if you want to know the full details. I'll focus on the part that causes this shape mismatch.

使用高级索引的索引表达式的所有组件彼此相邻时,来自高级索引的结果的维将放置在结果中它们替换的维的位置.高级索引组件类似于数组,例如数组,列表和标量.标量也可以在基本索引中使用,但是为此,它们被认为是高级的.因此,如果arr.shape == (10, 20, 30)ind.shape = (2, 3, 4),则

When all components of the indexing expression that use advanced indexing are next to each other, dimensions of the result coming from advanced indexing are placed into the result in the position of the dimensions they replace. Advanced indexing components are array-likes, such as arrays, lists, and scalars; scalars can also be used in basic indexing, but for this purpose, they're considered advanced. Thus, if arr.shape == (10, 20, 30) and ind.shape = (2, 3, 4), then

arr[:, ind, :].shape == (10, 2, 3, 4, 30)

您的第一个表达式属于这种情况.

Your first expression falls into this case.

另一方面,如果使用高级索引的索引表达式的组件被使用基本索引的组件分隔,则没有地方要插入高级索引维.例如,使用

On the other hand, if components of the indexing expression that use advanced indexing are separated by components that use basic indexing, there is no unambiguous place to insert the advanced indexing dimensions. For example, with

arr[ind, :, ind]

结果的尺寸必须为长度2、3、4和20,并且没有合适的位置贴上20.

the result needs to have dimensions of length 2, 3, 4, and 20, and there's no good place to stick the 20.

当高级索引编制组件与基本索引编制组件分开时,NumPy会将高级索引生成的所有维粘贴在结果数组的开始处.基本索引组成部分是:...np.newaxis(None).您的第二个表情属于这种情况.

When advanced indexing components are separated by basic indexing components, NumPy sticks all dimensions resulting from advanced indexing at the start of the result array. Basic indexing components are :, ..., and np.newaxis (None). Your second expression falls into this case.

由于第二个表达式具有由基本索引组件分隔的高级索引组件,而第一个表达式没有,因此两个表达式使用不同的索引规则.为避免这种情况,您可以将基本索引编制和高级索引编制分为两个阶段,也可以用等效的高级索引编制来替换基本索引编制.无论您做什么,我都建议在此类代码的上方加上解释性注释.

Since your second expression has advanced indexing components separated by basic indexing components and your first expression doesn't, your two expressions use different indexing rules. To avoid this, you could separate the basic indexing and advanced indexing into two stages, or you could replace the basic indexing with equivalent advanced indexing. Whatever you do, I recommend putting an explanatory comment above such code.

这篇关于numpy数组的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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