有一个函数np.split()
可以沿1轴分割数组。我想知道是否有一个多轴版本,你可以沿轴(0,1,2)分割。
答案 0 :(得分:9)
假设cube
的形状为(W, H, D)
,您希望将其分解为N
形状(w, h, d)
的小方块。由于NumPy数组具有固定长度的轴,w
必须均分W
,h
和d
也是如此。
然后有一种方法可以将形状(W, H, D)
的立方体重塑为一个新的形状(N, w, h, d)
数组。
例如,如果arr = np.arange(4*4*4).reshape(4,4,4)
(所以(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]]]])
这里的想法是在阵列中添加额外的轴,这些轴充当地点标记:
number of repeats act as placemarkers
o---o---o
| | |
v v v
(2,2,2,2,2,2)
^ ^ ^
| | |
o---o---o
newshape
然后我们可以对轴进行重新排序(使用transpose
),以便首先出现重复次数,并在最后显示新闻形式:
arr.reshape(2,2,2,2,2,2).transpose(0,2,4,1,3,5)
最后,调用reshape(-1, w, h, d)
将所有地标轴压缩成单个轴。这会生成一个形状(N, w, h, d)
的数组,其中N
是小立方体的数量。
上面使用的想法是this idea到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)
产生新的形状数组
(16, 2, 3, 4)
(256, 2, 2, 2, 2)
To" uncubify"数组:
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)
以下是一些测试代码,用于检查cubify
和uncubify
是否为反转。
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)
答案 1 :(得分:0)
我不认为有一个多轴版本,您可以沿着某些给定的轴进行分割。但是你可以一次将它拆分为一个维度。例如:
def split2(arys, sections, axis=[0, 1]):
if not isinstance(arys, list):
arys = [arys]
for ax in axis:
arys = [np.split(ary, sections, axis=ax) for ary in arys]
arys = [ary for aa in arys for ary in aa] # Flatten
return arys
可以像这样使用:
In [1]: a = np.array(range(100)).reshape(10, 10)
In [2]: split2(a, 2, axis=[0, 1])
Out[2]:
[array([[ 0, 1, 2, 3, 4],
[10, 11, 12, 13, 14],
[20, 21, 22, 23, 24],
[30, 31, 32, 33, 34],
[40, 41, 42, 43, 44]]),
array([[ 5, 6, 7, 8, 9],
[15, 16, 17, 18, 19],
[25, 26, 27, 28, 29],
[35, 36, 37, 38, 39],
[45, 46, 47, 48, 49]]),
array([[50, 51, 52, 53, 54],
[60, 61, 62, 63, 64],
[70, 71, 72, 73, 74],
[80, 81, 82, 83, 84],
[90, 91, 92, 93, 94]]),
array([[55, 56, 57, 58, 59],
[65, 66, 67, 68, 69],
[75, 76, 77, 78, 79],
[85, 86, 87, 88, 89],
[95, 96, 97, 98, 99]])]
答案 2 :(得分:0)
除了我对@ unutbu的答案的额外问题,我认为我已经反过来工作(如果你想将一个立方体拆分成立方体,将一个函数应用于每个,然后将它们组合起来)
import numpy as np
import pdb
np.set_printoptions(precision=3,linewidth=300)
class Cubify():
def __init__(self,oldshape,newshape):
self.newshape = np.array(newshape)
self.oldshape = np.array(oldshape)
self.repeats = (oldshape / newshape).astype(int)
self.tmpshape = np.column_stack([self.repeats, newshape]).ravel()
order = np.arange(len(self.tmpshape))
self.order = np.concatenate([order[::2], order[1::2]])
self.reverseOrder = self.order.copy()
self.reverseOrder = np.arange(len(self.tmpshape)).reshape(2, -1).ravel(order='F')
self.reverseReshape = np.concatenate([self.repeats,self.newshape])
def cubify(self,arr):
# newshape must divide oldshape evenly or else ValueError will be raised
return arr.reshape(self.tmpshape).transpose(self.order).reshape(-1, *self.newshape)
def uncubify(self,arr):
return arr.reshape(self.reverseReshape).transpose(self.reverseOrder).reshape(self.oldshape)
if __name__ == "__main__":
N = 9
x = np.arange(N**3).reshape(N,N,N)
oldshape = x.shape
newshape = np.array([3,3,3])
cuber = Cubify(oldshape,newshape)
out = cuber.cubify(x)
back = cuber.uncubify(out)