python numpy sun() 的axis用法

python 数学计算模块


numpy.sum()的axis参数, 英文解释

None or int or tuple of ints, optional

Axis or axes along which a sum is performed. The default, axis=None, will sum all of the elements of the input array. If axis is negative it counts from the last to the first axis.

New in version 1.7.0.

If axis is a tuple of ints, a sum is performed on all of the axes specified in the tuple instead of a single axis or all the axes as before.


不过怎么感觉怎么看都不太明白这里的axis和axes是啥意思, 不是学数学的,真实太吃亏了。后悔当初线性代数没有好好学

自己好好总结一些,自己来理解吧。

axis参数可以传一个整数,也可以传一个矩阵。传整数是对应维度内所有元素相加, 比如:axis=0, 既0维里面所有元素相加, 这个可借鉴numpy.shape()方法中给出的结果

例子:

>>> z
array([[[[ 1,  2,  3,  4]],


        [[ 1,  2,  3,  4]],


        [[ 1,  2,  3,  4]]],




       [[[ 7,  8,  9, 10]],


        [[ 7,  8,  9, 10]],


        [[ 7,  8,  9, 10]]],




       [[[13, 14, 15, 16]],


        [[13, 14, 15, 16]],


        [[13, 14, 15, 16]]],




       [[[19, 20, 21, 22]],


        [[19, 20, 21, 22]],


        [[19, 20, 21, 22]]],




       [[[25, 26, 27, 28]],


        [[25, 26, 27, 28]],


        [[25, 26, 27, 28]]],




       [[[31, 32, 33, 34]],


        [[31, 32, 33, 34]],


        [[31, 32, 33, 34]]],




       [[[ 1,  2,  3,  4]],


        [[ 1,  2,  3,  4]],


        [[ 1,  2,  3,  4]]],




       [[[ 7,  8,  9, 10]],


        [[ 7,  8,  9, 10]],


        [[ 7,  8,  9, 10]]],




       [[[13, 14, 15, 16]],


        [[13, 14, 15, 16]],


        [[13, 14, 15, 16]]],




       [[[19, 20, 21, 22]],


        [[19, 20, 21, 22]],


        [[19, 20, 21, 22]]],




       [[[25, 26, 27, 28]],


        [[25, 26, 27, 28]],


        [[25, 26, 27, 28]]],




       [[[31, 32, 33, 34]],


        [[31, 32, 33, 34]],


        [[31, 32, 33, 34]]]])



>>> np.sum(z, axis=0)
array([[[192, 204, 216, 228]],


       [[192, 204, 216, 228]],


       [[192, 204, 216, 228]]])


>>> np.sum(z, axis=1)
array([[[  3,   6,   9,  12]],


       [[ 21,  24,  27,  30]],


       [[ 39,  42,  45,  48]],


       [[ 57,  60,  63,  66]],


       [[ 75,  78,  81,  84]],


       [[ 93,  96,  99, 102]],


       [[  3,   6,   9,  12]],


       [[ 21,  24,  27,  30]],


       [[ 39,  42,  45,  48]],


       [[ 57,  60,  63,  66]],


       [[ 75,  78,  81,  84]],


       [[ 93,  96,  99, 102]]])


>>> np.sum(z, axis=2)
array([[[ 1,  2,  3,  4],
        [ 1,  2,  3,  4],
        [ 1,  2,  3,  4]],


       [[ 7,  8,  9, 10],
        [ 7,  8,  9, 10],
        [ 7,  8,  9, 10]],


       [[13, 14, 15, 16],
        [13, 14, 15, 16],
        [13, 14, 15, 16]],


       [[19, 20, 21, 22],
        [19, 20, 21, 22],
        [19, 20, 21, 22]],


       [[25, 26, 27, 28],
        [25, 26, 27, 28],
        [25, 26, 27, 28]],


       [[31, 32, 33, 34],
        [31, 32, 33, 34],
        [31, 32, 33, 34]],


       [[ 1,  2,  3,  4],
        [ 1,  2,  3,  4],
        [ 1,  2,  3,  4]],


       [[ 7,  8,  9, 10],
        [ 7,  8,  9, 10],
        [ 7,  8,  9, 10]],


       [[13, 14, 15, 16],
        [13, 14, 15, 16],
        [13, 14, 15, 16]],


       [[19, 20, 21, 22],
        [19, 20, 21, 22],
        [19, 20, 21, 22]],


       [[25, 26, 27, 28],
        [25, 26, 27, 28],
        [25, 26, 27, 28]],


       [[31, 32, 33, 34],
        [31, 32, 33, 34],
        [31, 32, 33, 34]]])
>>> np.sum(z, axis=3)
array([[[ 10],
        [ 10],
        [ 10]],


       [[ 34],
        [ 34],
        [ 34]],


你可能感兴趣的:(python,sum)