How to get the True Negative Rate from this code?

I am in a larger code project and we have this one binary classifier. I want to calculate the TNR.

The major problem is that I don’t find the information about the variables in the code.
What is the benign rate, guesses and ad rate, guesses? And to calculate the TNR of that?
I am guessing that the TNR is TNR = 2*benign_rate/len(y_hat).

y_hat  = np.array([0, 1])
y_test = np.array([0, 1])

nr_not_detect_adv = 0
benign_rate = 0
benign_guesses = 0
ad_guesses = 0
ad_rate = 0
for i in range(len(y_hat)):
    if y_hat[i] == 0:
        benign_guesses += 1
        if y_test[i] == 0:
            benign_rate += 1
    else:
        ad_guesses += 1
        if y_test[i] == 1:
            ad_rate += 1

    if y_test[i] == 1:
        if y_hat[i] == 0:
            nr_not_detect_adv +=1

acc = (benign_rate+ad_rate)/len(y_hat)        
TP = 2*ad_rate/len(y_hat)
TNR = 2*benign_rate/len(y_hat)

precision = ad_rate/ad_guesses
recall = round(100*TPR, 2)
TPR = 2 * ad_rate / len(y_hat)

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Looking at the for loop, I’d guess that meaning of the variables is:

  • y_hat: predicted labels
  • y_test: ground truth
  • beningn_rate: TN (true negatives)
  • benign_guesses: TN + FN (false negatives)
  • ad_rate: TP (true positives)
  • ad_guesses: TP + FP (false positives)
  • nr_not_detect_adv: FN (yes, it’s redundant)

These lines should be fixed (definitions):

TP = ad_rate
TNR = benign_rate/benign_guesses

precision = ad_rate/ad_guesses
recall = ad_rate / (ad_rate+nr_not_detect_adv)
TPR = recall


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x