I have two 2d arrays of equal shapes: given_array and reference_array. I have to write a file for each unique value of the reference_array computing mean values where the unique value are in the given array.
import numpy as np
given_array = np.array([[2,4,5,8,9,11,15],[1,2,3,4,5,6,7]])
reference_array = np.array([[2,2,2,8,8,8,15],[2,2,2,4,8,8,9]])
unique_value = np.unique(reference_array)
file_out = open('file_out', 'w')
for unique in unique_value:
index = reference_array == unique
mean = np.mean(given_array[index])
file_out.write(str(unique) + ',' + str(mean) + '\n')
file_out.close()
The above code works, but in my real problem two arrays are extremely large as read from raster image, and it is taking few days to complete the processing.
Would be grateful if someone could provide fastest way of producing the same result.