Skip to content
Advertisement

Fastest way to find a 2d array inside another array that holds multiple 2d arrays

Hi I’m trying to perform a search operation in an array that contains multiple 2d arrays comparing it’s itens to a specific array. I managed to do it using a for loop iterating trough the itens inside the big array but I have to perform this search 10^6 times and the length of this for loop can grow up to 2^100 so it is becoming very time consuming. I’m wondering if there is a way to make this search faster using a np.where or np.isin() function.

this is an example of the slower working method

import numpy as np

frequencies = {}
b = np.array ([[0, 0, 0], [0, 0, 0], [1, 1, 1]]) #template

a = np.array([[[1, 1, 1], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [1, 1, 1], [0, 0, 0]],[[0, 0, 0], [0, 0, 0], [1, 1, 1]],[[0, 0, 1], [0, 0, 1], [0, 0, 1]],[[0, 1, 0], [0, 1, 0], [0, 1, 0]],[[1, 0, 0], [1, 0, 0], [1, 0, 0]]])

#I need to know if b is inside a and the index where it its located
for I in range (a): 
     if np.all(b==a[I]):
          frequencies [I] = frequencies [I] + 1

and I would like to make something like this. I need to store the indexes where b is found inside a in the index c of the dictionary frequencies

import numpy as np

frequencies = {}
b = np.array ([[0, 0, 0], [0, 0, 0], [1, 1, 1]]) #template

a = np.array([[[1, 1, 1], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [1, 1, 1], [0, 0, 0]],[[0, 0, 0], [0, 0, 0], [1, 1, 1]],[[0, 0, 1], [0, 0, 1], [0, 0, 1]],[[0, 1, 0], [0, 1, 0], [0, 1, 0]],[[1, 0, 0], [1, 0, 0], [1, 0, 0]]])

c = np.where(np.all(b==a))

frequencies [c] = frequencies [c] + 1

Advertisement

Answer

You can use NumPy.all with a two-axis then use NumPy.argwhere for finding the index like below:

b = np.array ([[0, 0, 0], [0, 0, 0], [1, 1, 1]])

a = np.array([[[1, 1, 1], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [1, 1, 1], [0, 0, 0]],[[0, 0, 0], [0, 0, 0], [1, 1, 1]],[[0, 0, 1], [0, 0, 1], [0, 0, 1]],[[0, 1, 0], [0, 1, 0], [0, 1, 0]],[[1, 0, 0], [1, 0, 0], [1, 0, 0]]])

np.all(b == a, axis=(-1))
# array([[False,  True, False],
#        [ True, False, False],
#        [ True,  True,  True],
#        [False, False, False],
#        [False, False, False],
#        [False, False, False]])

np.all(b == a, axis=(-1,1))
# array([False, False,  True, False, False, False])

indexs = np.argwhere(np.all(b == a, axis=(-1, 1)))

Output:

>>> indexs
array([[2]])
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement