I tried to word the question as simply as possible but I'm new to Python and very bad at logic so I'm having a bit of trouble. Basically I want to know if there's a cleaner way to count confusion matrices of two 1D arrays of booleans.
Here's an example:
arr1 = [0, 0, 1, 0, 1, 1]
arr2 = [1, 0, 0, 0, 1, 0]
tp = fp = tn = fn = 0
for i,p in enumerate(arr1):
a = arr2[i]
if p & a: tp += 1
if p & ~a: fp += 1
if ~p & ~a: tn += 1
if ~p & a: fn += 1
# This was pointed out to be incorrect (see Mozway's answer below)
I tried this but it just adds more lines and looks arguably worse:
if p == o:
if p: tp += 1
else: tn += 1
else:
if p: fp += 1
else: fn += 1
I then tried adding nested conditional expressions (I believe these are Python's version of ternary operators?) but this disgusting monstrosity doesn't even compile:
(tp += 1 if a else fp += 1) if p else (tn += 1 if ~a else fn += 1)
Any help would be appreciated!
EDIT: Sorry I should have clarified, the result I want is this:
Adding print(tp, fp, tn, fn) would give 1, 2, 2, 1. Simply counting the combinations of each of the booleans in the arrays.