Numpy:3D 到 2D
Numpy : 3D to 2D
我有一个形状为 [2000, 140, 190]
的矩阵。这里,2000 是二维切片的数量,每个切片是 [140, 190]。
我想将此 3D 矩阵转换为 [7000, 7600]
(提示:140*50 = 7000
;190*40 = 7600
;50*40 = 2000
)。我想以行主要方式扩展矩阵。有什么指点吗?
如评论中所述,您可以 np.reshape
m_2d = np.random.rand(7000, 7600)
m_3d = m_2d.reshape([2000, 140, 190])
解决方案是reshape
,如果你想要行专业,那么根据文档你应该将order
设置为F
:
m_2d = np.random.rand(7000, 7600)
m_3d = m_2d.reshape([2000, 140, 190],order='F')
听起来你也想要一个移调:
m_3d = np.random.rand(2000, 140, 190)
# break the 2000 dimension in two. Pick one:
m_4d = m_3d.reshape((50, 40, 140, 190))
# move the dimensions to collapse to be adjacent
# you might need to tweak this - you haven't given enough information to know
# what order you want
m_4d = m_4d.transpose((0, 2, 1, 3))
# collapse adjacent dimensions
m_2d = m_4d.reshape((7000, 7600))
我有一个形状为 [2000, 140, 190]
的矩阵。这里,2000 是二维切片的数量,每个切片是 [140, 190]。
我想将此 3D 矩阵转换为 [7000, 7600]
(提示:140*50 = 7000
;190*40 = 7600
;50*40 = 2000
)。我想以行主要方式扩展矩阵。有什么指点吗?
如评论中所述,您可以 np.reshape
m_2d = np.random.rand(7000, 7600)
m_3d = m_2d.reshape([2000, 140, 190])
解决方案是reshape
,如果你想要行专业,那么根据文档你应该将order
设置为F
:
m_2d = np.random.rand(7000, 7600)
m_3d = m_2d.reshape([2000, 140, 190],order='F')
听起来你也想要一个移调:
m_3d = np.random.rand(2000, 140, 190)
# break the 2000 dimension in two. Pick one:
m_4d = m_3d.reshape((50, 40, 140, 190))
# move the dimensions to collapse to be adjacent
# you might need to tweak this - you haven't given enough information to know
# what order you want
m_4d = m_4d.transpose((0, 2, 1, 3))
# collapse adjacent dimensions
m_2d = m_4d.reshape((7000, 7600))