Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there an alternative to numpy.where() that doesn't return the list in ascending order?

The numpy.where() function will always return a list in ascending order. I am using it to get indices from values in a list that are found in another list like this:

lst = [1, 2, 8, 7, 3, 4, 6, 5]

values = [4, 8]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

indices = np.where(np.isin(lst, values))

The output is [2,5] instead of the expected [5,2].

The problem, is that I need it to return the indices in the order that the values are found. So in my example, 4 is the first value, so the index that corresponds to it(5) should be first in the output, but it isn’t. Is there an alternative to the numpy.where() function that keeps the order instead of sorting it? I could simply use some for loops and basic indexing to figure this out, but speed is super important because this is part of my custom 2d Max Pool function for a neural net.

>Solution :

You can reorder the indices based on the output values:

lst = [1, 2, 8, 7, 3, 4, 6, 5]
values = [4, 8]

a = np.array(lst)
m = np.isin(a, values)
order = np.argsort(a[m], kind='stable')

indices = np.where(np.isin(lst, values))[0][order]

Output: [5, 2]

Output with lst = [1, 2, 8, 7, 3, 4, 6, 5, 4, 8]: array([5, 8, 2, 9])

NB. My previous statement was incorrect. I had misread unique instead of where.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading