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

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

我正在尝试将数据重塑为图像,如下所示。一切正常,除了最后一行代码:

y[i]=image2

我想将结果存储在一个新变量中y。我在行代码 y[i]=image2 处收到此错误:

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

我的代码:

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

      y = [len(v) for v in batch_1.values()]

   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)))
        image_result = reshape(image_result, (1, 3072))
        Y[i] = image_result

问题出在额外维度上。将 (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