'How to calculate TP/TN/FP/FN from binary value pairs in Python?

I want to write a Python function that on a binary value input pair (truth, prediction) gives back True Positive/True Negative/False Positive/False Negative values according their input. So far I have reached the required output with this:

def func(truth, prediction):
    
    if prediction == 1:
        
        if truth == 1:
            return "TP"
        else:
            return "FP"

    elif truth == 1:
        return "FN"

    else:
        return "TN"

However, this seems a but clunky solution, is there a shorter, more elegant way?

(The input pair is supposed to be a binary integer 0/1)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source