python 多维数组轴_numpy多维数组中的轴

最简单的方法是举个例子: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学习如何使用多维数据数组

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值