gpt4 book ai didi

python-3.x - 向 ndarray 添加维度并 reshape

转载 作者:行者123 更新时间:2023-12-01 02:44:13 25 4
gpt4 key购买 nike

我有一个形状为 (16,2) 的 numpy 数组

[[-109.12722222    1454.        ]
[-109.12694444 1459. ]
[-109.12666667 1463. ]
[-109.12638889 1465. ]
[-109.12611111 1464. ]
[-109.12583333 1464. ]
[-109.12555556 1464. ]
[-109.12527778 1464. ]
[-109.125 1464. ]
[-109.12472222 1465. ]
[-109.12444444 1465. ]
[-109.12416667 1463. ]
[-109.12388889 1462. ]
[-109.12361111 1461. ]
[-109.12333333 1459. ]
[-109.12305556 1454. ]]

我想知道如何 reshape 它,使其成为 (4,4,2)。

[[-109.12722222    1454.        ] [-109.12694444    1459.        ][-109.12666667    1463.        ][-109.12638889    1465.        ]
[-109.12611111 1464. ] [-109.12583333 1464. ] [-109.12555556 1464. ][-109.12527778 1464. ]
[-109.125 1464. ][-109.12472222 1465. ][-109.12444444 1465. ][-109.12416667 1463. ]
[-109.12388889 1462. ][-109.12361111 1461. ][-109.12333333 1459. ][-109.12305556 1454. ]]

我试过这个:

numpy_array = np.reshape(4, 4, 2)

但它抛出:

ValueError: 无法将大小为 1 的数组 reshape 为形状 (4,)

最佳答案

您可以使用 numpy.newaxis

import numpy as np

a = np.zeros((16, 2))
b = a[:, :, np.newaxis]
c = b.reshape(4, 4, 2)

print(c.shape)

关于python-3.x - 向 ndarray 添加维度并 reshape ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57620467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com