Varlor
Varlor

Reputation: 1471

Python : Vectorized conditional sum along a axis

Is there a way to sum along an axis with some conditions? So i have a array like this and can sum it along the 0 axis.

#array creation:
tpfnfpArray = np.zeros((1000,3))
for i in range(1000):
    tpfnfpArray[i,:] = (i,i,i)

#first result
tp,fn, fp = np.sum(tpfnfpArray,axis=0) 

#preparing second result
tp2,fp2,fn2,tn2 = (0,0,0,0)

So far so good. Now i want to have another result (tp2,fp2,fn2,tn2) with such conditions:

for i in range(1000):
    if tpfnfpArray[i][0] > 0 or tpfnfpArray[i][1]>0:
        if tpfnfpArray[i][2] > 0: # 0,1,1 or 1,0,1
            tp2+=1
        else: # 0,1,0 or 1,0,0
            fp2+=1
    else:
        if tpfnfpArray[i][2] > 0: # 0,0,1
            fn2+=1
        else: # 0,0,0
            tn2+=1

Is it possible to to such thing without looping throug each row of the first array?

Upvotes: 1

Views: 93

Answers (2)

Dev Khadka
Dev Khadka

Reputation: 5461

You can do it using vectorized logical operations like below

import numpy as np
#array creation:
tpfnfpArray = np.zeros((1000,3))
for i in range(1000):
    tpfnfpArray[i,:] = (i,i,i)

#first result
tp,fp,fn = np.sum(tpfnfpArray,axis=0) 

#preparing second result
tp2,fp2,fn2,tn2 = (0,0,0,0)

gt_zero = tpfnfpArray>0

# if tpfnfpArray[i][0] > 0 or tpfnfpArray[i][1]>0:
cond1 = gt_zero[0] | gt_zero[1]

# --if tpfnfpArray[i][2] > 0:
tp2 = np.sum(cond1 & gt_zero[2])
# --else:
fp2 = np.sum(cond1 & ~gt_zero[2])

# else
# -- if tpfnfpArray[i][2] > 0:
fn2 = np.sum(~cond1 & gt_zero[2])
# -- else:
tn2 = np.sum(~cond1 & ~gt_zero[2])

tp2, fp2, fn2, tn2

Upvotes: 0

javidcf
javidcf

Reputation: 59731

You can make that computation in with vectorized boolean operations:

import numpy as np

# Random binary array
np.random.seed(0)
tpfnfpArray = np.random.randint(0, 2, (1000, 3))

# Loop computation for comparison
tp2, fp2, fn2, tn2 = (0, 0, 0, 0)
for i in range(1000):
    if tpfnfpArray[i][0] > 0 or tpfnfpArray[i][1]>0:
        if tpfnfpArray[i][2] > 0:
            tp2 += 1
        else:
            fp2 += 1
    else:
        if tpfnfpArray[i][2] > 0:
            fn2 += 1
        else:
            tn2 += 1
print(tp2, fp2, fn2, tn2)
# 401 377 115 107

# Vectorized computation
tp_m = tpfnfpArray[:, 0] > 0
fn_m = tpfnfpArray[:, 1] > 0
fp_m = tpfnfpArray[:, 2] > 0
tpfn_m = tp_m | fn_m
tp3 = np.count_nonzero(tpfn_m & fp_m)
fp3 = np.count_nonzero(tpfn_m & ~fp_m)
fn3 = np.count_nonzero(~tpfn_m & fp_m)
tn3 = np.count_nonzero(~tpfn_m & ~fp_m)
print(tp3, fp3, fn3, tn3)
# 401 377 115 107

Upvotes: 1

Related Questions