0

I have an index array, and an associated value array.

delta = np.array([0,3,4,1,1,4,4,5,7,10], dtype = int)
theta = np.random.normal(size = (12, 5))

I want to "clean" the index array such indices with no presence are dropped, and higher indices are moved down to take their place. In this case, the result will be:

delta == np.array([0,2,3,1,1,3,3,4,5,6], dtype = int)
theta == theta[np.array([0,1,3,4,5,7,10, 2,6,8,9,11], dtype = int)]

and the associated entries are moved up in the theta array such that their indices match the new indices in the delta vector. How do I go about this?

1
  • Can you show us the expected output ? Commented Sep 1, 2021 at 6:03

1 Answer 1

1

Let's ask unique for all of the optional values.

In [799]: np.unique(x,return_index=True,return_inverse=True, return_counts=True)
Out[799]: 
(array([ 0,  1,  3,  4,  5,  7, 10]),
 array([0, 3, 1, 2, 7, 8, 9]),
 array([0, 2, 3, 1, 1, 3, 3, 4, 5, 6]),
 array([1, 2, 1, 3, 1, 1, 1]))

Looks like the 'inverse' is what you want.

Review np.unique docs for more details.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.