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)
Still good. But you want to leave pixels as a list until you're done with the iteration.
pixels = np.array(pixels) labels = np.array(labels)
You have this permission in another question. What happened? previous
Iterating, collecting values ββin a list, and then at the end, connecting things with a large array, is the right way. To keep things clear, I often prefer to use a type notation:
alist = [] for .. alist.append(...) arr = np.array(alist)
If the names indicate something about the nature of the object, Iβm less likely to get errors like yours.
I do not understand what you are trying to do with traindata . I doubt you need to build it during the cycle. pixels and labels have basic information.
it
traindata = np.array([traindata[i][i],traindata[1]], dtype=object)
comes from the previous question. I'm not sure you understand this answer.
traindata = [] traindata.append(pixels) traindata.append(labels)
if done out of cycle just
traindata = [pixels, labels]
labels is a 1d array, a bundle of 1s (or [0,1,2,3 ...] if my assumption is correct). pixels is an array with a larger size. What is its shape?
Stop here. It makes no sense to turn this list into an array. You can save the list with pickle .
You copy code from an earlier question and get formatting incorrectly. cPickle very large amount of data