Reputation: 8396
If I have the following array:
a = [4, -5, 10, 4, 4, 4, 0, 4, 4]
To get the three minimum values index I do:
a1 = np.array(a)
print a1.argsort()[:3]
This outputs the following, which is ok:
[1 6 0]
The thing is that this includes that -5
(index 1
).
How can I do this exact thing but ignoring the -5
of the array?
Upvotes: 0
Views: 2975
Reputation: 23322
If you're looking to exclude the minimum element, you can simply skip the element while slicing:
>>> a = [4, 3, 10, -5, 4, 4, 4, 0, 4, 4]
>>> a1 = np.array(a)
>>> minimum_indexes= a1.argsort()[1:4]
>>> print a1[minimum_indexes]
[0 3 4]
Upvotes: 2
Reputation:
You can filter out your specific number after argsort
:
>>> a = [4, -5, 10, 4, 4, 4, 0, 4, 4]
>>> a1 = np.array(a)
>>> indices = a1.argsort()
>>> indices = indices[a1[indices] != -5]
>>> print(indices[:3])
[6 0 3]
It's easy to change your condition. For instance, if you want to filter out all negative numbers, then use the following line instead:
>>> indices = indices[a1[indices] >= 0]
Upvotes: 2