且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 reshape 在 numpy 中将 N 长度向量重塑为 3x(N/3) 矩阵

更新时间:2022-02-26 03:40:41

Reshape 将第一个轴一分为二,后者的长度为 3 并转置 -

Reshape to split the first axis into two with the latter of length 3 and transpose -

a.reshape(-1,3).T

或者按照 fortran 顺序进行整形,并翻转整形参数 -

Or reshape in fortran order with reshaping parameters flipped -

a.reshape(3,-1, order='F')

样品运行 -

In [714]: a
Out[714]: array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])

In [715]: a.reshape(-1,3).T
Out[715]: 
array([[ 1,  4,  7, 10],
       [ 2,  5,  8, 11],
       [ 3,  6,  9, 12]])

In [719]: a.reshape(3,-1, order='F')
Out[719]: 
array([[ 1,  4,  7, 10],
       [ 2,  5,  8, 11],
       [ 3,  6,  9, 12]])