将数据重塑为图像ValueError:无法将形状(1,3072)的输入数组广播为形状(32,3)

时间:2016-10-27 13:46:49

标签: python-2.7 image-processing multidimensional-array 2d

尝试将数据重塑为图像,如下所示。一切都运作良好,除了最后一行代码:

y[i]=image2

我想将结果存储在新变量y中。我在第[{1}}行代码中收到此错误:

y[i]=image2

我的代码:

**ValueError: could not broadcast input array from shape (1,3072) into shape (32,3)**

1 个答案:

答案 0 :(得分:0)

问题在于额外的维度。将(1, 3072)替换为3072

编辑

您的代码也存在更多问题。 Y未定义,并且按行image_result= reshape(image2, (1, 3072))我认为您的确意味着:

image2 = reshape(image_result, (1, 3072))

新代码应如下所示(未经测试):

from numpy import *
import cPickle
import scipy.io as io
from random import randrange

Y = zeros([len(batch_1['data'][:]),3072])

for i in range(len(batch_1['data'][:])):  #len() =10000  
    image = batch_1['data'][i]
    image.shape = (3, 32, 32)
    image_result = copy(image.transpose((1, 2, 0)))
    image2 = reshape(image_result, (1, 3072))
    y[i]=image2