gpt4 book ai didi

python - np.reshape 返回错误 'ValueError: total size of new array must be unchanged'

转载 作者:太空宇宙 更新时间:2023-11-03 15:44:42 24 4
gpt4 key购买 nike

我尝试输入的数组长度为 240。

我已经尝试过:

r = np.reshape(array, (3, 80))

因为我在本网站的其他地方读到,输入 reshape 的行和列必须乘以数组长度。

但是,我仍然收到错误:

ValueError: total size of new array must be unchanged

最佳答案

您说您的数组中有额外的维度,因此您需要保留它们:

>>> arr = np.random.random((240, 215, 3))
>>> reshaped = np.reshape(arr, (3, 80, arr.shape[1], arr.shape[2]))
>>> reshaped.shape
(3, 80, 215, 3)

或使用解包来避免对尺寸进行硬编码:

>>> reshaped = np.reshape(arr, (3, 80, *arr.shape[1:]))
(3, 80, 215, 3)

如果您希望取消最后一个维度,那么您还可以使用 -1 作为 reshape 中的最后一个轴:

>>> reshaped_ravel = np.reshape(arr, (3, 80, -1))
>>> reshaped_ravel.shape
(3, 80, 645)

关于python - np.reshape 返回错误 'ValueError: total size of new array must be unchanged',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41862119/

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