我有一個 numpy 數(shù)組,其中的列在塊中。我想轉(zhuǎn)置塊。它在概念上很簡單,我想人們可以簡單地做到這一點,但我不知道如何做到。給定塊形式的 numpy 數(shù)組np.hstack(list_of_blocks),我想得到np.vstack(list_of_blocks).為了使它更精確,我想在下面的代碼片段中從一個數(shù)組a到另一個數(shù)組b。import numpy as npa = np.zeros((3,6))b = np.zeros((9,2))t_max = 3for col in range(1,7): for time in range(1,t_max+1): val = ((1+col)//2)*100+((col+1) % 2)*10+time a[time-1,col-1]= val b[time+t_max*(((1+col)//2)-1)-1,((col+1) % 2)] = val矩陣看起來像:>>> print(a)[[101. 111. 201. 211. 301. 311.] [102. 112. 202. 212. 302. 312.] [103. 113. 203. 213. 303. 313.]]>>> print(b)[[101. 111.] [102. 112.] [103. 113.] [201. 211.] [202. 212.] [203. 213.] [301. 311.] [302. 312.] [303. 313.]]當(dāng)然,矩陣不是3 x (2*3),而是n x (k*m)在 numpy 中重塑這樣的有效方法是什么?
2 回答

神不在的星期二
TA貢獻(xiàn)1963條經(jīng)驗 獲得超6個贊
重塑,置換軸和重塑 -
N = a.shape[1]//t_max b_out = a.reshape(a.shape[0],-1,N).swapaxes(0,1).reshape(-1,N)
更多信息intuition behind nd-to-nd array transformation
。
添加回答
舉報
0/150
提交
取消