ValueError: could not broadcast input array from shape (22500,3) into shape (1)

蹲街弑〆低调 提交于 2019-12-12 02:24:40

问题


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?

Thanks.


回答1:


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])]

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)

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



来源:https://stackoverflow.com/questions/42941668/valueerror-could-not-broadcast-input-array-from-shape-22500-3-into-shape-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!