0
$\begingroup$

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

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])

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:

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

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

TNR = 2*benign_rate/len(y_hat)
$\endgroup$
2
  • 1
    $\begingroup$ You have the predicted (y_hat) and true (y_test) values, right? Then, TNR is np.sum((y_hat == 0) & (y_test == 0)) / np.sum(y_test == 0). $\endgroup$ Commented Apr 29, 2022 at 12:28
  • $\begingroup$ @alex I get this using your suggestion: RES:, AUC, ACC, PRE, TPR, F1, TNR, FNR RES:,100.0,90.7,100.0,81.4,89.75,100.0,18.6 How to countercheck this? and that it would be the solution I think :) It is btw the same as my current TNR $\endgroup$
    – Peter
    Commented Apr 29, 2022 at 14:02

1 Answer 1

0
$\begingroup$

The True Negative Ratio is the fraction of the correctly classified negative samples from all negative samples.

You can compute it using numpy:

correct_negative = np.sum((y_hat == 0) & (y_test == 0))
total_negative = np.sum(y_test == 0)

TNR = correct_negative / total_negative

or you can do it "manually":

correct_negative = 0
total_negative = 0

for i in range(len(y_hat)):
    if y_test[i] == 0:
        total_negative += 1
        if y_hat[i] == 0:
            correct_negative += 1

TNR = correct_negative / total_negative
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.