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

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

问题描述

使用 numpy 已经有一段时间了.然而,就在我认为我已经弄清楚了数组的时候,它又给了我另一条曲线.例如,我构造了 3D 数组 pltz,然后

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

这两种形状究竟为何不同?

解决方案

由于您的索引表达式中同时包含 : 和列表,因此 NumPy 需要同时应用基本和高级索引规则,他们互动的方式有点奇怪.相关文档是 此处,如果您想了解完整的详细信息,请咨询它.我将重点介绍导致这种形状不匹配的部分.

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

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

你的第一个表达就属于这种情况.

<小时>

另一方面,如果使用高级索引的索引表达式的组件由使用基本索引的组件分隔,则没有明确的位置可以插入高级索引维度.例如,与

arr[ind, :, ind]

结果需要有长度为 2、3、4 和 20 的维度,并且没有合适的地方来粘贴 20.

当高级索引组件被基本索引组件分开时,NumPy 会将高级索引产生的所有维度都粘贴在结果数组的开头.基本索引组件是 :...np.newaxis (None).你的第二个表达式属于这种情况.

<小时>

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

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?

解决方案

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.

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]

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

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天全站免登陆