代码之家  ›  专栏  ›  技术社区  ›  youpilat13 Ty Petrice

python numpy-将子数组与4d数组合并,无法让swapaxes构建2d全局数组

  •  0
  • youpilat13 Ty Petrice  · 技术社区  · 6 年前

    我有一个8x8数组,分为2x2个块,所以我有16个子数组。四个维度是(4,4,2,2):第一个维度是块的行,第二个维度是列,第三个维度是子数组2x2的行索引,第四个维度是子数组2x2的列索引。

    全局数组的前2行是(8列中的2行):

    [3.28542331e+09 3.28542331e+09 0. 0. 0. 0. 0. 0]
    [0. 0. 2.60113771e+10 2.60113771e+10 5.12629421e+10 5.12629421e+10 8.49990653e+10 8.49990653e+10]
    

    我尝试从所有2x2块(总共16块)中获取8x8全局数组;我做到了:

    arrayFullCross.swapaxes(0,2).reshape(8,8)
    

    但这不管用。的确,第一行是正确的,但第二行不行。我得到的是:

    reshape =  [[3.28542331e+09 3.28542331e+09 0.00000000e+00 0.00000000e+00
      0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
     [0.00000000e+00 0.00000000e+00 2.60113771e+10 2.60113771e+10
      0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
     ...
    

    如您所见,值 5.12629421e+10 5.12629421e+10 8.49990653e+10 8.49990653e+10 不要出现在第二行。

    它们出现在第三行:

    [0.00000000e+00 0.00000000e+00 5.12629421e+10 5.12629421e+10
      1.01028455e+11 1.01028455e+11 0.00000000e+00 0.00000000e+00]
    

    相反,我想进入第二行:

     [[3.28542331e+09 3.28542331e+09 0.00000000e+00 0.00000000e+00
          0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
         [0.00000000e+00 0.00000000e+00 2.60113771e+10 2.60113771e+10
          5.12629421e+10 5.12629421e+10 8.49990653e+10 8.49990653e+10]
    

    如果有人能帮我从4d数组中构建一个2d8x8数组…

    编辑1 :以下是完整4D阵列的打印(通过执行 print 'arrayFullCross = ', arrayFullCross ):

    arrayFullCross =  [[[[3.28542331e+09 3.28542331e+09]
       [8.97951610e+07 8.97951610e+07]]
    
      [[0.00000000e+00 0.00000000e+00]
       [0.00000000e+00 0.00000000e+00]]
    
      [[0.00000000e+00 0.00000000e+00]
       [0.00000000e+00 0.00000000e+00]]
    
      [[0.00000000e+00 0.00000000e+00]
       [0.00000000e+00 0.00000000e+00]]]
    
    
     [[[0.00000000e+00 0.00000000e+00]
       [0.00000000e+00 0.00000000e+00]]
    
      [[2.60113771e+10 2.60113771e+10]
       [7.10926896e+08 7.10926896e+08]]
    
      [[5.12629421e+10 5.12629421e+10]
       [1.40108708e+09 1.40108708e+09]]
    
      [[8.49990653e+10 8.49990653e+10]
       [2.32314196e+09 2.32314196e+09]]]
    
    
     [[[0.00000000e+00 0.00000000e+00]
       [0.00000000e+00 0.00000000e+00]]
    
      [[0.00000000e+00 0.00000000e+00]
       [0.00000000e+00 0.00000000e+00]]
    
      [[1.01028455e+11 1.01028455e+11]
       [2.76124733e+09 2.76124733e+09]]
    
      [[1.67515243e+11 1.67515243e+11]
       [4.57842318e+09 4.57842318e+09]]]
    
    
     [[[0.00000000e+00 0.00000000e+00]
       [0.00000000e+00 0.00000000e+00]]
    
      [[0.00000000e+00 0.00000000e+00]
       [0.00000000e+00 0.00000000e+00]]
    
      [[0.00000000e+00 0.00000000e+00]
       [0.00000000e+00 0.00000000e+00]]
    
      [[1.38878482e+11 1.38878482e+11]
       [3.79574089e+09 3.79574089e+09]]]]
    

    编辑2 好的,我要检查整形是否做得好的方法是:

      print 'shape(arrayFull = ', np.shape(arrayFullCross)
    
      print 'here first line  , arrayFullCross column = 0 = ', arrayFullCross[0][0][0][0:2] 
      print 'here first line  , arrayFullCross column = 1 = ', arrayFullCross[0][1][0][0:2] 
      print 'here first line  , arrayFullCross column = 2 = ', arrayFullCross[0][2][0][0:2] 
      print 'here first line  , arrayFullCross column = 3 = ', arrayFullCross[0][3][0][0:2] 
      print ' '
      print 'here second line  , arrayFullCross column = 0 = ', arrayFullCross[1][0][0][0:2] 
      print 'here second line  , arrayFullCross column = 1 = ', arrayFullCross[1][1][0][0:2] 
      print 'here second line  , arrayFullCross column = 2 = ', arrayFullCross[1][2][0][0:2] 
      print 'here second line  , arrayFullCross column = 3 = ', arrayFullCross[1][3][0][0:2] 
      print ' '
      print 'test all  first line  , arrayFullCross column = 0,1,2,3 = ', arrayFullCross[0][0:4][0][0:2] 
      print ' '
      print 'here first line  , arrayFullCross column = 1 = ', arrayFullCross[0][1][0][0:2] 
      print 'here first line  , arrayFullCross column = 2 = ', arrayFullCross[0][2][0][0:2] 
      print 'here first line  , arrayFullCross column = 3 = ', arrayFullCross[0][3][0][0:2] 
    

    它给出:

    shape(arrayFull =  (4, 4, 2, 2)
    here first line  , arrayFullCross column = 0 =  [3.28542331e+09 3.28542331e+09]
    here first line  , arrayFullCross column = 1 =  [0. 0.]
    here first line  , arrayFullCross column = 2 =  [0. 0.]
    here first line  , arrayFullCross column = 3 =  [0. 0.]
    
    here second line  , arrayFullCross column = 0 =  [0. 0.]
    here second line  , arrayFullCross column = 1 =  [2.60113771e+10 2.60113771e+10]
    here second line  , arrayFullCross column = 2 =  [5.12629421e+10 5.12629421e+10]
    here second line  , arrayFullCross column = 3 =  [8.49990653e+10 8.49990653e+10]
    

    但我对这两行首行列标的印刷有疑问(第二行 index j 在里面 arrayFullCross[i][j][k][l] .

    不幸的是, 几乎解 具有 print 'reshape = ', arrayFullCross.swapaxes(2,0).reshape(8,8) 给予:

    reshape =  [[3.28542331e+09 3.28542331e+09 0.00000000e+00 0.00000000e+00
      0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
     [0.00000000e+00 0.00000000e+00 2.60113771e+10 2.60113771e+10
      0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
     [0.00000000e+00 0.00000000e+00 5.12629421e+10 5.12629421e+10
      1.01028455e+11 1.01028455e+11 0.00000000e+00 0.00000000e+00]
     [0.00000000e+00 0.00000000e+00 8.49990653e+10 8.49990653e+10
      1.67515243e+11 1.67515243e+11 1.38878482e+11 1.38878482e+11]
     [8.97951610e+07 8.97951610e+07 0.00000000e+00 0.00000000e+00
      0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
     [0.00000000e+00 0.00000000e+00 7.10926896e+08 7.10926896e+08
      0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00]
     [0.00000000e+00 0.00000000e+00 1.40108708e+09 1.40108708e+09
      2.76124733e+09 2.76124733e+09 0.00000000e+00 0.00000000e+00]
     [0.00000000e+00 0.00000000e+00 2.32314196e+09 2.32314196e+09
      4.57842318e+09 4.57842318e+09 3.79574089e+09 3.79574089e+09]]
    

    从我的打印结果来看,第二行应该等于:

    [0.00000000e+00 0.00000000e+00 2.60113771e+10 2.60113771e+10
          5.12629421e+10 5.12629421e+10 8.49990653e+10 8.49990653e+10]
    

    是否可以多次使用swapaxes?

    当做

    1 回复  |  直到 6 年前
        1
  •  1
  •   hpaulj    6 年前

    我想我的评论还不够清楚。

    In [811]: arr = np.ones((4,4,2,2),int)
    In [812]: arr.swapaxes(0,2).shape
    Out[812]: (2, 4, 4, 2)
    

    是的,这可以被重塑为(8,8),但肯定会有某种转换,因为一对维度是(2,4),另一对是(4,2)。

    如果您交换了轴以生成(2,4,2,4)或(4,2,4,2),我希望重新整形是正确的。

    哪个交换是正确的,具体细节取决于您希望如何划分子块。希望你能追踪到这些?


    用尼斯(2,2)块制作一个简单的数组:

    In [813]: arr = np.arange(4).reshape(2,2)
    In [815]: arr1 =np.tile(arr[None,None,:,:],(4,4,1,1))
    In [816]: arr1.shape
    Out[816]: (4, 4, 2, 2)
    
    In [817]: arr1
    Out[817]: 
    array([[[[0, 1],
             [2, 3]],
    
            [[0, 1],
             [2, 3]],
       ...
    

    看看不同的掉期产生了什么:

    In [822]: arr1.swapaxes(0,2).reshape(8,8)
    Out[822]: 
    array([[0, 1, 0, 1, 0, 1, 0, 1],
           [0, 1, 0, 1, 0, 1, 0, 1],
           [0, 1, 0, 1, 0, 1, 0, 1],
           [0, 1, 0, 1, 0, 1, 0, 1],
           [2, 3, 2, 3, 2, 3, 2, 3],
           [2, 3, 2, 3, 2, 3, 2, 3],
           [2, 3, 2, 3, 2, 3, 2, 3],
           [2, 3, 2, 3, 2, 3, 2, 3]])
    In [823]: 
    In [823]: arr1.swapaxes(1,3).reshape(8,8)
    Out[823]: 
    array([[0, 0, 0, 0, 2, 2, 2, 2],
           [1, 1, 1, 1, 3, 3, 3, 3],
           [0, 0, 0, 0, 2, 2, 2, 2],
           [1, 1, 1, 1, 3, 3, 3, 3],
           [0, 0, 0, 0, 2, 2, 2, 2],
           [1, 1, 1, 1, 3, 3, 3, 3],
           [0, 0, 0, 0, 2, 2, 2, 2],
           [1, 1, 1, 1, 3, 3, 3, 3]])
    In [824]: arr1.swapaxes(1,2).reshape(8,8)
    Out[824]: 
    array([[0, 1, 0, 1, 0, 1, 0, 1],
           [2, 3, 2, 3, 2, 3, 2, 3],
           [0, 1, 0, 1, 0, 1, 0, 1],
           [2, 3, 2, 3, 2, 3, 2, 3],
           [0, 1, 0, 1, 0, 1, 0, 1],
           [2, 3, 2, 3, 2, 3, 2, 3],
           [0, 1, 0, 1, 0, 1, 0, 1],
           [2, 3, 2, 3, 2, 3, 2, 3]])
    

    工作时产生(4,2,4,2)形状:

    In [825]: arr1.swapaxes(0,2).shape
    Out[825]: (2, 4, 4, 2)
    In [826]: arr1.swapaxes(1,3).shape
    Out[826]: (4, 2, 2, 4)
    In [827]: arr1.swapaxes(1,2).shape
    Out[827]: (4, 2, 4, 2)
    

    还有另一个交换

    In [829]: arr1.swapaxes(0,3).shape
    Out[829]: (2, 4, 2, 4)
    In [830]: arr1.swapaxes(0,3).reshape(8,8)
    Out[830]: 
    array([[0, 0, 0, 0, 2, 2, 2, 2],
           [0, 0, 0, 0, 2, 2, 2, 2],
           [0, 0, 0, 0, 2, 2, 2, 2],
           [0, 0, 0, 0, 2, 2, 2, 2],
           [1, 1, 1, 1, 3, 3, 3, 3],
           [1, 1, 1, 1, 3, 3, 3, 3],
           [1, 1, 1, 1, 3, 3, 3, 3],
           [1, 1, 1, 1, 3, 3, 3, 3]])