Assuming there is a 2D-array and it is divided into several sub-regions as regions. there is another array filled with values. I would like to aggregate the values by sub-regions. The following code is my solution.
But, when the number of sub-regions is very large, the iteration costs much time. I would like to ask if there is any way to accelerate the programm? I suppose maybe numpy could do this, but I don't know how to do.
import numpy as np
regions = np.array([[0,0,0,1],
[0,0,1,1],
[1,1,1,2],
[2,2,2,2]], dtype=np.int32)
value_array = np.array([[9,5,8,4],
[6,4,8,5],
[4,5,9,7],
[4,7,3,0]], dtype=np.float32)
aggre_array = np.zeros_like(value_array)
for r in range(regions.max()+1):
region = regions==r
aggre_array[region] = value_array[region].mean()
print(aggre_array)
'''output
[[6.4 6.4 6.4 5.8333335]
[6.4 6.4 5.8333335 5.8333335]
[5.8333335 5.8333335 5.8333335 4.2 ]
[4.2 4.2 4.2 4.2 ]]
'''