Python numpy 数组中的轴是如何索引的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17079279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how is axis indexed in numpy's array?
提问by Alcott
From Numpy's tutorial, axis can be indexed with integers, like 0is for column, 1is 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?
从Numpy's tutorial,轴可以用整数索引,就像0是列,1是行,但我不明白为什么以这种方式索引它们?以及如何在处理多维数组时找出每个轴的索引?
采纳答案by nneonneo
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.
根据定义,维度的轴号是该维度在数组的shape. 它也是索引期间用于访问该维度的位置。
For example, if a 2D array ahas 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.
例如,如果2D阵列a具有形状(5,6),则可以访问a[0,0]到a[4,5]。因此,轴 0 是第一个维度(“行”),轴 1 是第二个维度(“列”)。在更高的维度中,“行”和“列”不再有意义,请尝试根据所涉及的形状和索引来考虑轴。
If you do .sum(axis=n), for example, then dimension nis collapsed and deleted, with each value in the new matrix equal to the sum of the corresponding collapsed values. For example, if bhas 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].
.sum(axis=n)例如,如果您这样做,则维度n会折叠并删除,新矩阵中的每个值都等于相应折叠值的总和。例如,如果b有 shape (5,6,7,8),而您有c = b.sum(axis=2),那么轴 2(尺寸为 7 的维度)会折叠,结果为 shape (5,6,8)。此外,c[x,y,z]等于所有元素的总和b[x,y,:,z]。
回答by ndas
In general, axis = 0, means all cells with first dimension varying with each value of 2nd dimension and 3rd dimension and so on
一般来说,axis = 0,表示所有具有第一维的单元格随第二维和第三维的每个值而变化,依此类推
For example , 2-dimensional array has two corresponding axes: the first running vertically downwards across rows (axis 0), and the second running horizontally across columns (axis 1)
例如,二维数组有两个对应的轴:第一个垂直向下跨行(轴 0),第二个水平跨列(轴 1)
For 3D, it becomes complex, so, use multiple for loops
对于 3D,它变得复杂,因此,使用多个 for 循环
>>> x = np.array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
>>> x.shape #(3, 3, 3)
#axis = 0
>>> for j in range(0, x.shape[1]):
for k in range(0, x.shape[2]):
print( "element = ", (j,k), " ", [ x[i,j,k] for i in range(0, x.shape[0]) ])
...
element = (0, 0) [0, 9, 18] #sum is 27
element = (0, 1) [1, 10, 19] #sum is 30
element = (0, 2) [2, 11, 20]
element = (1, 0) [3, 12, 21]
element = (1, 1) [4, 13, 22]
element = (1, 2) [5, 14, 23]
element = (2, 0) [6, 15, 24]
element = (2, 1) [7, 16, 25]
element = (2, 2) [8, 17, 26]
>>> x.sum(axis=0)
array([[27, 30, 33],
[36, 39, 42],
[45, 48, 51]])
#axis = 1
for i in range(0, x.shape[0]):
for k in range(0, x.shape[2]):
print( "element = ", (i,k), " ", [ x[i,j,k] for j in range(0, x.shape[1]) ])
element = (0, 0) [0, 3, 6] #sum is 9
element = (0, 1) [1, 4, 7]
element = (0, 2) [2, 5, 8]
element = (1, 0) [9, 12, 15]
element = (1, 1) [10, 13, 16]
element = (1, 2) [11, 14, 17]
element = (2, 0) [18, 21, 24]
element = (2, 1) [19, 22, 25]
element = (2, 2) [20, 23, 26]
# for sum, axis is the first keyword, so we may omit it,
>>> x.sum(0), x.sum(1), x.sum(2)
(array([[27, 30, 33],
[36, 39, 42],
[45, 48, 51]]),
array([[ 9, 12, 15],
[36, 39, 42],
[63, 66, 69]]),
array([[ 3, 12, 21],
[30, 39, 48],
[57, 66, 75]]))
回答by Lyn
You can grasp axis in this way:
您可以通过这种方式掌握轴:
>>> a = np.array([[[1,2,3],[2,2,3]],[[2,4,5],[1,3,6]],[[1,2,4],[2,3,4]],[[1,2,4],[1,2,6]]])
array([[[1, 2, 3],
[2, 2, 3]],
[[2, 4, 5],
[1, 3, 6]],
[[1, 2, 4],
[2, 3, 4]],
[[1, 2, 4],
[1, 2, 6]]])
>>> a.shape
(4,2,3)
I created an array of a shape with different values(4,2,3)so that you can tell the structure clearly. Different axis means different 'layer'.
我创建了一个具有不同值的形状数组,(4,2,3)以便您可以清楚地分辨结构。不同的轴意味着不同的“层”。
That is, axis = 0index the first dimension of shape (4,2,3). It refers to the arrays in the first []. There are 4 elements in it, so its shape is 4:
也就是说,axis = 0索引 shape 的第一个维度(4,2,3)。它指的是第一个[]. 里面有 4 个元素,所以它的形状是 4:
array[[1, 2, 3],
[2, 2, 3]],
array[[2, 4, 5],
[1, 3, 6]],
array[[1, 2, 4],
[2, 3, 4]],
array[[1, 2, 4],
[1, 2, 6]]
axis = 1index the second dimension in shape(4,2,3). There are 2 elements in each array of the layer: axis = 0,e.c. In the array of
axis = 1在 shape 中索引第二个维度(4,2,3)。该层的每个数组中有2个元素:axis = 0,ec 的数组中
array[[1, 2, 3],
[2, 2, 3]]
. The two elements are:
. 这两个要素是:
array[1, 2, 3]
array[2, 2, 3]
And the third shape value means there are 3 elements in each array element of layer: axis = 2. e.c. There are 3 elements in array[1, 2, 3]. That is explicit.
第三个形状值表示 layer 的每个数组元素中有 3 个元素:axis = 2。ec 中有 3 个元素array[1, 2, 3]。那是明确的。
And also, you can tell the axis/dimensions from the number of []at the beginning or in the end. In this case, the number is 3([[[), so you can choose axisfrom axis = 0, axis = 1and axis = 2.
而且,您可以从[]开头或结尾的编号来判断轴/尺寸。在本例中,数字为 3( [[[),因此您可以axis从axis = 0、axis = 1和 中进行选择axis = 2。


