在numpy数组中沿轴求和 [英] Sum along axis in numpy array

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

问题描述

我想了解这个 ndarray.sum(axis=) 是如何工作的.我知道axis=0 用于列,axis=1 用于行.但是在 3 个维度(3 个轴)的情况下,很难解释以下结果.

arr = np.arange(0,30).reshape(2,3,5)阿尔出[1]:数组([[[ 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, 27, 28, 29]]])arr.sum(axis=0)出[2]:数组([[15, 17, 19, 21, 23],[25, 27, 29, 31, 33],[35, 37, 39, 41, 43]])arr.sum(轴=1)出[8]:数组([[15, 18, 21, 24, 27],[60, 63, 66, 69, 72]])arr.sum(axis=2)出[3]:数组([[ 10, 35, 60],[ 85, 110, 135]])

在这个形状为(2,3,5)的3轴阵列示例中,有3行5列.但是如果我从整体上看这个数组,似乎只有两行(都有 3 个数组元素).

谁能解释一下这个总和如何在 3 个或更多轴(维度)的数组上工作.

解决方案

如果要保留尺寸可以指定keepdims:

<预><代码>>>>arr = np.arange(0,30).reshape(2,3,5)>>>arr.sum(axis=0, keepdims=True)数组([[[15, 17, 19, 21, 23],[25, 27, 29, 31, 33],[35, 37, 39, 41, 43]]])

否则,您求和的轴将从形状中删除.跟踪此情况的一种简单方法是使用 numpy.ndarray.shape 属性:

<预><代码>>>>形状(2, 3, 5)>>>arr.sum(axis=0).shape(3, 5) # 第一个条目 (index = axis = 0) 维度被移除>>>arr.sum(axis=1).shape(2, 5) # 第二个条目 (index = axis = 1) 被移除

如果需要,您也可以沿多个轴求和(将维数减少指定轴的数量):

<预><代码>>>>arr.sum(axis=(0, 1))数组([75, 81, 87, 93, 99])>>>arr.sum(axis=(0, 1)).shape(5, ) # 第一个和第二个条目被删除

I want to understand how this ndarray.sum(axis=) works. I know that axis=0 is for columns and axis=1 is for rows. But in case of 3 dimensions(3 axes) its difficult to interpret below result.

arr = np.arange(0,30).reshape(2,3,5)

arr
Out[1]: 
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, 27, 28, 29]]])

arr.sum(axis=0)
Out[2]: 
array([[15, 17, 19, 21, 23],
       [25, 27, 29, 31, 33],
       [35, 37, 39, 41, 43]])


arr.sum(axis=1)
Out[8]: 
array([[15, 18, 21, 24, 27],
       [60, 63, 66, 69, 72]])

arr.sum(axis=2)
Out[3]: 
array([[ 10,  35,  60],
       [ 85, 110, 135]])

Here in this example of 3 axes array of shape(2,3,5), there are 3 rows and 5 columns. But if i look at this array as whole, seems like only two rows (both with 3 array elements).

Can anyone please explain how this sum works on array of 3 or more axes(dimensions).

解决方案

If you want to keep the dimensions you can specify keepdims:

>>> arr = np.arange(0,30).reshape(2,3,5)
>>> arr.sum(axis=0, keepdims=True)
array([[[15, 17, 19, 21, 23],
        [25, 27, 29, 31, 33],
        [35, 37, 39, 41, 43]]])

Otherwise the axis you sum along is removed from the shape. An easy way to keep track of this is using the numpy.ndarray.shape property:

>>> arr.shape
(2, 3, 5)

>>> arr.sum(axis=0).shape
(3, 5)  # the first entry (index = axis = 0) dimension was removed 

>>> arr.sum(axis=1).shape
(2, 5)  # the second entry (index = axis = 1) was removed

You can also sum along multiple axis if you want (reducing the dimensionality by the amount of specified axis):

>>> arr.sum(axis=(0, 1))
array([75, 81, 87, 93, 99])
>>> arr.sum(axis=(0, 1)).shape
(5, )  # first and second entry is removed

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

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