C1_W3_Logistic_Regression assignement: TypeError from UNC C2 compute cost function

Im getting a “TypeError: unsupported format string passed to numpy.ndarray.format” from the two the test functions for the # UNQ_C2
after the compute_cost function. I’ve attached the screenshot of the error msg as well as the code part for better clarity…
After this the rest of the codes are all okay…but because of this error my submission failed. Have been trying to figure out the issue but have no success so would appreciate any help in pointing out the issue or direction to get it solved. Thanks.

1 Like

Please do not post your code on the forum. That is not allowed by the Code of Conduct.

I suspect an error in your use of indices in the for loops.

Try adding some code that prints the size of the cost value.

1 Like

Have you chosen correct specialisation? seems like this is from machine learning specialisation

please confirm the specialisation, course, and week.

Kindly do not mix up the category section, for now I am making the correction. but in any case you are not sure or unable to find the course specific name in category, mention it in the post with explanation about which specialisation, course and week

Yes i have entrolled on both courses…but this error is from Machine learning specialization. Should i delete and repost it there?

1 Like

At the error, it is trying to print a single floating point value. A vector would make it error.

1 Like

Please do not share any codes images from future, that is against community guidelines. You can always use edit option to do the correction, for now I will remove the image.

the way you have initialised cost and total cost to 0 is incorrect. you are only suppose to initialise cost to 0

Then in the ith iteration, you initialisation is again incorrect, instruction clearly mentions first to calculate z_wb but you used z_wb_i.

in j iteration again the second statement is incorrect.

There are some hint section in these exercise which help you do correction iteration, kindly go through them.

Regards
DP

Does this mean that the compute_cost() function is returning an ndarray instread of float? i’ve declated total_cost = 0 like this at the beginning and it gets returned at the end of the function. i assumed that it is a scalar and should be ok.

1 Like

Thank you for the response. I’ve removed the initialisation of the total_cost variable. The z_wb_i variable was just my naming of the variable instead of z_wb as i wanted to add the i for my own clarify. But anyways, i changed that and followed the same convention from the hint section… But i still get the same error though.

1 Like

It’s not only about naming, the way you have iterated is creating error.

You cant just use that name as z_wb_i in that equation is not same as z_wb for that cell.

And then while j iteration with the first condition was correct, the second condition again was incorrect.

First let me know what is the difference between z_wb_i and z_wb? And also z_wb_ij?

If you understand the difference, you will understand your error

Well, with all due respect, as we are using z-wb inside the i’th iteration and calculate z_wb for each iteration of i, i chose to name it as z_wb_i and as we have another j’th iteration inside this i’th iteration, i’ve used the z_wb_ij. But that doesnt seems to be the issue here as i have changed all and now it looks like exactly in the hint section… but still i get the error. so i guess im missing something else here and not able to figure it out yet.

first the loss_sum/cost need to be initialised to 0 (which you did correctly)
but then you also initialised total cost to 0 which was incorrect

then while creating loop over each training example
the first calculation is to be done is
z_wb = w[0]*X[i][0]+…+w[n-1]*X[i][n-1]+b and not z_wb_i

then next while creating loop over each feature
you first calculation the feature for z_wb_ij which was correctly done by you
then under the same loop you were suppose to add the above calculation to the z_wb(here again you made mistake by as you used z_wb_i which meant you adding to the training examples and not to the main sigmoid equation.

then next step where tells you to add bias to z_wb, you again used the training example z_wb_i which is incorrect.

then in next step cost needed to be calculated for training example and then in the next step you had use this cost to add to initial cost you initiated to 0

this part is explained very well after the code cell on how to implement it, you can check there once.

Regards
DP

can we skip the naming convention part, as it seems to be some miscommunication here and its not leading to any solution :slight_smile: Like i said, i have ranamed the variables as it was stated in the hint section and still i get this error. Perhaps either you or someone else take a look at the changed code (if that is permitted in the platform), that would be helpful. thanks again.

I understood your naming part but you are not understanding here rename it as z_wb_i instead of z_wb is creating error at some places.

1 Like

Use the below hint, to understand why you using z_wb_i instead of z_wb is creating error and also at the end where you calculated cost and then added to the cost is missing a step

No Mam, you are not getting what im saying. i just used a different variable name z_wb_i instead of z_wb but i was using it consistently all over the function and this same function was again called later below from compute_cost_reg function and it worked fine there. Also i printed the total_cost before returning and it did print the same expected cost but with more decimal points as it was not formatted for 3 digits. And again after remaning all the places i used z_wb_i to z_wb i still get the same error. As TMosh pointed out there, it seems like a float vs array issue but i have no idea what exactly is wrong.

Can you DM what correction you made??

click on my name and then message.

Sure will do. thanks

1 Like

Hello @jailani

In the first grader cell where we define sigmoid(z)

Can I know if you have followed the below instruction

  • z is not always a single number, but can also be an array of numbers.
  • If the input is an array of numbers, we’d like to apply the sigmoid function to each value in the input array.

Based on discussion, seems like either you got an obsolete copy and need to update lab and get a fresh copy.

No matter what the error did come from grade cell 1, yet your calculation in grade 2 was also not correctly implemented as you could see the cost value after making the correction but the ndarray issue persisted and that’s when we could track it to grade 1 cell as it had some interesting code added already as per your given information.

To get a new copy, delete the copy of assignment from your coursera Jupyter environment by click first File===open===delete the assignment file.
You will notice 404 error on screen page.
Click on Help on the top right corner, click reboot.
Once rebooting is done, click help again, then Get Latest Version, and then Update Lab. you will have a fresh copy.

Regards
DP