2

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
5
  • Why is the expected output 5 (I see only 4)? Why are you working with a 2D array? What's the full list of "requirements"? Commented Sep 4, 2022 at 20:56
  • I just need to count the element 1's if there are more than 3 times(for ex:) repeated consecutively. otherwise, ignore the count Commented Sep 4, 2022 at 20:59
  • in another case: if element value 1 is not repeated more than 3 times we can consider it as 0 Commented Sep 4, 2022 at 21:00
  • 1
    In pure numpy: length = np.diff(np.flatnonzero(np.diff(data, append=0, prepend=0, axis=1)))[::2]; length[length > 3].sum() Commented Sep 4, 2022 at 21:27
  • @Michael smart one (however I think this might require some explanations to the non aficionados :p) Commented Sep 4, 2022 at 21:37

2 Answers 2

1

You can erode/dilate your data to remove the stretches of less than N consecutive 1s.

from scipy.ndimage import binary_erosion, binary_dilation

N = 3
k = np.ones((1, N+1))

binary_dilation(binary_erosion(data, k), k).sum()

Output: 5

Output on data=np.array([[0,0,0,1,1,1,0,0,0,0,1,1,1,1,1], [0,0,0,1,1,1,1,1,1,1,1,0,0,0,0], [0,0,0,0,0,0,1,0,0,0,0,0,0,0,0]]): 13

Sign up to request clarification or add additional context in comments.

Comments

1

A simple method would be to use a "running average" of window-size 3 and then compare to 1.
I still don't understand why the OP is using a 2d array, but instead of changing the example I'll just flatten it out:

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]])    

def running_avg(x, N):
    return np.convolve(x, np.ones(N)/N, mode='valid')

print(sum(running_avg(data.flatten(), 3) == 1))
# 4, which is actually the correct answer for the given example data as far as I can tell

3 Comments

for this example: data=np.array([[0,0,0,1,1,1,0,0,0,0,1,1,1,1,1], [0,0,0,1,1,1,1,1,1,1,1,0,0,0,0], [0,0,0,0,0,0,1,0,0,0,0,0,0,0,0]]) def running_avg(x, N): return np.convolve(x, np.ones(N)/N, mode='valid') print(sum(running_avg(data.flatten(), 3) == 1)) output it gives:10 instead of count :12
Did you mean 13?
Looking at your example, I'd want an answer of 10. Or are we counting rows and columns separately? the op was not very clear despite being asked about the 2d nature explicitly...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.