Doubt in C3_W1_Anomaly_Detection

In exercise 2 of this lab where we are writing function to choose epsilon,
It works perfect when I use the vectorised implementation. But my doubt is, during the first iteration of the outer for loop, the values of fn=9, tp=0,fp=0 (AS i am printing them in the function I was able to see them), but yet the precision which has denominator as tp + fp which is equal to zero doesnt flag zero by division error. Rather the code runs through next iteration of epsilon.

But when I implement the above code in normal version (With an inner for loop) it flags division by zero error right after the first iteration of inner loop.

Kindly help if I am missing something

Hi Waze,

Thanks for sharing your concerns. I tried to reproduce your error also adding some prints in the for loop. I did not have any Division by zero Error but got a NaN value for precision. Sharing with you some of the values of the problematic iteration:

tp = 0
fn = 9
fp = 0
prec=nan
zero denominator
F1=nan

I suggest that you use a try/catch to capture the exception and then set manually maybe the precision to a NaN value. However it would be nice that you review the whole code just in case. Use the hints of the lab to inspire you. :slight_smile:

Let me know if that solves the issue.
Happy learning,

Rosa

Best.

1 Like

The problem is with the type of the 0 value.
Dividing by original types will cause error but for example “np.float64” type will give you the result of a single NaN.
The hints are using numpy so they will get automatically the right format…

1 Like

Hi @arosacastillo,

Thanks a lot for giving me the insight about Nan value which you got for precision and F1 in first iteration. I used try and except to change the values of precision and F1 to 0 when ZeroDivisionError occurs.

I was then able to recall Dr. Andrew’s lecture on precision and recall where he mentioned we have to consider precision as 0 even when we get denominator and numerator as 0. Here in this case, we got denominator as 0.

Numpy is implicitly very clear in handling 0/0, rather than throwing error, it sets the value to Nan.

1 Like

Hi @Adam_Litter,

Indeed you are right. Even for np.int64, 0/0 gives Nan. Numpy is intelligent. Anyway, thank you for your insight.

hi, Wazw:
I have the same doubt when doing this exercise as well. So in conclusion, the 0/0 problem is solved by Numpy implicitly right? While in a normal for loop way the error will occur. Also, I have an alternative way to solve this problem by adding a small postive value np.finfo(float).eps to the denominator, in this way, the denominator will always be greater than 0 yet not affect the result.

1 Like