close

[Solved] AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’ Error Occurs?

Today I get the following error AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’ in python.

How To Solve AttributeError: ‘numpy.ndarray’ object has no attribute ‘append’ Error ?

  1. How To Solve AttributeError: 'numpy.ndarray' object has no attribute 'append' Error ?

    To Solve AttributeError: 'numpy.ndarray' object has no attribute 'append' Error Use numpy.concatenate(list1 , list2) or numpy.append() Look into the thread at Append a NumPy array to a NumPy array.

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

    To Solve AttributeError: 'numpy.ndarray' object has no attribute 'append' Error Use numpy.concatenate(list1 , list2) or numpy.append() Look into the thread at Append a NumPy array to a NumPy array.

Solution 1


I got this error after change a loop in my program, let`s see:

for ...
  for ... 
     x_batch.append(one_hot(int_word, vocab_size))
     y_batch.append(one_hot(int_nb, vocab_size, value))
  ...
  ...
  if ...
        x_batch = np.asarray(x_batch)
        y_batch = np.asarray(y_batch)
...

In fact, I was reusing the variable and forgot to reset them inside the external loop, like the comment of John Lyon:

for ...
  x_batch = []
  y_batch = []
  for ... 
     x_batch.append(one_hot(int_word, vocab_size))
     y_batch.append(one_hot(int_nb, vocab_size, value))
  ...
  ...
  if ...
        x_batch = np.asarray(x_batch)
        y_batch = np.asarray(y_batch)
...

Then, check if you are using np.asarray() or something like that.

Solution 2

Use numpy.concatenate(list1 , list2) or numpy.append() Look into the thread at Append a NumPy array to a NumPy array.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read