如何从 Python numpy 3D 数组到 2D 到 1D 回到 2D(保留 3D 数组的原始第 2 维和第 3 维)
How to go from Python numpy 3D array to 2D to 1D back to 2D (preserving the original 2nd and 3rd dimension of the 3D array)
我有一个 3D 数组,a:
`print(a.shape)
In [1]:(4, 4571, 8893)
b = a.reshape(a.shape[2]*a.shape[1],a.shape[0]) # Here I've also tried changing the shape of with (a.shape[2]*a.shape[1],a.shape[0])
print(b.shape)
In [2]:(40649903, 4)
c=some_function(b) # returns c which has same shape as b.shape[0]
print(c.shape)
In [2]: (40649903,)
d = c.reshape(a.shape[1],a.shape[2]) # same shape as a.shape[1:]
print(d.shape)
In [3]:(4571, 8893)
`
现在当我看 d 时,我得到这样的形状:
plt.imshow(d)
但必须像下图一样(请忽略颜色,黄色区域的形状必须像海军蓝色区域):
plt.imshow(a[0])
也许这与重塑轴有关,但我无法弄清楚我在哪里使用了错误的轴来重塑。我对此进行了一些思考,并阅读了 numpy 文档,但文档和在线示例(SO 问题)似乎没有针对我的特定问题的干净示例。我所缺少的任何指示都会有所帮助。
以下是可能遇到相同问题的其他人的答案:
a = np.arange(24).reshape(4,3,2)
print(a); print(a.shape)
b = a.reshape(a.shape[0],a.shape[1]*a.shape[2]).T;
print(b); print(b.shape) # X
c = a[0].flatten() # Y
print(c); print(c.shape)
d = c.reshape(a[1].shape);
print(d); print(d.shape) # same as print(a[0].shape)
感谢您的建议@hpaulj
我有一个 3D 数组,a:
`print(a.shape)
In [1]:(4, 4571, 8893)
b = a.reshape(a.shape[2]*a.shape[1],a.shape[0]) # Here I've also tried changing the shape of with (a.shape[2]*a.shape[1],a.shape[0])
print(b.shape)
In [2]:(40649903, 4)
c=some_function(b) # returns c which has same shape as b.shape[0]
print(c.shape)
In [2]: (40649903,)
d = c.reshape(a.shape[1],a.shape[2]) # same shape as a.shape[1:]
print(d.shape)
In [3]:(4571, 8893)
`
现在当我看 d 时,我得到这样的形状:
plt.imshow(d)
但必须像下图一样(请忽略颜色,黄色区域的形状必须像海军蓝色区域):
plt.imshow(a[0])
也许这与重塑轴有关,但我无法弄清楚我在哪里使用了错误的轴来重塑。我对此进行了一些思考,并阅读了 numpy 文档,但文档和在线示例(SO 问题)似乎没有针对我的特定问题的干净示例。我所缺少的任何指示都会有所帮助。
以下是可能遇到相同问题的其他人的答案:
a = np.arange(24).reshape(4,3,2)
print(a); print(a.shape)
b = a.reshape(a.shape[0],a.shape[1]*a.shape[2]).T;
print(b); print(b.shape) # X
c = a[0].flatten() # Y
print(c); print(c.shape)
d = c.reshape(a[1].shape);
print(d); print(d.shape) # same as print(a[0].shape)
感谢您的建议@hpaulj