C3_W1_Anomaly_Detection assignment exercise 2

was working on the C3_W1_Anomaly_Detection assignment exercise 2
but I got this error when I ran my code. pls how do i fix it
AssertionError: Wrong best_epsilon. Expected: 0.04 got: 0.28971

Hi @Precious_usoroh.

We can’t tell you how to fix it because it is your assignment. Your code can run through but does not produce the correct result, so I would suggest you to follow this debugging tips to find out which line(s) of your code is wrong. Sometimes it is also good to take a break from it if you have spent long hours on the assignments.


PS: Moved your thread to the right category, and modified the thread’s title.

I tried what it, still did not work

It’s great that you have tried out the code debugging approach. Are you saying that you have printed all variables’ values, and you have verified that all those values are correct? They will be helpful in our future discussion because we can talk about the printed values.

yes the values are correct. I Tried THE debugging tips out still not working.

Hi @Precious_usoroh ,

I would say that there are 2 critical points in this exercise:

The first one is to select properly the different sets of predictions to evaluate on each iteration.

The second one is to correctly calculate the tp (true positives), fp (false positives) and fn (false negatives).

If you nail down these two steps above, then the rest is just a matter of applying the formulas provided in the exercise instructions, which are the formulas for ‘precision’, ‘recall’, and ‘F1’.

So please check out the ‘predictions’ used in each iteration, and the tp, fp, and fn of each iteration.

Hope this helps.

If you still have issues, I will be more than happy to check your code and try to provide a more focused hint - just send it to me via direct message.

Good luck!


1 Like

Hi @Kai_Zhang1 ,

Welcome to the community! This is your first post :slight_smile:

Regarding your issue, please check the formula for ‘rec’. That’s where your problem is. I hope this hint helps :slight_smile:

Also: Please delete your code. Posting code goes against the Honor Code of the community.



Same error and i don’t know what’s wrong, i think there might be a problem in the result verification function

Please copy your error here, and please remove your code.

AssertionError Traceback (most recent call last)
----> 8 select_threshold_test(select_threshold)

~/work/public_tests.py in select_threshold_test(target)
8 best_epsilon, best_F1 = target(y_val, p_val)
----> 9 assert np.isclose(best_epsilon, 0.04, atol=0.3 / 1000), f"Wrong best_epsilon. Expected: {0.04} got: {best_epsilon}"
10 assert best_F1 == 1, f"Wrong best_F1. Expected: 1 got: {best_F1}"

AssertionError: Wrong best_epsilon. Expected: 0.04 got: 0.28971

Hello @yazid_elbouachi,

We can only suggest how you could debug this.

If the code is correct, then we can expect it to loop over all epsilons, and then in each round, if the F1 score is better than the “best_F1” score, we make an update. Right? Now, the assignment has given these to you by default:

        if F1 > best_F1:
            best_F1 = F1
            best_epsilon = epsilon

I need you to add 3 lines there, but note that you will have to remove the added lines before any submission, OK? Otherwise, those lines can fail a submission.

        if F1 > best_F1:
            print('Great, a better F1 is found', epsilon, F1, best_F1) # This is an added line, and need to be removed before submission
            best_F1 = F1
            best_epsilon = epsilon
        else:  # This is an added line, and need to be removed before submission
            print('Poor, this one is not better', epsilon, F1, best_F1)  # This is an added line, and need to be removed before submission

OK, so after adding them, run it, and run again the test that complained about the error, this time you should see one print-line per epsilon, and observe the process of the algorithm finding the best F1. If anything is unusual there, you find a lead to the bug.

After you clear the bug, don’t forget to remove those added lines!

Good luck!

Hi @rmwkwok ,

I think this post is double. Here’s the other link:


As it turns out, the problem is in the indentation of the following line:

It was outside of the for-loop so it was taking only the last epsilon.

Thank you, Juan. Indeed!

PS: The link doesn’t work. It says the page doesn’t exist.