I have a performance problem when "filtering" the array according to the closest float found in another array.
These are the MWEproblems:
import numpy as np
def random_data(N):
return np.random.uniform(0., 10., N).tolist()
N1 = 1500
list1 = [random_data(N1), random_data(N1), random_data(N1)]
list2 = random_data(1000)
min_1, max_1 = min(list1[2]), max(list1[2])
list4 = [[], [], []]
for elem2 in list2:
if min_1 <= elem2 <= max_1:
indx, elem1 = min(enumerate(list1[2]), key=lambda x:abs(x[1]-elem2))
list4[0].append(list1[0][indx])
list4[1].append(list1[1][indx])
list4[2].append(elem1)
(note that it list2contains fewer elements than list1[2], which is a sub-list that I compare with it)
This block works as expected, but it is terribly inefficient. I am sure that the answer lies in the correct application of broadcasting arrays and numpy, but I still have not been able to use it enough to apply it to my problem.
, ( : , )
, , ali_m .
( , ), , .
ali_m ?
2
, 2357112 Eelco Hoogendoorn, . , , list1[2] list2 . ( MWE ):
def random_data(N, xi, xf):
return np.random.uniform(xi, xf, N).tolist()
N1 = 1500
list1 = [random_data(N1, 13., 20.), random_data(N1, -1., 4.), random_data(N1, 2., 7.)]
list2 = random_data(1000, 0., 10.)
list1[2] list2, , , i, list2[i] > max(list1[2]) list2[i] < min(list1[2]).
, ? , , .