简体   繁体   English

np.reshape 1d array to 2d array - 选择正确的尺寸

[英]np.reshape 1d array to 2d array - choosing right dimensions

I'm trying to reshape a 1d array to a 2d array with numpy's reshape:我正在尝试使用 numpy 的重塑将一维数组重塑为二维数组:

import numpy as np
inputArray =np.random.randint(low=0, high=4, size=160000)
inputArray_ = inputArray.reshape(-1,4000, 4000,4)

Which returns a value error:返回值错误:

ValueError: cannot reshape array of size 160000 into shape (400,400,4)

Use

inputArray_  = np.reshape(inputArray, (-1, 2))

Or或者

inputArray_ = np.reshape(inputArray, (len(inputArray)/2,2))

since 400*400*4 = 640,000 is bigger than 160000 you cannot reshape.由于 400*400*4 = 640,000 大于 160000,因此您无法重塑。

You don't have enough values to fill the new shape.您没有足够的值来填充新形状。

640,000-160,000 = 480,000. 640,000-160,000 = 480,000。 you lack 480,000 values.你缺少 480,000 个值。

divide your shape of 160000 by the other dimensions-multiplicated, if a int is the result, it works.将您的 160000 形状除以其他维度 - 乘以,如果结果为 int,则它有效。

eg例如

inputArray_ = inputArray.reshape(-1,40, 40, 10)

this will result in a shape of [10,40,40,10]这将导致 [10,40,40,10] 的形状

since 160000 / (40*40*10) = 10 ---> 10 is the dim that the "-1" takes因为 160000 / (40*40*10) = 10 ---> 10 是“-1”所采用的暗淡

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM