0

I have a list of numpy arrays, which consists of all possible configurations of 0 and 1 in a 10-pixel arrays. I'm trying to determine the number of arrays that have specific group of 1s for more than two 1s. For example, the array is [1,0,0,1,1,1,1,1,0,1]. I want to determine this array has five 1s as a block. Another example, the array is [1,1,1,0,1,1,1,1,1,1]. I want to find the block as six 1s instead of three 1s block. I couldn't find a way to do this.

Here is the code I generate the list of all possible arrays:

import numpy as np
from itertools import product

all_arrays = np.array(list(product([0,1], repeat=10)))

2 Answers 2

1

convert the array to a string and check for membership along the lines of:

str_array = ''.join(my_array)
return 5*'1' in str_array
Sign up to request clarification or add additional context in comments.

Comments

1

Try below approach. Its not very efficient, but it should work.

from functools import reduce
import numpy as np
from itertools import product

n=int(input("How many 1s you want?"))
all_arrays = list(filter(lambda x : n*"1" in x and (n+1)*"1" not in x,map(lambda e : reduce(lambda x,y : str(x)+str(y), e), product([0,1], repeat=10))))
print(len(all_arrays))

3 Comments

I was trying to determine this number of 1-blocks for each of the array, instead of generating with a specific number of 1s-block. The solution above worked for this. Thank you for your code though, I can use it for something else!
So, just to clarify. You want the count but you are trying to avoid generating the arrays, right?
Yes, that is right. I'm trying to count the number of 1s in a group of 1s for each array in the list

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.