首页 > 解决方案 > np.reshape 1d 数组到 2d 数组 - 选择正确的尺寸

问题描述

我正在尝试使用 numpy 的 reshape 将 1d 数组重塑为 2d 数组:

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)

标签: pythonnumpy

解决方案


利用

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

或者

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

推荐阅读