I have an array like:
import numpy as np
data=np.array([[0,0,0,1,1,1,0,0,0,0,1,1,1,1,1],
[0,0,0,1,0,0,0,0,1,0,0,0,0,0,0],
[0,0,0,0,0,0,1,0,0,0,0,0,0,0,0]])
Requirement:
Needs to count the number of ones in an array, I can count by using this function.
print(np.count_nonzero(data==1))
the output I got:
11
But, one special requirement is needed, like if consecutive ones are more than 3 times then only count the ones, in that case, the excepted count of number ones more than 3 consecutive are 5
expected output:
5
numpy:length = np.diff(np.flatnonzero(np.diff(data, append=0, prepend=0, axis=1)))[::2]; length[length > 3].sum()