[英]Converting 3D matrix to cascaded 2D Matrices
I have a 3D
matrix in python as the following: 我在python中有一个
3D
矩阵,如下所示:
import numpy as np
a = np.ones((2,2,3))
a[0,0,0] = 2
a[0,0,1] = 3
a[0,0,2] = 4
I want to convert this 3D
matrix to a set of 2D
matrices. 我想将这个
3D
矩阵转换为一组2D
矩阵。 I have tried np.reshape
but it did not solve my problem. 我试过
np.reshape
但它没有解决我的问题。 The final shape I am interested in is the following cascaded vesrsion: 我感兴趣的最终形状是以下级联的vesrsion:
[[ 2. 1. 3. 1. 4. 1.]
[ 1. 1. 1. 1. 1. 1.]]
However, np.reshape
gives me the following 但是,
np.reshape
给了我以下内容
[[ 2. 3. 4. 1. 1. 1.]
[ 1. 1. 1. 1. 1. 1.]]
How can I solve this? 我怎么解决这个问题?
Use transpose
alongwith reshape
- 使用
transpose
reshape
-
a.transpose([0,2,1]).reshape(a.shape[0],-1)
Or use swapaxes
that does the same job as transpose
alongwith reshape
- 或者使用与
transpose
相同的swapaxes
- reshape
swapaxes
-
a.swapaxes(2,1).reshape(a.shape[0],-1)
Sample run - 样品运行 -
In [66]: a
Out[66]:
array([[[ 2., 3., 4.],
[ 1., 1., 1.]],
[[ 1., 1., 1.],
[ 1., 1., 1.]]])
In [67]: a.transpose([0,2,1]).reshape(a.shape[0],-1)
Out[67]:
array([[ 2., 1., 3., 1., 4., 1.],
[ 1., 1., 1., 1., 1., 1.]])
In [68]: a.swapaxes(2,1).reshape(a.shape[0],-1)
Out[68]:
array([[ 2., 1., 3., 1., 4., 1.],
[ 1., 1., 1., 1., 1., 1.]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.