Suppose I have an array of integers x and I want to do the following:
- get an array
unique_xof the unique values ofx - build an array
yin whichy[i]is the index inunique_xof the valuex[i].
I managed to do this as follows:
import numpy as np
unique_x = np.unique(x)
y = np.zeros_like(x)
for k, value in unique_x:
indices, = np.nonzero(x == value)
y[indices] = k
My question is: is there a way to do this using only numpy builtin functions and some slicing? I have a feeling that this loop won't be as fast as it could be if this was done with numpy builtins.