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
JavaScript
x
12
12
1
import numpy as np
2
3
frequencies = {}
4
b = np.array ([[0, 0, 0], [0, 0, 0], [1, 1, 1]]) #template
5
6
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]]])
7
8
#I need to know if b is inside a and the index where it its located
9
for I in range (a):
10
if np.all(b==a[I]):
11
frequencies [I] = frequencies [I] + 1
12
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
JavaScript
1
12
12
1
import numpy as np
2
3
frequencies = {}
4
b = np.array ([[0, 0, 0], [0, 0, 0], [1, 1, 1]]) #template
5
6
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]]])
7
8
c = np.where(np.all(b==a))
9
10
frequencies [c] = frequencies [c] + 1
11
12
Advertisement
Answer
You can use NumPy.all
with a two-axis then use NumPy.argwhere
for finding the index like below:
JavaScript
1
17
17
1
b = np.array ([[0, 0, 0], [0, 0, 0], [1, 1, 1]])
2
3
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]]])
4
5
np.all(b == a, axis=(-1))
6
# array([[False, True, False],
7
# [ True, False, False],
8
# [ True, True, True],
9
# [False, False, False],
10
# [False, False, False],
11
# [False, False, False]])
12
13
np.all(b == a, axis=(-1,1))
14
# array([False, False, True, False, False, False])
15
16
indexs = np.argwhere(np.all(b == a, axis=(-1, 1)))
17
Output:
JavaScript
1
3
1
>>> indexs
2
array([[2]])
3