I need a good, quick method for finding the 10 smallest real values from a numpy array that could have arbitrarily many nan
and/or inf
values.
I need to identify the indices of these smallest real values, not the values themselves.
I have found the argmin
and nanargmin
functions from numpy. They aren't really getting the job done because I also want to specify more than 1 value, like I want the smallest 100 values, for example. Also they both return -inf
values as being the smallest value when it is present in the array.
heapq.nsmallest
kind of works, but it also returns nan
and -inf
values as smallest values. Also it doesn't give me the indices that I am looking for.
Any help here would be greatly appreciated.
The only values that should be throwing this out are the negative infinite ones. So try:
import numpy as np
a = np.random.rand(20)
a[4] = -np.inf
k = 10
a[np.isneginf(a)] = inf
result = a[np.argsort(a)[:k]]
It seems to me like you could just take the first n
finite values from your sorted array, instead of trying to modify the original array, which could be dangerous.
n = 10
b = np.sort(a)
smalls = b[np.isfinite(b)][n:]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With