我有一个尺寸为 549x19x50
的 3D 矩阵,我需要创建一个 2D 矩阵,它可以得到一个 549x950
矩阵。
到目前为止我所做的是使用tensorflow;
#data_3d is the 3D matrix
data_2d = tf.reshape(data_3d,[549,-1])
这会打印出提示中 data_3d 的所有值,当我尝试访问 data_2d 时,它会给我一个 NameError
data_3d 是列表的列表的列表。不是张量或 ndarray。如果我们不能对列表执行此操作,有什么方法可以轻松地将列表转换为 ndarray?
提前致谢,
巴希特
最佳答案
使用numpy
有一个简单的方法:
import numpy as np
data_3d = np.arange(27).reshape((3,3,3))
data_2d = data_3d.swapaxes(1,2).reshape(3,-1)
输出:
data_2d
[[ 0 3 6 1 4 7 2 5 8]
[ 9 12 15 10 13 16 11 14 17]
[18 21 24 19 22 25 20 23 26]]
print data_3d
[[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]]
[[ 9 10 11]
[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]
[24 25 26]]]
注意:swapaxes(1,2)
是这里的主要内容 - 您需要定义要交换的轴。
关于python - 使用 tensorflow 将 3D 矩阵 reshape 为 2D 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38220133/