Imagine you have a 2D-array (as a NumPy int array) like:
[[2,2,3,3],
[2,3,3,3],
[3,3,4,4]]
Now you want to get an array of the same shape, but instead of the original values, you want to replace the number by its occurrences. Which means, the number 2 changes to 3, since it occurred 3 times, the 3s become 7s and the 4s become 2s.
So the output would be:
[[3,3,7,7],
[3,7,7,7],
[7,7,2,2]]
My solution was first to create a dictionary, which saves all original values as keys and as values the number of occurrences. But for arrays of shape 2000x2000, this seemed to be quite slow.
How could I achieve this more efficiently?
Thanks!
I believe you should be able to stay in NumPy here by using return_inverse within np.unique():
If True, also return the indices of the unique array (for the specified axis, if provided) that can be used to reconstruct
ar.
>>> import numpy as np
>>> a = np.array([[2,2,3,3],
... [2,3,3,3],
... [3,3,4,4]])
>>> _, inv, cts = np.unique(a, return_inverse=True, return_counts=True)
>>> cts[inv].reshape(a.shape)
array([[3, 3, 7, 7],
[3, 7, 7, 7],
[7, 7, 2, 2]])
This will also work for the case where the flattened array is not sorted, such as b = np.array([[1, 2, 4], [4, 4, 1]]).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With