Home > Blockchain >  Numpy - how to count values in column based on binary values in other columns?
Numpy - how to count values in column based on binary values in other columns?

Time:12-04

I have a 2d numpy array of only binary values, and I'm trying to count the number of 1s I have in a column based on the binary combinations of other columns with the same row.

so let's say I have:

[[0,0,0,0],
[1,0,1,0],
[0,0,0,1],
[1,0,0,0],
[0,1,0,0],
[1,0,1,0]]

and i want to be able to count the number of 1s in the first column where the other columns within the same row have the values [0, 1, 0]. I need my code to return 2. As that is the case in rows 1 and 5.

I've tried to do this with a mask, but it's not returning the result I'm looking for:

test = numpy.asarray(
    [[0,0,0,0],
    [1,0,1,0],
    [0,0,0,1],
    [1,0,0,0],
    [0,1,0,0],
    [1,0,1,0]])
mask = test[:,[1, 2, 3]] == [0,1,0]
mask

which returns

array([[ True, False,  True],
       [ True,  True,  True],
       [ True, False, False],
       [ True, False,  True],
       [False, False,  True],
       [ True,  True,  True]])

assuming masking is the correct approach, any suggestions on what to do next after I get this mask?

CodePudding user response:

You want all three elements in each row to match [0,1,0], so your mask should be:

mask = (test[:,1:] == [0,1,0]).all(axis=1)
out = test[mask,0].sum()

Output:

2

If you want to count zeros in the first column that corresponds to the same configuration, then you add a filter for 0 in the first column to the mask above:

out2 = test[mask&(test[:,0]==0),0].shape[0]
  • Related