AttributeError numpy ndarray object has no attribute append

0 votes

Hi Guys,

I am trying to append new  dataset in my array using append function, but It is showing me the error.

AttributeError: 'numpy.ndarray' object has no attribute 'append' 

How can I solve this error?

May 5, 2020 in Python by akhtar
• 38,260 points
52,940 views
Thank you. Your answer helped me to solve this error well.

3 answers to this question.

0 votes

Hi@akhtar,

If you trying to append data in your array, then you can use the below steps.

import numpy as np
arr = np.array([1,1,2])
arr1 = np.append(arr,4)
arr1
array([1, 1, 2, 4])
answered May 5, 2020 by MD
• 95,460 points
0 votes

Use numpy. append() to append an array to an empty array

  1. empty_array = np. array([])
  2. to_append = np. array([1, 2, 3])
  3. combined_array = np. append(empty_array, to_append) append `to_append` to `empty_array`
answered Dec 12, 2020 by Gitika
• 65,770 points
0 votes
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)???

So far ok. But you want to leave pixels as a list until you are done with the iteration.

pixels = np.array(pixels)
labels = np.array(labels)

You had this indention right in your other question.

Iterating, collecting values in a list, and then at the end joining things into a bigger array is the right way. To make things clear I often prefer to use notation like:

alist = []
for ..
    alist.append(...)
arr = np.array(alist)

If names indicate something about the nature of the object I'm less likely to get errors like yours.

I don't understand what you are trying to do with traindata. I doubt if you need to build it during the loop. pixels and labels have the basic information.

That

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

comes from the previous question. I'm not sure you understand that answer.

traindata = []
traindata.append(pixels)
traindata.append(labels)

if done outside the loop is just

traindata = [pixels, labels]

labels is a 1d array, a bunch of 1s (or [0,1,2,3...] if my guess is right). pixels is a higher dimension array. What is its shape?

Stop right there. There's no point in turning that list into an array. You can save the list with pickle.

answered Dec 12, 2020 by Rajiv
• 8,870 points

Related Questions In Python

0 votes
1 answer

AttributeError: 'numpy.ndarray' object has no attribute 'append'

Hi@Nilesh, You have created two empty lists. And ...READ MORE

answered Aug 11, 2020 in Python by MD
• 95,460 points
6,401 views
0 votes
1 answer

AttributeError: type object 'numpy.ndarray' has no attribute '__array_function__'

Hi@akhtar, I think numpy version is not compatible ...READ MORE

answered Apr 13, 2020 in Python by MD
• 95,460 points
14,749 views
+1 vote
1 answer

'numpy.ndarray' object has no attribute 'append'

Hi@Noman, You created two empty list. And in ...READ MORE

answered May 17, 2020 in Python by MD
• 95,460 points
8,190 views
0 votes
0 answers

AttributeError: 'numpy.int32' object has no attribute 'map'

can any one suggest solution data['isLarge'] = data.size.map({'small' ...READ MORE

Sep 5, 2020 in Python by Mohammed
• 120 points
4,604 views
0 votes
1 answer

How to create Pandas series from numpy array?

Hi. Refer to the below command: import pandas ...READ MORE

answered Apr 1, 2019 in Python by Pavan
3,513 views
0 votes
1 answer
0 votes
1 answer

How to create Pandas series from dictionary?

Here's a sample script: import pandas as pd import ...READ MORE

answered Apr 1, 2019 in Python by Prateek
2,331 views
0 votes
1 answer

How to convert pandas dataframe to numpy array?

Irrespective of whether the dataframe has similar ...READ MORE

answered May 13, 2019 in Python by Rishi
6,731 views
+1 vote
1 answer

AttributeError: 'numpy.ndarray' object has no attribute 'append'

Hi, You created two empty list. And in ...READ MORE

answered May 17, 2020 in Python by MD
• 95,460 points
5,513 views
0 votes
1 answer

AttributeError: 'numpy.ndarray' object has no attribute 'append'

Hi@Nilesh, You created two empty lists. And in ...READ MORE

answered Aug 11, 2020 in Python by MD
• 95,460 points
3,274 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP