[英]Reshape 3-D numpy.array to 1-D array in specific order
將 3-D numpy 陣列重塑為 1-D 陣列:
我想重塑一個看起來像這樣的 3-D 數組:
test_3d = np.array([[[0., 0.],
[1., 1.]],
[[2., 2.],
[3., 3.]]])
對於如下所示的一維數組:
array([0., 1., 2., 3., 0., 1., 2., 3.])
使用test_3d.flatten()
輸出展平數組:
array([0., 0., 1., 1., 2., 2., 3., 3.])
np.flatten/ravel 和轉置功能的組合適用於 2-D arrays,但對於 3-D 陣列,我得到以下信息:
Input:
test_3d.T.flatten()
Output:
array([0., 2., 1., 3., 0., 2., 1., 3.])
有人有什么想法嗎?
為了提供一個更有啟發性的例子,我將test_3d定義為:
test_3d = np.array(
[[[0., 10.],
[1., 11.]],
[[2., 12.],
[3., 13.]]])
(現在您可以區分兩個“初始”零)。
要獲得預期結果,請運行:
result = np.transpose(test_3d, (2, 0, 1)).flatten()
結果是:
array([ 0., 1., 2., 3., 10., 11., 12., 13.])
另一種解決方案是:
result = np.rollaxis(test_3d, 2).flatten()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.