Week 2 Exercise 6 "'numpy.ndarray' object has no attribute 'append'"

Any idea why I am getting this error? Is costs not a numpy array? Exercise 5 runs properly but then I get this error after exercise 6 when using the line “costs.append(cost)”. Thanks for any help!

Because your costs array is a numpy array that does not support append method. But, in fact in this case it should be a list of cost. I would recommend checking how you are setting costs array. It may be array also, but for these exercises, it is a list.


Hi thanks,
Exercise 5 (the propagate function) ends with this line…
which was already written in the code, not written by me… why would they have this line making costs a numpy array but then the following exercise 6 tells me that I can’t use append with the numpy array, although that line “costs.append(cost)” is also written by them? Should I be changing it somewhere between these 2 points? Thanks for your help I hope I’m making sense.

Yes, you are making sense but if you look at how the cost is calculated, you will see that we always deal with numpy arrays as our input arrays. And a cost value is computed using those arrays. So, it’s safe to deal cost with the above method so as not to lose any info. I think, your problem may be in how you compute your cost. Maybe it’s not returning a length one array. Everything runs well if all the mathematical equations are implemented well keeping in regard how numpy arrays work.

But the return value from propagate is just one cost value for a particular iteration right? The costs variable is local to the optimize routine. It was initialized for you to be an empty list in the template code. You must have modified that code. If you want to get the clean copy of the notebook to compare what was there originally, there is a topic about that on the FAQ Thread.

Or maybe you gave the wrong variable name to hold the return value from the call to propagate.