Select threshold using for loops

Part of my code below with error msg.


2
I don’t get why I’m getting this error. I have already define y_e as a (1, len(p_val)) vector. I’m assuming that p_val and y_val all have a shape of (1, len(p_val)). Been trying for several hours on this. I do not want to use vectorize manners to write this code since I am not familiar with that. I want to use the basic for loop method, thanks!

You have top check the shape of p_val, also know that list indexing starts from 0 so in your code here; y_e and p_val do not have the same size. y_e starts at 1.

How do I check the shape of p_val? I’ve tried printing it out in Jupyter notebook by adding a line of print(p_val.shape) but it doesn’t show up, thanks!

Check the len(p_val) as it is a list not a numpy array, method shape is not available for lists, or you can print p_val directly. I think your error is because you are starting y_e at (1, len(p_val)), try leaving it as just (len(p_val)) as this starts from index 0.

Hi @evilyoda, the error is because m = p_val.shape will give a tuple containing two values, number of examples and number of features in an example.
You should instead use m = p_val.shape[0] to get single value for m.
And posting code at discussion forum is against course rules, so please remove the code.

Hi @ritik5 ,

Thanks for the tip. I deleted one of the reply with code, but I cannot delete the original post, do I flag it and wait for moderators to delete it?

Also, most of the time I have a coding problem, do I just post the error first? How do I let people know the context of my problem without giving the code? Thanks!

Hi @evilyoda, yes you can post error first, if your error is not resolved here, then I think you can send your code to mentor in dm, and you can get further help.

Need some help.
1
So it seems like the divider is zero. I’ve checked the last iteration and came up with tp = 9, fp = 296, fn = 0. Even with fn at zero, the divider should not be zero. The method I used to calculate tp, fp, fn is by adding and subtracting y_e (based on 𝑝(𝑥)<𝜀p) by y_val, and then use np.count_nonzero() to count, for example np.count_nonzero(array_add == 2) should give me tp since both array should be 1, which would add to 2, and then counting the frequency of 2 in that array_add (adding y_e and y_val). RIght now I’m assuming a certain epsilon makes my (tp+fp) equal to 0, is there any way to check this?

Hello @evilyoda,

The best way to debug is to add some print lines. Please add the following one before calculating prec:

print(epsilon, tp, fp, fn)

Below are the first few lines of my printed results, and I hope you are seeing the same:

4.513250930309714e-36 0 0 9
8.990852779269493e-05 7 0 2
0.00017981705558538986 7 0 2
0.0002697255833780848 7 0 2
0.00035963411117077973 7 0 2
0.0004495426389634747 7 0 2
0.0005394511667561696 7 1 2
0.0006293596945488645 7 1 2
0.0007192682223415595 7 1 2
0.0008091767501342544 7 1 2

Your code has a variable called array_sub which is not suggested in the hints.

If you are not seeing the same printing results, then I would ask you to comment out your current code, and follow the hints to pass the tests first. With a passed solution, you may then investigate the difference in your current version to find out why it didn’t work.

With my print line, I expect to see tp + fn = 0 right before the devision by zero error occurs again.

Cheers,
Raymond

Thanks, I think I need to move on first with the suggested hint and investigate later, been stuck here for several days. Is there a way to continue the loop even if an error occurs? I don’t remember if it’s “pass” or something else

There is Python syntax which allows us to bypass an error, but if our code can’t compute the answer, then bypassing the error won’t give us the correct answer either, and consequently we can’t pass the grader of the assignment. If you have passed the assignment with the hints then I agree that we can investigate later.

I have passed the assignment and would like to investigate this!