I have the following code for determining TP, TN, FP and FN values for binary classification given two sparse vectors as input (using the `sparse`

library):

```
def confused(sys1, ann1):
# True Positive (TP): we predict a label of 1 (positive), and the true label is 1.
TP = np.sum(np.logical_and(ann1 == 1, sys1 == 1))
# True Negative (TN): we predict a label of 0 (negative), and the true label is 0.
TN = np.sum(np.logical_and(ann1 == 0, sys1 == 0))
# False Positive (FP): we predict a label of 1 (positive), but the true label is 0.
FP = np.sum(np.logical_and(ann1 == 0, sys1 == 1))
# False Negative (FN): we predict a label of 0 (negative), but the true label is 1.
FN = np.sum(np.logical_and(ann1 == 1, sys1 == 0))
return TP, TN, FP, FN
```

I’m trying to find a way to optimize this for speed. This is based on how-to-compute-truefalse-positives-and-truefalse-negatives-in-python-for-binary-classification-problems where my addition was to add the sparse arrays to optimize for memory usage, since the input vectors for the current problem I am trying to solve have over 7.9 M elements, and the positive cases (i.e., 1), are few and far between wrt the negative cases (i.e., 0).

I’ve done profiling of my code and about half the time is spent in this method.