numpy 数组中的轴是如何索引的? [英] how is axis indexed in numpy's array?

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

问题描述

来自 Numpy 的教程,轴可以用整数索引,如 0 用于列,1 用于行,但我不明白为什么它们以这种方式编入索引?以及如何在处理多维数组时找出每个轴的索引?

From Numpy's tutorial, axis can be indexed with integers, like 0 is for column, 1 is for row, but I don't grasp why they are indexed this way? And How do I figure out each axis' index when coping with multidimensional array?

推荐答案

根据定义,维度的轴号是该维度在数组shape中的索引.它也是索引期间用于访问该维度的位置.

By definition, the axis number of the dimension is the index of that dimension within the array's shape. It is also the position used to access that dimension during indexing.

例如,如果二维数组 a 的形状为 (5,6),那么您可以访问 a[0,0]a[4,5].因此,轴 0 是第一个维度(行"),轴 1 是第二个维度(列").在更高维度中,行"和列"不再有意义,请尝试根据所涉及的形状和索引来考虑轴.

For example, if a 2D array a has shape (5,6), then you can access a[0,0] up to a[4,5]. Axis 0 is thus the first dimension (the "rows"), and axis 1 is the second dimension (the "columns"). In higher dimensions, where "row" and "column" stop really making sense, try to think of the axes in terms of the shapes and indices involved.

例如,如果您执行 .sum(axis=n),则维度 n 会折叠并删除,新矩阵中的每个值都等于总和对应的折叠值.例如,如果 b 具有形状 (5,6,7,8),并且您执行 c = b.sum(axis=2),然后轴 2(尺寸为 7 的维度)折叠,结果具有形状 (5,6,8).此外,c[x,y,z] 等于所有元素b[x,y,:,z]的总和.

If you do .sum(axis=n), for example, then dimension n is collapsed and deleted, with each value in the new matrix equal to the sum of the corresponding collapsed values. For example, if b has shape (5,6,7,8), and you do c = b.sum(axis=2), then axis 2 (dimension with size 7) is collapsed, and the result has shape (5,6,8). Furthermore, c[x,y,z] is equal to the sum of all elements b[x,y,:,z].

这篇关于numpy 数组中的轴是如何索引的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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