簡體   English   中英

np.reshape 返回錯誤“ValueError:新數組的總大小必須保持不變”

[英]np.reshape returns error 'ValueError: total size of new array must be unchanged'

我嘗試輸入的數組長度為 240。

我試過了:

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

因為我在本網站的其他地方讀到,進入 reshape 的行和列必須乘以數組長度。

但是,我仍然收到錯誤:

ValueError:新數組的總大小必須保持不變

你說你的數組中有額外的維度,所以你需要保留它們:

>>> 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作為重塑中的最后一個軸:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM