# Computation of t\_p, f\_p, t\_n (threshold detection in week 1, lab2 , exercise 2)

**URL:** <https://community.deeplearning.ai/t/computation-of-t-p-f-p-t-n-threshold-detection-in-week-1-lab2-exercise-2/439197>\
**Category:** Unsupervised Learning, Recommenders, Reinforcement\
**Tags:** week-module-1\
**Created:** [September 7, 2023, 12:09pm UTC](https://community.deeplearning.ai/t/computation-of-t-p-f-p-t-n-threshold-detection-in-week-1-lab2-exercise-2/439197 "2023-09-07T12:09:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juergen\_Geiser](https://avatars.discourse-cdn.com/v4/letter/j/8491ac/32.png) [@Juergen\_Geiser](https://community.deeplearning.ai/u/Juergen_Geiser)\
**Post date:** [September 7, 2023, 12:09pm UTC](https://community.deeplearning.ai/t/computation-of-t-p-f-p-t-n-threshold-detection-in-week-1-lab2-exercise-2/439197/1 "2023-09-07T12:09:18Z")

</div>

Dear colleagues,

I am not sure, how to compute the t\_p, f\_p an t\_n values.  
Did I have to summerize the values of the detected and non detected.

I am stuck, please give me a hint,  
how to compute the values t\_p, f\_p and t\_n

Thanks,

Juergen

---

<div class="post-metadata">

**Author:** ![Wendy](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/wendy/32/8167_2.png) [@Wendy](https://community.deeplearning.ai/u/Wendy)\
**Post date:** [September 7, 2023, 6:20pm UTC](https://community.deeplearning.ai/t/computation-of-t-p-f-p-t-n-threshold-detection-in-week-1-lab2-exercise-2/439197/2 "2023-09-07T18:20:17Z")

</div>

Hi, @Juergen_Geiser!

Some comments about how to think through computing t\_p, f\_p and t\_n:

The instructions for Exercise 2 explain what tp, fp, and tn are:

- 𝑡𝑝 is the number of true positives: the ground truth label says it’s an anomaly and our algorithm correctly classified it as an anomaly.
- 𝑓𝑝 is the number of false positives: the ground truth label says it’s not an anomaly, but our algorithm incorrectly classified it as an anomaly.
- 𝑓𝑛 is the number of false negatives: the ground truth label says it’s an anomaly, but our algorithm incorrectly classified it as not being anomalous.

So, taking _tp_ as an example: _tp_ is the number of true positives, and a value is a true positive if the ground truth label says it’s an anomaly, and our prediction correctly classified it as an anomaly.

The _select\_threshold()_ function is given these parameters:

- y\_val - an ndarray of the ground truth labels
- p\_val - an ndarray of our predictions

The instructions for Exercise 2 also explains:

- Recall that if an example 𝑥 has a low probability 𝑝(𝑥)\<𝜀, then it is classified as an anomaly.

How can you use this info to find the number of true positives: the number of values where the ground truth says it is an anomaly and the probability also says it is an anomaly?

Once you have your idea of what you need to calculate, think about how you can implement it in code. If you get stuck on ideas for the coding, try clicking on the green “Click for hints” below the exercise. If that is still not enough, there will be some further hints you can click on for more detailed help on specific parts of the coding you might be stuck on.

---

<div class="post-metadata">

**Author:** ![Juergen\_Geiser](https://avatars.discourse-cdn.com/v4/letter/j/8491ac/32.png) [@Juergen\_Geiser](https://community.deeplearning.ai/u/Juergen_Geiser)\
**Post date:** [September 8, 2023, 6:32am UTC](https://community.deeplearning.ai/t/computation-of-t-p-f-p-t-n-threshold-detection-in-week-1-lab2-exercise-2/439197/3 "2023-09-08T06:32:27Z")

</div>

Dear Wendy,

many thanks, it works perfectly, also with the fast coding of Python

By the way, I learned Python with a more, I would say “extended” coding (for-loops etc).  
So, I see the short and compressed commands, which I could reconstruct in their  
functions, e.g.:

Fast coding with:

Predictions = (p\_val \< epsilon)  
(it creates an array with the dimension of p\_val with 0 or 1, if p\_val\<epsilon is true, then 1, else 0)

or

fp = sum((predictions==1)& (y\_val==0))  
it summerize all the entries of the array (dimension oft he prediction), when prediction==1 and y\_val==0

At least, I found out, how they will work.

But let me know a good book or pdf-file or online-sheet, where could I find such very  
short commands of Python. That will improve me also in Python (I think, I have to learn  
the very short and compressed commands, they are really very helpful)

Many thanks again, now I also found the tips related to the exercises, they are very helpful.

Best wishes

Juergen

---

<div class="post-metadata">

**Author:** ![Wendy](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/wendy/32/8167_2.png) [@Wendy](https://community.deeplearning.ai/u/Wendy)\
**Post date:** [September 9, 2023, 7:35pm UTC](https://community.deeplearning.ai/t/computation-of-t-p-f-p-t-n-threshold-detection-in-week-1-lab2-exercise-2/439197/4 "2023-09-09T19:35:44Z")

</div>

That’s great news, @Juergen_Geiser!

Good observations, too. The “fast coding” techniques you point out are ways to operate on a whole array or matrix, rather than looping through and operating on each value individually. Vectorized approaches like this typically run faster, in addition to being more concise. These come in very handy for machine learning where we use lots of matrices of data (tensors, arrays, …).

I don’t have any favorite book or online references for summarizing these types of techniques, but try googling something like “array operations python tips and tricks” or “python vectorization” to see what you find. Here’s one Medium article I noticed:

> **[Mastering NumPy: Tips and Tricks for Efficient Numerical Computing](https://moez-62905.medium.com/mastering-numpy-tips-and-tricks-for-efficient-numerical-computing-624d44b4bebd)**
>
> Maximizing NumPy’s Potential: Best Practices for Data Analysis and Scientific Computing

Also, check out the [MLS Resources](https://community.deeplearning.ai/c/machine-learning-specialization/mls-resources/279) category which has a whole range of helpful info for learners in this course. You may find some useful python recommendations there. And, it’s generally a good resource to know about. You can either search for keywords at the main MLS Resource level, or go to the [FAQ page](https://community.deeplearning.ai/t/faq-frequently-asked-questions-for-all-mls-courses/184322) and drill into whatever looks interesting or helpful from there.
