Week 2 exercise 4 doubt

Getting the following error for using np.zeros(dim) and b=0. Plz help

AssertionError Traceback (most recent call last)
in
6 print ("b = " + str(b))
7
----> 8 initialize_with_zeros_test_1(initialize_with_zeros)
9 initialize_with_zeros_test_2(initialize_with_zeros)

~/work/release/W2A2/public_tests.py in initialize_with_zeros_test_1(target)
19 assert b == 0., “b must be 0.0”
20 assert type(w) == np.ndarray, f"Wrong type for w. {type(w)} != np.ndarray"
—> 21 assert w.shape == (dim, 1), f"Wrong shape for w. {w.shape} != {(dim, 1)}"
22 assert np.allclose(w, [[0.], [0.], [0.]]), f"Wrong values for w. {w} != {[[0.], [0.], [0.]]}"
23 print(‘\033[92mFirst test passed!’)

AssertionError: Wrong shape for w. (3,) != (3, 1)

The tests are sending a few `AssertionError`s here. These errors are thrown by the unit tests used to help you debug your code. Read through them carefully; they are your friends.

Line 19 reports that you have probably specified model parameter b as a Python type `int` rather than as type `float`. For example, I could write `x = 7` or I could write `x = 7.0`. A language such as Matlab doesn’t care–they would both be interpreted as floating point numbers.

Python, however, makes the distinction. When it comes to mathematical analysis (any numbers that we want to apply mathematical operators to), we are almost exclusively interested in floating points. In which case, we want to use the latter statement: `x = 7.0`. It is also acceptable (and more concise) to write `x = 7. It's the popular choice too. ` Also, any `int` can be converted to a `float` with the built-in function `float()` as in: `x = float(7)`, which of course, is way less concise!

And then you have the wrong shape for w. You have produced an array of dimension-0, i.e, giving dimensions (3,), where an array of dimesion-1 is required, i.e. dimension (3, 1). The instructions to the exercise ask you to look up `np.zeros()` in the Numpy library documentation. Please do!

1 Like