給定一個 3D 陣列,如下所示
nnodes,nslice,nsbj=2,4,3
arr=np.random.randint(10, size=(nsbj,nslice,nnodes))
[[[5 0]
[3 3]
[7 9]
[3 5]]
[[2 4]
[7 6]
[8 8]
[1 6]]
[[7 7]
[8 1]
[5 9]
[8 9]]]
我想將其重塑為形狀陣列 (2,12)
5 3 7 3 2 7 8 1 7 8 5 8
0 3 9 5 4 6 8 6 7 1 9 9
使用order重塑的 A、C、F 的變化不會產生預期的輸出
arr.(2,-1,order='F') # tested also against A, and C
同樣,以下也沒有給出我想要的
arr.transpose(0,2,1).reshape(2,-1,order='F')
為了重現性,這里是玩具代碼
import numpy as np
np.random.seed(0)
nnodes,nslice,nsbj=2,4,3
arr=np.random.randint(10, size=(nsbj,nslice,nnodes))
uj5u.com熱心網友回復:
你可以ravel/flatten然后reshape:
arr.ravel().reshape(2,-1,order='F')
輸出:
array([[5, 3, 7, 3, 2, 7, 8, 1, 7, 8, 5, 8],
[0, 3, 9, 5, 4, 6, 8, 6, 7, 1, 9, 9]])
替代reshape和transpose:
arr.reshape(-1,2).T
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/369813.html
