1

I would like to access an numpy array data via an index idx, but still preserving the order in data. Below is an example where the array is accessed with an order different from the one in the original array.

In [125]: data = np.array([2, 2.2, 2.5])
In [126]: idx=np.array([1,0])
In [127]: data[idx]
Out[127]: array([2.2, 2. ])

I hope to get [2,2.2] instead. Is there a highly efficient way to do so? In my problem setting, I have the data with more than a million floating-point numbers, and idx with a 0.1 million integers.

Important info: The array data can be preprocessed if needed. The data come from an image processing work. For example, if we need to sort data beforehand, the time consumed on sorting would not be considered when measuring the performance. On the other hands, idx is something I would rather not process too much at runtime as time spent on it has to be counted. E.g. soriting idx with an O(n log n) algorithm can be too expensive.

asked Sep 26, 2020 at 10:19

2 Answers 2

2

Creat a boolean 'mask'

 mask = np.zeros(data.shape, bool)
 mask[idx] = True
 res = data[mask]
answered Sep 26, 2020 at 14:36
Sign up to request clarification or add additional context in comments.

Comments

-1

Something like this? Or I didn't get the point?

data=np.array([2,2.2,2.5])
idx=np.array([1,0])
data[np.sort(idx)]
answered Sep 26, 2020 at 10:28

2 Comments

Where the output of the last command is: >>> array([2. , 2.2])
Thanks, but "soriting idx with an O(n log n) algorithm can be too expensive." On the other hand, it is okay to sort data if needed. So my comments above.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.