Week 3 practice lab: logistic regression. What does this code mean?


I’m a bit confused…

What should i do with this code?
It is a template code…
And overall, isn’t this the wrong code to compute gradient descent for logistic regression?
Because the “Optional lab: Gradient descent for logistic regression” showed different code and now i’m completely confused.

We aren’t allowed to delete any code that is already written in the lab, or?
So what should i do with this code and why it says “None”?

I’m either totally stupid, or completely confused.
I would appreciate any kind of help / clarification.

Your task is to replace the “None” statements with the correct code for each line.

1 Like

But am i allowed to add lines of codes? Or i’m only allowed to replace the “None” statements?

The lines of code that are in the template should be sufficient.
But what matters in the end is what values your function returns.
If you’re feeling brave, you can go off-script and write your own code.

1 Like

When I did this assignment, adding any extra code line didn’t get me the expected output. As told by TMosh you are only suppose to replace None with the correct code. Yes it is different code from the optional lab but use the hints given below the two grader cells. That will help you crack the code. Actually they have simplified the code with the given code lines but then the implementation needs to be done the way graders wants it. So keep trying. You can refer these post if getting confused

Hope it helps!

Keep Learning!!!


1 Like

Note that the grader never inspects the code you add - it only cares about the return values.

1 Like

Thanks for all the answers.

The thing which is confusing me, is that the template code uses z_wb…
while the optional lab that explained computing gradient for logistic regression used this code:

for i in range(m):
f_wb_i = sigmoid(np.dot(X[i],w) + b) #(n,)(n,)=scalar
err_i = f_wb_i - y[i] #scalar
for j in range(n):
dj_dw[j] = dj_dw[j] + err_i * X[i,j] #scalar
dj_db = dj_db + err_i
dj_dw = dj_dw/m #(n,)
dj_db = dj_db/m #scalar

this code works though, but differs from the “template” code.

When i look into the “hints” it uses other code again.
So i’m faced to three different codes “template code with none”, “optional lab” and “hint”.

I even feel a bit stupid to ask those questions… bc maybe the solution is obvious, but i really wonder what i don’t understand?

edit: The code explained in “optional lab” works and gives the expected output. But it differs completely from the “template code with none” why is that?

The “template code” is where you’re supposed to do your work. That’s the basis of the assignment.

There is more than one way to write the code to implement the calculations.

What do you observe by comparing the two methods given?

1 Like

Hello Bastian,

For the same confusion, I had sent you the links which if you had read in detail would have got the code.

You do need follow the template code given in assignment book as the grader wants you to follow the same code lines to crack or write the correct code accordingly in the way they want.

z_wb is where you getting confused must be for
f_wb = sigmoid(z_wb) apply this code only in compute cost but

for compute gradient f_wb is calculated based on this image

Also there is no err_i code given in the template code, so please remove that code line. Follow the links I shared in the previous comment. People have got stuck in the same lines and I have explained how to go about.

  1. You need to write this section
    dj_db_i =
    dj_db +=

    for j in range(n):
        dj_dw[j] = 

based on these hints

Hope it helps!!!

Keep learning!!!


1 Like