numpy多维数组中的轴 [英] Axis in numpy multidimensional array
本文介绍了numpy多维数组中的轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我还没有理解numpy中多维数组中轴之间的差异.你能解释一下吗? 我特别想知道numpy三维数组中的axis0,axis1和axis2在哪里. 又为什么呢? 谢谢.
I have not understud the difference between axis in a multidimensional array in numpy. Can you explain me? In particular I would like to know where are axis0, axis1 and axis2 in a numpy tridimensional array. And why? Thanks.
推荐答案
最简单的方法是使用示例:
The easiest way is with an example:
In [8]: x = np.array([[1, 2, 3], [4,5,6],[7,8,9]], np.int32)
In [9]: x
Out[9]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], dtype=int32)
In [10]: x.sum(axis=0) # sum the columns [1,4,7] = 12, [2,5,8] = 15 [3,6,9] = 18
Out[10]: array([12, 15, 18])
In [11]: x.sum(axis=1) # sum the rows [1,2,3] = 6, [4,5,6] = 15 [7,8,9] = 24
Out[11]: array([ 6, 15, 24])
轴0 是列,而轴1 是行.
在三维数组中:
In [26]: x = np.array((((1,2), (3,4) ), ((5,6),(7,8))))
In [27]: x
Out[27]:
array([[[1, 2],
[3, 4]],
[[5, 6],
[7, 8]]])
In [28]: x.shape # dimensions of the array
Out[28]: (2, 2, 2)
In [29]: x.sum(axis=0)
Out[29]:
array([[ 6, 8], # [1,5] = 6 [2,6] = 8 [3,7] = 10 [4, 8] = 12
[10, 12]])
In [31]: x.sum(axis=1)
Out[31]:
array([[ 4, 6], # [1,3] = 4 [2,4] = 6 [5, 7] = 12 [6, 8] = 14
[12, 14]])
In [33]: x.sum(axis=2) # [1, 2] = 3 [3, 4] = 7 [5, 6] = 11 [7, 8] = 15
Out[33]:
array([[ 3, 7],
[11, 15]])
In [77]: x.ndim # number of dimensions of the array
Out[77]: 3
链接有关使用多维数据数组的好教程
Link for a good tutorial on using multidimensional data arrays
这篇关于numpy多维数组中的轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文