1

What happends in this numpy function: https://numpy.org/doc/stable/reference/generated/numpy.unique.html

a = np.array([1, 2, 5, 3, 2])
u, indices = np.unique(a, return_inverse=True)

The results are:

u

array([1, 2, 3, 5)]

indices

array([0,1,3,2,1), dtype=int64)

u[indices]

array([1, 2, 5, 3, 1])

u are the single values inside this array. What is indices? why is it a 3 and not a 5? and what is going on in u[indices]?

martin
  • 329
  • 3
  • 12

1 Answers1

2

Firstly the function returns the variables u and indices:

  • u contains the unique elements sorted. In other words no element is repeated (the number 2 does not appear twice) and the elements will be listed from the smallest to the largest value
  • indices is the same size as a and basically it contains the index in u you should used to recover a. So when they give you [0,1,3,2,1], in this case the number 3 refers to the 3rd index of u = [1, 2, 3, 5] which in this case is 5

You can see how u and indices can be used to recover a by running the following code:

for element in indices:
      print(u[element])

This also explains your last question: u[indices] it basically gives you back a. Note that in your question u[indices] should return array([1, 2, 5, 3, 2]). I assume you made a typo

Burger
  • 173
  • 6