我能想到的最簡單的方法是不向嘗試使用reshape
與方法,如ravel('F')
,只是來連接你的陣列的切片意見。
例如:
>>> cols = 4
>>> a = np.array([1,2,3,4,5,6,7])
>>> np.concatenate([a[i::cols] for i in range(cols)])
array([1, 5, 2, 6, 3, 7, 4])
這適用於陣列的任何長度和任何數量的列:
>>> cols = 5
>>> b = np.arange(17)
>>> np.concatenate([b[i::cols] for i in range(cols)])
array([ 0, 5, 10, 15, 1, 6, 11, 16, 2, 7, 12, 3, 8, 13, 4, 9, 14])
或者,使用as_strided
重塑。事實上,該陣列a
太小,以適應(2, 4)
形狀並不重要:你只收到垃圾(即無論在內存中的)最後一位:
>>> np.lib.stride_tricks.as_strided(a, shape=(2, 4))
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 168430121]])
>>> _.flatten('F')[:7]
array([1, 5, 2, 6, 3, 7, 4])
在一般情況下,給定陣列b
和列所需數量的cols
你可以這樣做:
>>> x = np.lib.stride_tricks.as_strided(b, shape=(len(b)//cols + 1, cols)) # reshape to min 2d array needed to hold array b
>>> np.concatenate((x[:,:len(b)%cols].ravel('F'), x[:-1, len(b)%cols:].ravel('F')))
這揭開了陣列的「好」的一部分(這些列不包含垃圾值)和壞的部分(除垃圾值這位於最下面一行)並連接兩個解開的數組。例如:
>>> cols = 5
>>> b = np.arange(17)
>>> x = np.lib.stride_tricks.as_strided(b, shape=(len(b)//cols + 1, cols))
>>> np.concatenate((x[:,:len(b)%cols].ravel('F'), x[:-1, len(b)%cols:].ravel('F')))
array([ 0, 5, 10, 15, 1, 6, 11, 16, 2, 7, 12, 3, 8, 13, 4, 9, 14])
這適用於我們只需要填充一個數字的情況,但不幸的是我需要填充很多。在一個簡單的例子中,我可能有[1,2,3,4,5,6],所以調用'as_strided'給出[[1,2,3,4],[5,6,9325823,1204738]],這成爲[1,5,2,6,3,9325823,4,1204738] - 現在採取前6個元素不會刪除垃圾:( – 2014-10-30 21:06:32
@GregOwen道歉 - 我應該解釋如何將該方法擴展到在我的答案中是一般情況,如果你感興趣的話,我現在添加它(一旦你有一個數組和列的數量,只需要兩行代碼來獲得結果!):-) – 2014-10-30 21:39:53