Numpy将立方体分割成立方体

mat*_*dns 6 numpy

有一个功能np.split()可以沿1轴分割数组.我想知道是否有一个多轴版本,你可以沿轴(0,1,2)分割.

unu*_*tbu 12

假设它cube有形状(W, H, D),你希望把它分成几个N小方块的形状(w, h, d).由于NumPy阵列具有固定长度的轴,因此w必须均匀分割W,并且类似于hd.

然后有一种方法可以将形状的立方体重新塑造(W, H, D)成一个新的形状阵列(N, w, h, d).

例如,如果arr = np.arange(4*4*4).reshape(4,4,4)(so (W,H,D) = (4,4,4))并且我们希望将其分解为形状的立方体(2,2,2),那么我们可以使用

In [283]: arr.reshape(2,2,2,2,2,2).transpose(0,2,4,1,3,5).reshape(-1,2,2,2)
Out[283]: 
array([[[[ 0,  1],
         [ 4,  5]],

        [[16, 17],
         [20, 21]]],

...
       [[[42, 43],
         [46, 47]],

        [[58, 59],
         [62, 63]]]])
Run Code Online (Sandbox Code Playgroud)

这里的想法是为数组添加额外的轴,这些轴充当地点标记:

 number of repeats act as placemarkers
 o---o---o
 |   |   |
 v   v   v
(2,2,2,2,2,2)
   ^   ^   ^
   |   |   |
   o---o---o
   newshape
Run Code Online (Sandbox Code Playgroud)

然后我们可以对轴进行重新排序(使用transpose),以便首先获得重复次数,并在最后显示新闻形状:

arr.reshape(2,2,2,2,2,2).transpose(0,2,4,1,3,5)
Run Code Online (Sandbox Code Playgroud)

最后,调用reshape(-1, w, h, d)将所有地标轴压缩成单个轴.这会产生一个形状数组,(N, w, h, d)其中N是小立方体的数量.


上面使用的想法是将这个想法概括为3维.它可以进一步推广到任何维度的ndarray:

import numpy as np
def cubify(arr, newshape):
    oldshape = np.array(arr.shape)
    repeats = (oldshape / newshape).astype(int)
    tmpshape = np.column_stack([repeats, newshape]).ravel()
    order = np.arange(len(tmpshape))
    order = np.concatenate([order[::2], order[1::2]])
    # newshape must divide oldshape evenly or else ValueError will be raised
    return arr.reshape(tmpshape).transpose(order).reshape(-1, *newshape)

print(cubify(np.arange(4*6*16).reshape(4,6,16), (2,3,4)).shape)
print(cubify(np.arange(8*8*8*8).reshape(8,8,8,8), (2,2,2,2)).shape)
Run Code Online (Sandbox Code Playgroud)

产生新的形状数组

(16, 2, 3, 4)
(256, 2, 2, 2, 2)
Run Code Online (Sandbox Code Playgroud)

要"取消"数组:

def uncubify(arr, oldshape):
    N, newshape = arr.shape[0], arr.shape[1:]
    oldshape = np.array(oldshape)    
    repeats = (oldshape / newshape).astype(int)
    tmpshape = np.concatenate([repeats, newshape])
    order = np.arange(len(tmpshape)).reshape(2, -1).ravel(order='F')
    return arr.reshape(tmpshape).transpose(order).reshape(oldshape)
Run Code Online (Sandbox Code Playgroud)

这里有一些测试代码来检查cubifyuncubify反转.

import numpy as np
def cubify(arr, newshape):
    oldshape = np.array(arr.shape)
    repeats = (oldshape / newshape).astype(int)
    tmpshape = np.column_stack([repeats, newshape]).ravel()
    order = np.arange(len(tmpshape))
    order = np.concatenate([order[::2], order[1::2]])
    # newshape must divide oldshape evenly or else ValueError will be raised
    return arr.reshape(tmpshape).transpose(order).reshape(-1, *newshape)

def uncubify(arr, oldshape):
    N, newshape = arr.shape[0], arr.shape[1:]
    oldshape = np.array(oldshape)    
    repeats = (oldshape / newshape).astype(int)
    tmpshape = np.concatenate([repeats, newshape])
    order = np.arange(len(tmpshape)).reshape(2, -1).ravel(order='F')
    return arr.reshape(tmpshape).transpose(order).reshape(oldshape)

tests = [[np.arange(4*6*16), (4,6,16), (2,3,4)],
         [np.arange(8*8*8*8), (8,8,8,8), (2,2,2,2)]]

for arr, oldshape, newshape in tests:
    arr = arr.reshape(oldshape)
    assert np.allclose(uncubify(cubify(arr, newshape), oldshape), arr)
    # cuber = Cubify(oldshape,newshape)
    # assert np.allclose(cuber.uncubify(cuber.cubify(arr)), arr)
Run Code Online (Sandbox Code Playgroud)

  • @mattdns:我在上面添加了一个 `uncubify` 函数。我们对 `reverseOrder`(或者我在 `uncubify` 函数中称为 `order` 的定义)的定义有所不同。它对高维数组产生影响。将定义更改为 `self.reverseOrder = np.arange(len(self.tmpshape)).reshape(2, -1).ravel(order='F')` 将修复您的代码。 (2认同)