简体   繁体   中英

NumPy Stack multidimensional arrays

I have 3 nD arrays as follows

x = [[1, 2, 3],  
     [4, 5, 6],  
     [7, 8, 9]]    


y = [[10, 11, 12],
     [13, 14, 15],
     [16, 17, 18]]

z = [[ 19,  20,  21],
     [ 22,  23,  24],
     [ 25,  26,  27]]

Without using for loop, I am trying to append every 2x2 matrix elements together such that

a1 = [[1,2]
      [4,5]]

a2 = [[10,11], 
      [13,14]]

a3 = [[19,20],
      [22,23]]

should append to

a = [[1,10,19],[2,11,20],[4,13,22],[5,14,23]]

Please note, the NxN matrix will always be N = j - 1 where j is x.shape(i,j)

Similarly for other 2x2 matrices, the arrays are as follows

b = [[2,11,20],[3,12,21],[5,14,23],[6,15,24]]
c = [[4,13,22],[5,14,23],[7,16,25],[8,17,26]]
d = [[5,14,23],[6,15,24],[8,17,26],[9,18,27]]

For large datasets, the for loop impacts runtime so I am trying to see if there is a way using NumPy stacking techniques

Your 3 arrays:

In [46]: x=np.arange(1,10).reshape(3,3)                                                                         
In [48]: y=np.arange(10,19).reshape(3,3)                                                                        
In [49]: z=np.arange(19,28).reshape(3,3)                                                                        

combined into one:

In [50]: xyz=np.stack((x,y,z))                                                                                  
In [51]: xyz                                                                                                    
Out[51]: 
array([[[ 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]]])

Your a1 (2,2) array:

In [55]: xyz[0,:2,:2]                                                                                           
Out[55]: 
array([[1, 2],
       [4, 5]])

and all 3:

In [56]: xyz[:,:2,:2]                                                                                           
Out[56]: 
array([[[ 1,  2],
        [ 4,  5]],

       [[10, 11],
        [13, 14]],

       [[19, 20],
        [22, 23]]])

and rearrange them into the desired (4,3):

In [57]: xyz[:,:2,:2].transpose(1,2,0)                                                                          
Out[57]: 
array([[[ 1, 10, 19],
        [ 2, 11, 20]],

       [[ 4, 13, 22],
        [ 5, 14, 23]]])
In [58]: xyz[:,:2,:2].transpose(1,2,0).reshape(4,3)                                                             
Out[58]: 
array([[ 1, 10, 19],
       [ 2, 11, 20],
       [ 4, 13, 22],
       [ 5, 14, 23]])

similarly for the other windows:

In [59]: xyz[:,1:3,:2].transpose(1,2,0).reshape(4,3)                                                            
Out[59]: 
array([[ 4, 13, 22],
       [ 5, 14, 23],
       [ 7, 16, 25],
       [ 8, 17, 26]])
In [60]: xyz[:,0:2,1:3].transpose(1,2,0).reshape(4,3)                                                           
Out[60]: 
array([[ 2, 11, 20],
       [ 3, 12, 21],
       [ 5, 14, 23],
       [ 6, 15, 24]])
In [61]: xyz[:,1:3,1:3].transpose(1,2,0).reshape(4,3)                                                           
Out[61]: 
array([[ 5, 14, 23],
       [ 6, 15, 24],
       [ 8, 17, 26],
       [ 9, 18, 27]])

We could also the view_as_windows as @Divakar suggested (or as_strided ), but conceptually that's trickier.

====

I can skip the transpose if I stack differently:

In [65]: xyz=np.stack((x,y,z), axis=2)                                                                          
In [66]: xyz                                                                                                    
Out[66]: 
array([[[ 1, 10, 19],
        [ 2, 11, 20],
        [ 3, 12, 21]],

       [[ 4, 13, 22],
        [ 5, 14, 23],
        [ 6, 15, 24]],

       [[ 7, 16, 25],
        [ 8, 17, 26],
        [ 9, 18, 27]]])

In [68]: xyz[:2,:2].reshape(4,3)                                                                                
Out[68]: 
array([[ 1, 10, 19],
       [ 2, 11, 20],
       [ 4, 13, 22],
       [ 5, 14, 23]])

===

In [84]: import skimage                                                                                         
In [85]: skimage.util.view_as_windows(xyz,(2,2,3),1).shape                                                      
Out[85]: (2, 2, 1, 2, 2, 3)
In [86]: skimage.util.view_as_windows(xyz,(2,2,3),1).reshape(4,4,3)                                             
Out[86]: 
array([[[ 1, 10, 19],
        [ 2, 11, 20],
        [ 4, 13, 22],
        [ 5, 14, 23]],

       [[ 2, 11, 20],
        [ 3, 12, 21],
        [ 5, 14, 23],
        [ 6, 15, 24]],

       [[ 4, 13, 22],
        [ 5, 14, 23],
        [ 7, 16, 25],
        [ 8, 17, 26]],

       [[ 5, 14, 23],
        [ 6, 15, 24],
        [ 8, 17, 26],
        [ 9, 18, 27]]])

a1 = np.array([[1,2],[4,5]])

a2 = np.array([[10,11],[13,14]])

a3 = np.array([[19,20],[22,23]])

def everything(a1,a2,a3):

    b1 = a1.reshape(-1)
    b2 = a2.reshape(-1)
    b3 = a3.reshape(-1)
    c = np.concatenate((b1, b2, b3))
    b = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
    def inner(a, i):
        while i < len(a):
            i = i + 1
            return a[i - 1]
    def looping(a, c):
        k = 0
        j = 0
        while j < len(a) - 1:
            i = 0
            while i < len(a):
                b[i][j] = inner(c, k)
                i += 1
                k += 1
            j += 1
    looping(b3, c)
    print(b)
everything(a1,a2,a3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM