• numpy中的拼接、堆叠、差分


    #拼接
    import
    numpy as np a = np.arange(1,25).reshape(2,3,4) b = np.arange(101,125).reshape(2,3,4) print('axis = 0') c = np.concatenate((a,b), axis = 0) print(c) print(c.shape) print('axis = 1') c = np.concatenate((a,b), axis = 1) print(c) print(c.shape) c = np.concatenate((a,b), axis = 2) print(c) print(c.shape) 输出 axis = 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]] [[101 102 103 104] [105 106 107 108] [109 110 111 112]] [[113 114 115 116] [117 118 119 120] [121 122 123 124]]] (4, 3, 4) axis = 1 [[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12] [101 102 103 104] [105 106 107 108] [109 110 111 112]] [[ 13 14 15 16] [ 17 18 19 20] [ 21 22 23 24] [113 114 115 116] [117 118 119 120] [121 122 123 124]]] (2, 6, 4) [[[ 1 2 3 4 101 102 103 104] [ 5 6 7 8 105 106 107 108] [ 9 10 11 12 109 110 111 112]] [[ 13 14 15 16 113 114 115 116] [ 17 18 19 20 117 118 119 120] [ 21 22 23 24 121 122 123 124]]] (2, 3, 8)
    #堆叠
    
    # 数组堆叠
    #Vstack最高维增加
    #hstack最低维添加
    a = np.arange(5)    # a为一维数组,5个元素
    b = np.arange(5,9) # b为一维数组,4个元素
    ar1 = np.hstack((a,b))  # 注意:((a,b)),这里形状可以不一样
    print(a,a.shape)
    print(b,b.shape)
    print(ar1,ar1.shape)
    a = np.array([[1],[2],[3]])   # a为二维数组,3行1列
    b = np.array([['a'],['b'],['c']])  # b为二维数组,3行1列
    ar2 = np.hstack((a,b))  # 注意:((a,b)),这里形状必须一样
    print(a,a.shape)
    print(b,b.shape)
    print(ar2,ar2.shape)
    print('-----')
    # numpy.hstack(tup):水平(按列顺序)堆叠数组
    
    a = np.arange(5)    
    b = np.arange(5,10)
    ar1 = np.vstack((a,b))
    print(a,a.shape)
    print(b,b.shape)
    print(ar1,ar1.shape)
    a = np.array([[1],[2],[3]])   
    b = np.array([['a'],['b'],['c'],['d']])   
    ar2 = np.vstack((a,b))  # 这里形状可以不一样
    print(a,a.shape)
    print(b,b.shape)
    print(ar2,ar2.shape)
    print('-----')
    # numpy.vstack(tup):垂直(按列顺序)堆叠数组
    
    a = np.arange(5)    
    b = np.arange(5,10)
    ar1 = np.stack((a,b))
    ar2 = np.stack((a,b),axis = 1)
    print(a,a.shape)
    print(b,b.shape)
    print(ar1,ar1.shape)
    print(ar2,ar2.shape)
    # numpy.stack(arrays, axis=0):沿着新轴连接数组的序列,形状必须一样!
    # 重点解释axis参数的意思,假设两个数组[1 2 3]和[4 5 6],shape均为(3,0)
    # axis=0:[[1 2 3] [4 5 6]],shape为(2,3)
    # axis=1:[[1 4] [2 5] [3 6]],shape为(3,2)
    #拆分
    import numpy as np
    a = np.arange(1,37).reshape(3,3,4)
    print(a)
    print('-'*50)
    
    print(np.split(a,(1,2),axis = 0))
    print('axis=0')
    print('-'*50)
    axis=0等价于vsplit
    print(np.split(a,(1,2),axis = 1))
    print('axis=1')
    print('-'*50)
    axis=1等价于hsplit
    print(np.split(a,(1,2),axis =2))
    print('axis=2')
    axis = 2等价于dsplit
    
    F:anaconda3az2python.exe G:/0work_study/3deep/学习资料/sxt/numpy代码/ex.py
    [[[ 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 30 31 32]
      [33 34 35 36]]]
    --------------------------------------------------
    [array([[[ 1,  2,  3,  4],
            [ 5,  6,  7,  8],
            [ 9, 10, 11, 12]]]),
     array([[[13, 14, 15, 16],
            [17, 18, 19, 20],
            [21, 22, 23, 24]]]), 
    array([[[25, 26, 27, 28],
            [29, 30, 31, 32],
            [33, 34, 35, 36]]])]
    axis=0
    --------------------------------------------------
    [array([[[ 1,  2,  3,  4]],
    
           [[13, 14, 15, 16]],
    
           [[25, 26, 27, 28]]]), 
    array([[[ 5,  6,  7,  8]],
    
           [[17, 18, 19, 20]],
    
           [[29, 30, 31, 32]]]), 
    array([[[ 9, 10, 11, 12]],
    
           [[21, 22, 23, 24]],
    
           [[33, 34, 35, 36]]])]
    axis=1
    --------------------------------------------------
    [array([[[ 1],
            [ 5],
            [ 9]],
    
           [[13],
            [17],
            [21]],
    
           [[25],
            [29],
            [33]]]), 
    array([[[ 2],
            [ 6],
            [10]],
    
           [[14],
            [18],
            [22]],
    
           [[26],
            [30],
            [34]]]), 
    array([[[ 3,  4],
            [ 7,  8],
            [11, 12]],
    
           [[15, 16],
            [19, 20],
            [23, 24]],
    
           [[27, 28],
            [31, 32],
            [35, 36]]])]
    axis=2
    
    Process finished with exit code 0
  • 相关阅读:
    Windows 2003安全配置最佳方法(转)
    令人十分怨念的tomcat注册成windows服务(转)
    AMR开源编码jrtplib中与int系统重定义问题解决
    由系统熵转移的思考
    此男因为什么被送进医院?
    [转]风水师的郁闷
    飞盘奇门局例我能顺利办好护照拿到签证出国参加会议吗?
    最近工作方面发生了一些大事情,所以特地为此摇了一挂,请高手进来断一断。
    概率面前的错误
    杜新会一个精彩占例之反推
  • 原文地址:https://www.cnblogs.com/yunshangyue71/p/13584316.html
Copyright © 2020-2023  润新知