交织3个numpy矩阵?

J.D*_*own 4 python numpy matrix

我如何在 columwise 中交织 numpy 矩阵。

给出这个例子:

>>> import numpy as np
>>> a = np.zeros((3,3))
>>> b = np.ones((3,3))
>>> c = b*2
Run Code Online (Sandbox Code Playgroud)

交织输出应该是

[[ a[0,0].  b[0,0].  c[0,0].  a[0,1]  b[0,1]  c[0,1] .. ]
 [ a[1,0].  b[1,0].  c[1,0].  a[1,1]  b[1,1]  c[1,1] .. ]
 [ a[2,0].  b[2,0].  c[2,0].  a[2,1]  b[2,1]  c[2,1] .. ]]
Run Code Online (Sandbox Code Playgroud)

最终形状应为 (3,9)

Psi*_*dom 5

另一种选择,可以使用np.dstack + reshape,根据文档dstack堆叠在明智序列深度(沿第三轴)阵列,因此它可以是方便的这种情况下:

np.dstack([a, b, c]).reshape(3,-1)
#array([[ 0.,  1.,  2.,  0.,  1.,  2.,  0.,  1.,  2.],
#       [ 0.,  1.,  2.,  0.,  1.,  2.,  0.,  1.,  2.],
#       [ 0.,  1.,  2.,  0.,  1.,  2.,  0.,  1.,  2.]])
Run Code Online (Sandbox Code Playgroud)

一些样本数据不那么模糊:

import numpy as np
a = np.arange(9).reshape(3,3)
b = (np.arange(9) + 9).reshape(3,3)
c = b*2

#array([[ 0,  9, 18,  1, 10, 20,  2, 11, 22],
#       [ 3, 12, 24,  4, 13, 26,  5, 14, 28],
#       [ 6, 15, 30,  7, 16, 32,  8, 17, 34]])
Run Code Online (Sandbox Code Playgroud)