Assertion error in C1W3LogisticRegression Lab

New to Python and seeking help from a mentor:

Unit Test for computing the cost resulting in wrong output (assertion error). I’ve double checked all my indentations, and up until this point my code has worked. Seeing as this specific section does not ask me to write my own code, I assume I made a mistake earlier on. First, I checked my Z_wb value, and found that removing a “+” from “+=” “inizialized” the value. However, I’m still receiving an assertion error. I also tried removing the “+=” from the “z_wb = z_wb_ij” function, this changed my result (expected), but again did not resolve the error.

Any help would be appreciated.

I’ve included a screenshot of my error for reference:

@Wendy & @TMosh were so helpful last time I posted, if either are available please send a DM

Your compute_cost() function does not return the correct cost value.

The = operator assigns a value to a variable.

The += operator adds the value on the right to the variable on the left. It is equivalent to using "<variable> = <variable> + <value>"

How could I have computed the correct cost function for initial_w and initial_b, but not for test_w and test_b?

As I understand, the function is the same for both, just with different variables…
X_train and y_train are attributes of the data set, so they’re not the cause of this error. Test_w and Test_b values were provided, so they’re also not the cause of this error. Where should I be looking instead?

Passing a test where the variables are 0 is very easy. It doesn’t prove whether your math is correct.

I recommend you not use a lot of for-loops, it’s very trick to get all of the indentation and indices correct.

And it’s not necessary. You can use the numpy functions like and np.multiply() to do the necessary calculations without any for-loops at all.

I’m using in my math functions, they’re inside one for-loop

I seemed to have messed everything up while editing, now things that worked are not

Start over.
Try it without any for-loops.

The reason @TMosh is suggesting trying this without for-loops is that the logic is much simpler if you rely on functions like to handle the dimensions. For example:

  •, w), where X is shape (m,n) and w is shape (n,) will give you a result of shape (m,)
  • of two values of shape (m,) will give you a scalar result

Taking advantage of this will let you get rid of the for-loops, resulting in code that’s easier to follow and much less prone to hard-to-debug errors.

Unfortunately, the hints for this function use for-loops, so you’ll have to take that into consideration. You can still look at the hints for ideas, but just for basic concepts - you won’t need any of those loops.

If you do end up trying to write this without loops and get stuck, feel free to ask questions. When you get it working, compare your result to the hints - you will be amazed at how much simpler yours is.

1 Like