zoukankan      html  css  js  c++  java
  • numpy运算简介(二)

    numpy.sum

    d = np.array([
        [1, 2, 1],
        [3, 0, 2]
    ])
    print(d.shape)  # (2, 3)

    axis的参数不能超过数组的维度,用来压缩其表示的维度,从下面的代码可以和明显看出其运算原理

    print(np.sum(d))  # 1+3+2+0+1+2 = 9
    print(np.sum(d, axis=0))  # [1+3, 2+0, 1+2] = [4, 2, 3]
    print(np.sum(d, axis=1))  # [1+2+1, 3+0+2] = [4, 5]

    再来个三维数组

    c = np.array([
        [
            [2, 3, 1],
            [4, 1, 0]
        ],
        [
            [0, 3, 1],
            [0, 1, 0]
        ]
    ])
    print(c.shape)  # (2, 2, 3)
    print(np.sum(c))  # 2+3+1+4+1+0 + 0+3+1+0+1+0=16
    print(np.sum(c, axis=0))  # [[2, 3, 1], [4, 1, 0]] + [[0, 3, 1], [0, 1, 0]] = [[2, 6, 2], [4, 2, 0]]
    print(np.sum(c, axis=1))  # [[2, 3, 1] + [4, 1, 0]] + [[0, 3, 1] + [0, 1, 0]] = [[6, 4, 1], [0, 4, 1]]
    print(np.sum(c, axis=2))  # [[2+3+1, 4+1+0],[0+3+1, 0+1+0]] = [[6, 5], [4, 1]]

    np.max、np.min、np.mean等同理 (以2维数组为例)

    print(np.max(d))  # 3
    print(np.max(d, axis=0))  # [3, 2,  2]
    print(np.max(d, axis=1))  # [2, 3]
    
    print(np.min(d))  # 0
    print(np.min(d, axis=0))  # [1, 0, 1]
    print(np.min(d, axis=1))  # [1, 0]
    
    print(np.mean(d))  # 1.5
    print(np.mean(d, axis=0))  # [2.  1.  1.5]
    print(np.mean(d, axis=1))  # [1.33333333 1.66666667]
  • 相关阅读:
    深入理解Mysql——锁、事务与并发控制
    深入理解Mysql——锁、事务与并发控制
    vs code中Vue代码格式化的问题
    Vue中的父子传值问题
    用画布canvas画安卓logo
    用画布canvas画安卓logo
    用画布canvas画安卓logo
    用画布canvas画安卓logo
    软中断
    软中断
  • 原文地址:https://www.cnblogs.com/answerThe/p/11369030.html
Copyright © 2011-2022 走看看