numpy:3D到2D

时间:2018-08-03 14:26:30

标签: python python-3.x python-2.7 numpy numpy-ndarray

我有一个形状为[2000, 140, 190]的矩阵。在这里,2000是2D切片的数量,其中每个切片为[140,190]。

我想将此3D矩阵转换为[7000, 7600](提示:140*50 = 7000; 190*40 = 7600; 50*40 = 2000)。我想以行主要方式扩展矩阵。有指针吗?

3 个答案:

答案 0 :(得分:1)

听起来您也想在其中转置

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))

答案 1 :(得分:0)

如评论中所述,您可以np.reshape

m_2d = np.random.rand(7000, 7600)
m_3d = m_2d.reshape([2000, 140, 190])

答案 2 :(得分:0)

解决方案是如上所述的reshape,如果要行专业,则应根据文档将order设置为F

m_2d = np.random.rand(7000, 7600)
m_3d = m_2d.reshape([2000, 140, 190],order='F')