Python - AttributeError: object 'numpy.ndarray' does not have attribute 'append'

This is related to my question here .

Now I have the updated code as follows:

import numpy as np import _pickle as cPickle from PIL import Image import sys,os pixels = [] labels = [] traindata = [] i = 0 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[i][i],traindata[1]], dtype=object) i = i + 1 # do the same for validation and test data # put all data and labels into 'data' array cPickle.dump(traindata,open('data.pickle','wb')) FILE = open("data.pickle", 'rb') content = cPickle.load(FILE) print (content) 

If there is only one image, the code works fine. But when I add another image or more, I get the following:

 Traceback (most recent call last): File "pickle_data.py", line 17, in <module> pixels.append((pix)) AttributeError: 'numpy.ndarray' object has no attribute 'append' 

How can I solve this problem?

Thank you

+8
source share
4 answers
 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) # append(i)??? 

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

+5
source

Numpy arrays have no add method. Instead, use the Numpy add function:

 import numpy as np array_3 = np.append(array_1, array_2, axis=n) # you can either specify an integer axis value n or remove the keyword argument completely 

For example, if array_1 and array_2 have the following meanings:

 array_1 = np.array([1, 2]) array_2 = np.array([3, 4]) 

If you call np.append without specifying an axis value, like this:

 array_3 = np.append(array_1, array_2) 

array_3 will have the following value:

 array([1, 2, 3, 4]) 

Otherwise, if you call np.append with an axis value of 0, for example, like this:

 array_3 = np.append(array_1, array_2, axis=0) 

array_3 will have the following value:

  array([[1, 2], [3, 4]]) 

More information about the add function here: https://docs.scipy.org/doc/numpy/reference/generated/numpy.append.html.

+8
source

append on ndarray is ambiguous; which axis do you want to add data to? Not knowing exactly what your data looks like, I can give an example using numpy.concatenate , which I hope will help:

 import numpy as np pixels = np.array([[3,3]]) pix = [4,4] pixels = np.concatenate((pixels,[pix]),axis=0) # [[3 3] # [4 4]] 
+1
source

pixels = np.array(pixels) in this row you are reassigning pixels . So this may not be a list. Although pixels not a list, it does not have append attributes. Does this make sense?

+1
source

Source: https://habr.com/ru/post/1265762/


All Articles