FAQ: Logistic Regression II - Prediction Thresholds

This community-built FAQ covers the “Prediction Thresholds” exercise from the lesson “Logistic Regression II”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Scientist: Machine Learning Specialist

Machine Learning: Logistic Regression

FAQs on the exercise Prediction Thresholds

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello, seems like the lesson misses explanation on how to solve question #3. I see the solution, but there is no explanation what is what. The way fn_rate formula is constructed is confusing.

#3. Calculate the false negative rate for each threshold

fn_rate = [1 - sum(y_pred_prob[y_test==1][:,1]>t)/pos_cases for t in thresh]

#Find the first threshold value where the is greater than 2 per 100

max_thresh = thresh[np.argmax(np.array(fn_rate)>0.02)]

print(f’Max Threshold for less than 2 per 100 FNs:{max_thresh}')

1 Like

If you have course quality suggestions or bug reports you can report them either here or here.

For anyone struggling with the poor explanation in part 3 of this exercise, I decided to rewrite the code provided in the solution into a function which I think is far more logical, albeit longer, given the context of the lesson so far including my comments on how each step works.

def get_min_thresh(y_pred_prob, min_false_negative_rate):
 
    thresh = np.linspace(0, 1, 100) # creates array between 0 and 1

    for t in thresh:
        y_pred_class = (y_pred_prob[:,1] > t) * 1.0 # calculates predicted classes for given threshold
        confusion = confusion_matrix(y_test, y_pred_class) # computes confusion matrix for given threshold
        fn_rate = confusion[1,0] / (confusion[1,0] + confusion[1,1]) # fn_rate = fn / (fn + tp)  
      
      if fn_rate >= min_false_negative_rate:
        print(f'Minimum Threshold for {min_false_negative_rate*100} false negatives per 100: {t:.2f}')
        return t

get_min_thresh(y_pred_prob, 0.02) # returns 0.22 for fn rate of two per hundred positive cases

I hope this is enough until Codecademy update this lesson for more clarity and better understanding.

1 Like

I’m not sure im getting the y_pred_class = (y_pred_prob[:,1]>0.25) * 1.0
part of this how does that code go through each item in y_pred_prob?
If that’s what its doing how is it then multiplying something like 9.44871111e-04 * 1 and getting 1 or 0