Wk2, Exercise 4


I’m doing Ex4 in Week2 and come up with this assertion error.
Not sure why it does that but I think I followed the instructions. Some help please?
This is my screenshot.

The assert statement in the test of your code is checking if b is of Python type float (i.e. a floating point number and not an integer. You have assigned it as an integer. You are one keystroke away from turning it into type float.

Also, I noticed that you used a Python list object as your argument to np.zeros(). This seems to work, but may cause some problems in other circumstances. Using a tuple (parentheses rather than square brackets) is the standard and safer choice.

Hi kenb,

Thanks for your reply. Do both tuple with parentheses and list with square brackets create the same vector in python?

In pure linear algebra terms, yes. But, since Python is a general programming language, neither lists nor tuples are not meant to be vectors as such. By that I mean, an ordered collection of real (or imaginary) numbers. They are heterogeneous object types that can store all kinds of objects: strings, dictionaries, other lists, … etc. NumPy is built on top of Python to facilitate vector/matrix/array operations ala linear algebra.

The main difference between tuples and lists is that the latter is mutable–lists can be modified after they have been initialized. Tuples, alternatively are immutable objects, i.e. they cannot be changed. Of course, the mutability of lists makes them incredibly useful. You have seen this in the assignments.

But, if a list is defined with the intention of never having it change, use a tuple instead. Since tuples cannot be changed, they require less memory. Relatedly, they also confer computing time advantages.

In all other respects, they are very much the same.

1 Like

Thank you very much for your detailed explanation!

I have an additional question regarding float type in backward propagation. I put in: db = 1/m*sum(A-Y). However, it came up with another assertion error, which pointed to the type of db.

I tried to put float() around the formula but it says: TypeError: only size-1 arrays can be converted to Python scalars.

You should be using np.sum() and not Python’s native sum() function. Also, note that the assert statement is looking for data type np.float64. You might instead use np.float64() to wrap your expression for db.