英文:
np.reshape 1d array to 2d array - choosing right dimensions
问题
我正在尝试使用numpy的reshape将一个1维数组重塑为一个2维数组:
import numpy as np
inputArray = np.random.randint(low=0, high=4, size=160000)
inputArray_ = inputArray.reshape(-1, 4000, 4000, 4)
这会引发一个值错误:
ValueError: 无法将大小为160000的数组重塑为形状(400, 400, 4)
英文:
I'm trying to reshape a 1d array to a 2d array with numpy's reshape:
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)
答案1
得分: 2
使用
```python
inputArray_ = np.reshape(inputArray, (-1, 2))
或者
inputArray_ = np.reshape(inputArray, (len(inputArray)/2,2))
英文:
Use
inputArray_ = np.reshape(inputArray, (-1, 2))
Or
inputArray_ = np.reshape(inputArray, (len(inputArray)/2,2))
答案2
得分: 0
since 4004004 = 640,000 is bigger than 160,000, you cannot reshape.
You don't have enough values to fill the new shape.
640,000 - 160,000 = 480,000. You lack 480,000 values.
Divide your shape of 160,000 by the other dimensions multiplied. If an integer is the result, it works.
e.g.
inputArray_ = inputArray.reshape(-1, 40, 40, 10)
This will result in a shape of [10, 40, 40, 10]
Since 160,000 / (404010) = 10 --> 10 is the dimension that the "-1" takes.
英文:
since 4004004 = 640,000 is bigger than 160000 you cannot reshape.
You don't have enough values to fill the new shape.
640,000-160,000 = 480,000. you lack 480,000 values.
divide your shape of 160000 by the other dimensions-multiplicated, if a int is the result, it works.
e.g.
inputArray_ = inputArray.reshape(-1,40, 40, 10)
this will result in a shape of [10,40,40,10]
since 160000 / (404010) = 10 ---> 10 is the dim that the "-1" takes
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论