有一個功能np.split()
可以沿1軸拆分數組。例如,我想知道是否有多軸版本可以沿軸(0,1,2)分割。Numpy將立方體拆分成立方體
1
A
回答
3
假設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
),使得重複的次數至上,和newshape自帶底:
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維。它可以進一步推廣到任何尺寸的ndarrays:
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)
到 「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)
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]])]
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)
相關問題
- 1. 生成座標立方體
- 2. SSAS立方體分組
- 3. 將球體投影到立方體上
- 4. SSAS:在立方體
- 5. 單位立方體
- 6. 重疊立方體
- 7. Hadoop超立方體
- 8. LWJGL立方體圖
- 9. 瀏覽器CSS變換立方體從立方體翻轉
- 10. SSAS:來自多個立方體實例的中央立方體
- 11. 如何繪製立方體,立方體和金字塔
- 12. olap立方體建築 - 多少個olap立方體?
- 13. Opengl基本立方體繪圖,但立方體繪製單色
- 14. Java 3D的數組:存儲立方體,檢索立方體
- 15. 一個立方體是另一個立方體的來源
- 16. 我想將一個超空間劃分爲立方體。立方體做成B +樹,我該怎麼做?
- 17. 繪製一個立方體並旋轉它:立方體的一部分消失
- 18. 沒有剛體的落體立方體
- 19. 拆分使用z軸的立方體邊界
- 20. 將立方體貼圖的立方體貼圖渲染到四邊形
- 21. 如何將兩列上的立方體看作是一個立方體?
- 22. 生成塊的大立方體
- 23. 生成超立方體鏈接數據
- 24. SSAS條件立方體計算成員
- 25. 使立方體變成圓角
- 26. 3D立方體問題,第2部分
- 27. 3D立方體問題!第1部分
- 28. 如何繪製立方體
- 29. LWJGL紋理立方體
- 30. SQL - 查詢立方體
如果我想解開它什麼是轉置重塑組合我需要回到大立方體? – mattdns
@mattdns:我在上面添加了一個'uncubify'函數。我們對'reverseOrder'的定義有所不同(或者我在'uncubify'函數中稱爲'order')。它對高維數組有所幫助。將定義更改爲'self.reverseOrder = np.arange(len(self.tmpshape))。reshape(2,-1).ravel(order ='F')'將修復您的代碼。 – unutbu