NumPy常见运算之min、max、mean、sum、exp、sqrt、sort、乘法、点积、拼接、切分

基本运算

  • ndarray.min() / np.min(ndarray)
  • ndarray.max() / np.max(ndarray)
  • ndarray.mean() / np.mean(ndarray)
  • ndarray.sum() / np.sum(ndarray)
  • ndarray.argmax() 最大值的索引
  • np.exp()
  • np.sqrt() 开根号
  • np.sort() 排序
  • np.argsort() 排序的索引

def numpy():
    n = np.array([[5, 10, 15],
                  [20, 25, 30],
                  [35, 40, 45]])
    print(n.min())  # 5
    print(n.max())  # 45
    print(n.mean())  # 25.0
    print(n.sum())  # 225
    # 指定所操作的维度,axis=0 按列,axis=1 按行
    print(n.sum(axis=0))  # [60 75 90]
    print(n.sum(axis=1))  # [ 30  75 120]

    print('- ' * 20)
    n1 = np.arange(3)
    print(n1)  # [0 1 2]
    print(np.exp(n1))  # [1.         2.71828183 7.3890561 ]
    print(np.sqrt(n1))  # [0.         1.         1.41421356]

    print('- ' * 20)
    n2 = np.random.random((2, 3)) * 10
    n3 = np.floor(n2)
    print(n2)
    '''
    [[8.99880764 4.00463427 0.419855  ]
    [6.25824191 3.02688928 3.98404113]]
    '''
    print(n3)
    '''
    [[8. 4. 0.]
    [6. 3. 3.]]
    '''

    print('- ' * 20)
    a = np.array([[4, 3, 5], [1, 6, 1]])
    print(a)
    a1 = np.sort(a, axis=1)
    print(a1)
    '''
    [[3 4 5]
    [1 1 6]]
    '''
    a.sort(axis=1)
    print(a)
    '''
    [[3 4 5]
    [1 1 6]]
    '''

    print('- ' * 20)
    b = np.array([4, 3, 1, 2])
    i = np.argsort(b)
    print(i)  # [2 3 1 0]
    print(b[i])  # [1 2 3 4]

矩阵运算

(1)矩阵乘法

  • ndarray1 * ndarray2
  • ndarray1.dot(ndarray2)
  • np.dot(ndarray1, ndarray2)

(2)拼接 ndarray 对象

  • np.hstack() # 横着拼接
  • np.vstack() # 竖着拼接

(3)切分 ndarray 对象

  • np.hsplit() # 横着切
  • np.vsplit() # 竖着切
def numpyMatrix():
    n1 = np.array([[1, 1],
                   [0, 1]])
    n2 = np.array([[2, 0],
                   [3, 4]])
    print(n1 * n2)
    '''
    [[2 0]
    [0 4]]
    '''
    print(n1.dot(n2))
    '''
    [[5 4]
    [3 4]]
    '''
    print(np.dot(n1, n2))
    '''
    [[5 4]
    [3 4]]
    '''
    n1 = np.array([[1, 2],
                   [3, 4]])
    n2 = np.array([[5, 6],
                   [7, 8]])

    hstack = np.hstack((n1, n2))  # 横着拼接
    print(hstack)
    '''
    [[1 2 5 6]
    [3 4 7 8]]
    '''
    vstack = np.vstack((n1, n2))  # 竖着拼接
    print(vstack)
    '''
    [[1 2]
     [3 4]
     [5 6]
     [7 8]]
    '''

    print('- ' * 20)
    a = np.floor(10 * np.random.random((2, 12)))
    print(a)
    '''
    [[9. 8. 4. 4. 5. 1. 2. 0. 7. 0. 7. 8.]
    [9. 4. 3. 5. 5. 5. 0. 4. 9. 1. 2. 3.]]
    '''
    print(np.hsplit(a, 3))  # 横着切
    '''
    [array([[9., 8., 4., 4.],
           [9., 4., 3., 5.]]), array([[5., 1., 2., 0.],
           [5., 5., 0., 4.]]), array([[7., 0., 7., 8.],
           [9., 1., 2., 3.]])]
    '''
    print(np.hsplit(a, (3, 5)))  # 指定切分位置
    '''
    [array([[9., 8., 4.],
           [9., 4., 3.]]), array([[4., 5.],
           [5., 5.]]), array([[1., 2., 0., 7., 0., 7., 8.],
           [5., 0., 4., 9., 1., 2., 3.]])]
    '''
    b = a.T
    print(b)
    '''
    [[9. 9.]
     [8. 4.]
     [4. 3.]
     [4. 5.]
     [5. 5.]
     [1. 5.]
     [2. 0.]
     [0. 4.]
     [7. 9.]
     [0. 1.]
     [7. 2.]
     [8. 3.]]
    '''
    print(np.vsplit(b, 3))  # 竖着切
    '''
    [array([[9., 9.],
           [8., 4.],
           [4., 3.],
           [4., 5.]]), array([[5., 5.],
           [1., 5.],
           [2., 0.],
           [0., 4.]]), array([[7., 9.],
           [0., 1.],
           [7., 2.],
           [8., 3.]])]
    '''

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/04/01/common-operations-of-numpy-include-min-max-mean-sum-exp-sqrt-sort-multiplication-dot-product-concatenation-and-segmentation/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
NumPy常见运算之min、max、mean、sum、exp、sqrt、sort、乘法、点积、拼接、切分
基本运算 ndarray.min() / np.min(ndarray) ndarray.max() / np.max(ndarray) ndarray.mean() / np.mean(ndarray) ndarray.sum() / np.sum(ndarray) ndarray.……
<<上一篇
下一篇>>
文章目录
关闭
目 录