Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get smallest N values from numpy array ignoring inf and nan

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.

like image 492
jeffery_the_wind Avatar asked Apr 24 '13 13:04

jeffery_the_wind


2 Answers

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]]
like image 162
YXD Avatar answered Nov 04 '22 03:11

YXD


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:]
like image 43
askewchan Avatar answered Nov 04 '22 04:11

askewchan