【问题标题】: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)如何从 Python numpy 3D 数组到 2D 到 1D 回到 2D(保留 3D 数组的原始第 2 维和第 3 维)
【发布时间】:2019-05-03 18:20:49
【问题描述】:

我有一个 3D 数组,一个:

`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 问题)似乎没有针对我的具体问题的干净示例。我所缺少的任何方向都会有所帮助。

【问题讨论】:

  • 用一些小东西做实验,比如 np.arange(24.reshape(4,3,2)。reshape to (4,6) 与 reshape to (6,4) 完全不同。跨度>
  • 好主意,谢谢!没想到!!

标签: python arrays numpy reshape


【解决方案1】:

以下是可能遇到相同问题的其他人的答案:

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

【讨论】:

    猜你喜欢
    • 2019-08-20
    • 2020-10-03
    • 2016-03-25
    • 2012-12-09
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 2012-12-31
    相关资源
    最近更新 更多