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

查看:86
本文介绍了如何在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.

例如,如果2D数组a的形状为(5,6),则最多可以访问a[0,0].因此,轴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天全站免登陆