NumPy堆叠多维数组

4
我有以下三个 nD 数组。
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]]

我想要不使用for循环,将每个2x2矩阵的元素拼接在一起:

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循环会影响运行时间,因此我正在尝试使用NumPy堆叠技术来解决问题。


它并没有回答我的问题。请重新打开它。我正在尝试从3个多维数组中提取2x2到2d数组。我只有xyz。如何创建所有可能的2x2矩阵,即a1、a2、a3、b1、b2、b3,然后添加元素? - Pradeep Tummala
认为使用np.dstack应该是您的起点。看起来您正在滑动。因此,也许可以在那里使用sckit-image的view_as_windows - Divakar
1
这里的 NxN 是什么意思?它总是会是 2x2 吗?还是对于较小的矩阵,它将是 NxN - r4bb1t
2个回答

1

你的3个数组:

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]]])

您的 a1 (2,2) 数组:

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

和所有3个:

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

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

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

并将它们重新排列成所需的(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]])

同样适用于其他窗口:
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]])

我们也可以像@Divakar建议的那样使用`view_as_windows`(或`as_strided`),但从概念上讲,这更棘手。

====

如果我以不同的 stack 方式堆叠,则可以跳过转置:

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]]])

0

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)

1
欢迎来到Stack Overflow!虽然这段代码可能解决了问题,但是包括解释它如何以及为什么解决了问题将有助于提高您的帖子质量,并可能导致更多的赞。请记住,您正在回答未来读者的问题,而不仅仅是现在提问的人。请[编辑]您的答案以添加解释并指出适用的限制和假设。 - rizerphe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接