Home > Enterprise >  revalue numpy array by order of unique appearance count
revalue numpy array by order of unique appearance count

Time:04-20

I have 2D numpy array, I need to revalue the array by unique appearance count.

import numpy as np
arr = np.array([[6, 1, 8, 5, 3],
      [2, 7, 3, 6, 9],
      [5, 3, 2, 4, 4],
      [6, 4, 6, 4, 4]])
values, counts = np.unique(arr, return_counts=True)
print('values',values)
print('counts',counts)
values [1 2 3 4 5 6 7 8 9]
counts [1 2 3 5 2 4 1 1 1]

[1, 6],
[2, 4],
[3, 3],
[4, 1],
[5, 5],
[6, 2],
[7, 7],
[8, 8],
[9, 9]

if more than one unique have same appearance than the smallest unique come first, in this example wherever there is one it should be replace by 6, 2 should be replace by 4, 3 remain 3, 4 should be replace by 1, 5 stay 5, 6 become 2, and so on. I have arrays of 10000*10000 and about 8000 unique so it cannot be manually and should be fast. if it is to problematic the order of same value does not have to be from smallest to biggest must be consistent with the correct main order, meaning that 2 can transfer to 5, and 5 can transfer to 4

CodePudding user response:

I think this gives the desired result. The stable sort makes sure the smallest value comes first in case multiple uniques have the same count.

values, inverse, counts = np.unique(arr, return_inverse=True, return_counts=True)
order = np.argsort(-counts, kind='stable')
new_values = np.empty_like(values)
new_values[order] = np.arange(1, 1 len(values))
result = new_values[inverse].reshape(arr.shape)
  • Related