68

I am trying to run the code presented on the second page:

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00-introduction-to-computer-science-and-programming-fall-2008/video-lectures/lecture-20/lec20.pdf

At the bottom of the code you have to add these lines:

simFlips(100,100)

show()

Here is the error that I get when I run it on ubuntu:

Traceback (most recent call last):
  File "coin.py", line 36, in <module>
    simFlips(100,100)
  File "coin.py", line 16, in simFlips
    diffs.append(abs(heads - tails))
AttributeError: 'numpy.ndarray' object has no attribute 'append'

What am I doing wrong that gives me the last error?

starball
  • 20,030
  • 7
  • 43
  • 238
vahshi
  • 681
  • 1
  • 5
  • 3
  • 1
    For some reason your `diffs` variable is a numpy ndarray. Are you sure you left the line that says `diffs = []`? This sets diffs to be an empty python list, which you can call `append` on. – John Lyon Dec 07 '11 at 02:06
  • Works for me as written. – DSM Dec 07 '11 at 02:09
  • Yes I did, I just took the code from the pdf added the two lines at the bottom and started to get this error. – vahshi Dec 07 '11 at 02:20
  • DSM, how did you execute the code. On ubuntu or a different platform? – vahshi Dec 07 '11 at 02:21

4 Answers4

59

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

Community
  • 1
  • 1
Arpita Biswas
  • 607
  • 5
  • 3
2

Basically, numpy arrays don't have have access to append(). If we look into documentation, we need to use np.append(array), where array is the values to be appended.

import numpy as np

arr = np.array([1,2,3,47,1,0,2])

np.append(arr, 4)
MD Mushfirat Mohaimin
  • 1,966
  • 3
  • 10
  • 22
Ahmed Ali
  • 21
  • 1
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.

Eduardo Freitas
  • 941
  • 8
  • 6
0

You will have to use numpy function to append numpy arrays. Append will workk only for ordinary lists not numpy arrays.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 30 '21 at 03:38