[英]np.reshape 1d array to 2d array - choosing right dimensions
我正在嘗試使用 numpy 的重塑將一維數組重塑為二維數組:
import numpy as np
inputArray =np.random.randint(low=0, high=4, size=160000)
inputArray_ = inputArray.reshape(-1,4000, 4000,4)
返回值錯誤:
ValueError: cannot reshape array of size 160000 into shape (400,400,4)
用
inputArray_ = np.reshape(inputArray, (-1, 2))
或者
inputArray_ = np.reshape(inputArray, (len(inputArray)/2,2))
由於 400*400*4 = 640,000 大於 160000,因此您無法重塑。
您沒有足夠的值來填充新形狀。
640,000-160,000 = 480,000。 你缺少 480,000 個值。
將您的 160000 形狀除以其他維度 - 乘以,如果結果為 int,則它有效。
例如
inputArray_ = inputArray.reshape(-1,40, 40, 10)
這將導致 [10,40,40,10] 的形狀
因為 160000 / (40*40*10) = 10 ---> 10 是“-1”所采用的暗淡
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.