Given 2 numpy arrays
a1
anda2
(composed only of 0 and 1's), find index locations of all 0's ina1
anda2
.Find matching index positions if any between array
a1
anda2
.Calculate a metric.
a1
array shape is 2161
a2
array shape is 2161
e.g.
a1 = [0,1,1,0,1,0,1]
a2 = [1,1,0,0,1,1,0]
The indices of all 0's in a1
are 0, 3, and 5.
The indices of all 0's in a2
are 2, 3, and 6.
The only common index between a1
and a2
is thus 3.
function_1
performs step1 and step2 and step3
function1_iterations
repeats function_1
after randomly shuffling a1
and repeating metric calculation 1000 times. This is for the purpose of finding if metric is statistically significant.
I perform below code on 100 million array pairs, multiprocessed on 256 cores. Best runtime for 100 million array pairs is about 40 mins. Is there any way I can make it significantly efficient? I need to be running this on billions of array pairs.
My code below is the fastest that I could come up with some help from people from codereview earlier:
def function_1(self, a1, a2):
event_index1, = np.where(a1 == 0)
event_index2, = np.where(a2 == 0)
n1, = event_index1.shape
n2, = event_index2.shape
if n1 == 0 or n2 == 0:
return 0, 0
n_matches, = np.intersect1d(event_index1, event_index2, assume_unique=True,).shape
c_ij = c_ji = n_matches/2
metric_1= (c_ij + c_ji) / math.sqrt(n1 * n2)
return metric_1
def function1_iterations(self,a1,a2,repeat = 1000,original_metric1):
list_metric1 = []
a1_copy = copy.deepcopy(a1)
for i in range(0, repeat):
np.random.shuffle(a1_copy) # shuffle bits in array and recalculate 1000 times
metric_1 = self.function_1(a1= a1_copy, a2 = a2)
list_metric1.append(metric_1)
list_metric1= np.array(list_metric1)
significance_val = len(np.where(list_metric1>= [original_metric1])[0])/repeat
return significance_val
run
into an answer, and this comment thread needs to continue on that answer and not on the question. \$\endgroup\$bool
. Otherwise, bitwise operations will produce different results. \$\endgroup\$