W3 Logistic Regression Practice Lab: Stuck on predict function

In the final lab of Week 3 for the Supervised ML course, under section 2.8 Evaluating logistic regression, Exercise 4, I’m asked to complete a predict function. I cannot find any situation in the previous labs where we were introduced to this predict function, and therefore do not know how to structure it, as I have no examples to go from.

I have followed the hint instructions which start out with “# Calculate f_wb (exactly how you did it in the compute_cost function above)”, so I copied and pasted my compute_cost function exactly as I had it above, which causes a lot of errors:

AssertionError                            Traceback (most recent call last)
<ipython-input-71-568191cf1844> in <module>
     10 # UNIT TESTS
---> 11 predict_test(predict)

~/work/public_tests.py in predict_test(target)
     69         raise ValueError("Did you apply the sigmoid before applying the threshold?")
     70     assert result.shape == (len(X),), f"Wrong length. Expected : {(len(X),)} got: {result.shape}"
---> 71     assert np.allclose(result, expected_1), f"Wrong output: Expected : {expected_1} got: {result}"
     73     b = -1.7

AssertionError: Wrong output: Expected : [1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0] got: [0. 0. 0. 0. 0. 0. 0. 1.]

Update: After trying a few different things, I now have a smaller error that is

TypeError                                 Traceback (most recent call last)
<ipython-input-126-568191cf1844> in <module>
      5 tmp_X = np.random.randn(4, 2) - 0.5
----> 7 tmp_p = predict(tmp_X, tmp_w, tmp_b)
      8 print(f'Output of predict: shape {tmp_p.shape}, value {tmp_p}')

TypeError: predict() missing 1 required positional argument: 'b'

Please advise.

Thank you.

I think you are changing the code which you are not supposed to do. Are you?

Remember that you only need to write your code between the two commented lines and not change the other code:



Oh yes. I’ve definitely changed code I’m not supposed to change. However, even then, I still don’t have access to an example predict function.

Please get a fresh copy of your notebook, as instructed here and do your code again (or copy it from your old notebook) without changing the pre-written code.

Ty for the info! I’ve created a fresh notebook and was able to input the right code for the predict function. Ty!

I am glad you solved the issue…

1 Like