Programming Assignment: Week 3 practice lab: logistic regression

I am having issues with my code for exercise 4,5,6
the error msg for exercise 4
“NameError: name ‘np’ is not defined”
exercise5 and 6
NameError: name ‘map_feature’ is not defined
{moderator edit: code removed}

{moderator edit: code removed}

{moderator edit: code removed}

pls how do i fix it

Welcome to the MLS community!

Every time you open the Notebook to work on the assignment, you must start by running all of the cells in the beginning of the notebook first.

The functions you are getting errors for are all defined when you run the first few cells in the notebook.

And, just a reminder: Please do not post your code on the forum. That’s not allowed by the Community Standards. You can post any error messages or asserts, but please do not post your code.

I have edited your message to remove the code.

1 Like

Also, one other hint about your code.

Python uses indentation to group blocks of code. You have to be extremely careful that everything you want to execute as a block all has the same indentation.

I think you have several places that need the indentation corrected.

Hi. i did what you said and am still getting errors

For the “data type not understood” error, (my original reply edited), see the reply below from Osama.

For the “X_train is not defined” errors, I suspect you did not run the cell in section 3.2 that imports the data set and creates the X_train and y_train variables.

I was able to fix exercise 5&6 but I am still having issues with exercise 4

you have to put your desired input shape in a tuple like this:
p = np.zeros((1,m))

Why am i getting this error msg?
how do i fix it

you have a wrong shape for the predict argument, check it and try to use the reshape method with the right dimensions. if you still can’t get it right show me how did you calculate the shape of it

1 Like

I don’t get what you mean. seems like my code is correct for some reason its not just working.
How do i fix it?

Hello @Precious_usoroh. Like Osama_Saad_Farouk, this error is due to mismatch of dimension of output vector and what is expected.
Output dimension should be of type (m,) instead of (1,m). So when you initialize the prediction vector use np.zeros(m) instead of np.zeros((1,m)). I think that will solve your problem.


i did that, and still its not working

Hello @Precious_usoroh, after changing p = np.zeros(m), are you getting same error which you were getting earlier?
If not then I think your implementation of looping over example may be wrong. Can you share the error that you are getting now? Or you can click on hints to get some hints on how to write the code for loop.

Hi! It seems like you’ve modified a line or more that is outside of the ### START CODE HERE ### and ### END CODE HERE tags of the graded cells. Initializing the p variable should already be given in the blank notebook so no need to change it. That might explain why you’re getting the errors. You can see other gotchas here.

Please revert all those changes for easier debugging and to avoid having problems with the grader later on. If you can’t track which lines you edited, I recommend these steps:

  1. Rename your current notebook and get a fresh copy (instructions here). The fresh copy should have the original filename: C1_W3_Logistic_Regression. Please do not change it.

  2. You can copy your previous solutions into the new notebook. But take note that you should not modify anything outside the ### START CODE HERE ### and ### END CODE HERE tags. You should only put your solutions between those markers.

  3. As Tom mentioned, when running an exercise, you should first run all the cells before it. That usually prevents the <something> is not defined errors.

  4. If you’re getting an error or your output does not match the unit tests, you can first look at the hints placed within the notebook. Click the green Click for hints prompt at the bottom of the graded cells to see if the information there is useful. Within that, there is sometimes a blue Click for more hints that provides even more information.

  5. If those do not help, please post here a screenshot of the error message you’re getting (like what you’re already doing) so we’ll know what might be wrong.

Hope these help! I have a feeling the root of the errors right now is due to the modified boilerplate code. Once those are reverted and you focus only on what’s inside the START and END code tags, I think we’ll be able to help you better. Let us know how it goes. Thanks!

NameError Traceback (most recent call last)
3 initial_b = 0.
----> 5 dj_db, dj_dw = compute_gradient(X_train, y_train, initial_w, initial_b)
6 print(f’dj_db at initial w and b (zeros):{dj_db}’ )
7 print(f’dj_dw at initial w and b (zeros):{dj_dw.tolist()}’ )

in compute_gradient(X, y, w, b, *argv)
22 for i in range(m):
23 # Calculate f_wb (exactly as you did in the compute_cost function above)
—> 24 f_wb = sigmoid(z_wb)
26 # Calculate the gradient for b from this example

NameError: name ‘z_wb’ is not defined

Does your code define the z_wb variable?

It does now.

1 Like

Post back here is you need more help.

AssertionError Traceback (most recent call last)
—> 10 compute_cost_test(compute_cost)

~/work/ in compute_cost_test(target)
24 b = 0
25 result = target(X, y, w, b)
—> 26 assert np.isclose(result, 2.15510667), f"Wrong output. Expected: {2.15510667} got: {result}"
28 X = np.random.randn(4, 3)

AssertionError: Wrong output. Expected: 2.15510667 got: 0.2665604678925331