本文介绍了ValueError:无法将输入数组从形状(22500,3)广播到形状(1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我依靠的代码是此处,但进行了少量修改.我的版本如下:

I relied on the code mentioned, here, but with minor edits. The version that I have is as follows:

import numpy as np
import _pickle as cPickle
from PIL import Image
import sys,os

pixels = []
labels = []
traindata = []
data=[]

directory = 'C:\\Users\\abc\\Desktop\\Testing\\images'
for root, dirs, files in os.walk(directory):
    for file in files:
        floc = file
        im = Image.open(str(directory) + '\\' + floc)
        pix = np.array(im.getdata())
        pixels.append(pix)
        labels.append(1)

pixels = np.array(pixels)
labels = np.array(labels)
traindata.append(pixels)
traindata.append(labels)
traindata = np.array(traindata)
# do the same for validation and test data
# put all data and labels into 'data' array
cPickle.dump(data,open('data.pkl','wb'))

运行代码时,我得到以下信息:

When I run the code, I get the following:

Traceback (most recent call last):
  File "pickle_data.py", line 24, in <module>
    traindata=np.array(traindata)
ValueError: could not broadcast input array from shape (22500,3) into shape (1)

我该如何解决这个问题?

How can I solve this issue?

谢谢.

推荐答案

为了理解traindata的结构,我用pixels.append(pix[np.ix_([1,2,3],[0,1,2])])替换了pixels.append(pix)来提供一些玩具示例.然后我得到traindata

To understand the structure of traindata, I replaced your pixels.append(pix) with pixels.append(pix[np.ix_([1,2,3],[0,1,2])]) to have some toy example. Then I get that traindata is

[array([[[16, 13, 15],
         [16, 13, 15],
         [16, 13, 15]]]), array([1])]

当您尝试将traindata转换为numpy数组时,会出现错误,因为它由不同大小的子数组组成.您可以将每个子数组保存在单独的numpy数组中,也可以这样:

When you tried to transform traindata to numpy array you got error as it consists of subarrays of different sizes. You can either save each one of the subarrays in a separate numpy array, or you can do it like that:

traindata = np.array([traindata[0][0],traindata[1]], dtype=object)

通过使用dtype=object,您可以创建由不同大小的元素组成的numpy数组.

By using dtype=object, you can create numpy array consists of elements of different sizes.

这篇关于ValueError:无法将输入数组从形状(22500,3)广播到形状(1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 15:01