C3_W1 practice lab2 q2: select_threshold

I am getting an assertion error: Wrong best_epsilon. Expected: 0.04 got: 0.28014.


if your function is implemented correctly, it should compute that when epsilon is roughly 0.04, the F1 score becomes the highest, whereas in your function, it says not. I suggest you to check your work again but with the hints given right below the exercise cell to find out and understand any differences. Also, make sure the below function’s skeleton is not changed:

# UNQ_C2
# GRADED FUNCTION: select_threshold

def select_threshold(y_val, p_val): 
    Finds the best threshold to use for selecting outliers 
    based on the results from a validation set (p_val) 
    and the ground truth (y_val)
        y_val (ndarray): Ground truth on validation set
        p_val (ndarray): Results on validation set
        epsilon (float): Threshold chosen 
        F1 (float):      F1 score by choosing epsilon as threshold

    best_epsilon = 0
    best_F1 = 0
    F1 = 0
    step_size = (max(p_val) - min(p_val)) / 1000
    for epsilon in np.arange(min(p_val), max(p_val), step_size):
        ### START CODE HERE ### 
        ### END CODE HERE ### 
        if F1 > best_F1:
            best_F1 = F1
            best_epsilon = epsilon
    return best_epsilon, best_F1

Let us know what you find.


I have checked this but i think everything is fine but code in still showing the same problem.

can you send the below cell because my code is showing -

Best epsilon found using cross-validation: 8.981862e-02
Best F1 on Cross Validation Set: 0.985025

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.28014

My mistake was that recall wasn’t calculated correctly and I got the same error message. I was able to get past that but for me it fails on a different test.

assert np.isclose(best_F1, 0.8888888), f"Wrong best_F1. Expected: 0.8888888 got: {best_F1}"

It passes epsilon assertion but fails at the best_F1 score. I think the assertion about best_F1 is wrong. When epsilon is 0.04002 I get 5 true positives, 1 false positive and 0 false negative. Which gives precision a value of 0.833 and recall 1.0 for an F1 score of 0.909

Sorry. There was a bug in my code. I wasn’t computing true positives correctly.