The Question about C1_W3_Lab05_Cost_Function_Soln

Why do you don’t use
cost = cost / - m outside cycle for in the compute_cost_logistic()?

Instead, you multiply every Cost’s element inside cycle ‘for’:

for i in range(m):
        cost +=  - y[i]*np.log(f_wb_i) - (1-y[i])*np.log(1-f_wb_i)

I don’t understand, because in the lesson “Simplified Cost Function for Logistic Regression” I see that

and if I try multiply expression to ‘-’ after calculate cost, like this:

for i in range(m):
        cost +=  y[i]*np.log(f_wb_i) - (1-y[i])*np.log(1-f_wb_i)

total_cost = cost/-m

I get another result by comparison when I multiple to “-” inside cycle for.

I will be grateful for your answer.

Hi Tata_P!
I request you to post your queries under specific tags so that you can get help quickly and your post doesn’t get lost in the lot. For example, if the query is regarding some assignment in the MLS, then post it under MLS (Specify course and assignment number too) not in general discussions.
And coming to your question, you are making this silly mistake here so that you are getting a different answer :

-(1/m)*(y[i]*np.log(f_wb_i) + (1-y[i])*np.log(1-f_wb_i)) is equivalent to (1/m)*(-y[i]*np.log(f_wb_i) - (1-y[i])*np.log(1-f_wb_i)) ,
not (y[i]*np.log(f_wb_i) - (1-y[i])*np.log(1-f_wb_i)). Else, the implementation is the same, so it doesn’t matter if you are multiplying -1 outside or inside.


Hi, Nithin!
Thank you for your answer.

  1. I corrected tags for this post, as you recommended :+1:
  2. You are right: i didn’t see next “+” :weary:

Now I double-checked - all calculations have converged!
Thank you for prompting! :slightly_smiling_face:

Have nice day! :wave: