将1维数组重新整形为2维数组 – 选择正确的维度

huangapple go评论74阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年1月4日 01:22:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582752.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定