Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using numpy.argpartition ignoring NaNs

I have a large array with approximately 49million items (7000*7000) and in there I need to find the largest N items and their indexes ignoring all the NaNs. I can't remove these NaNs before hand because I need the index values of the largest N items from first array to extract data from another which have NaNs in different indexes compared to the first array. I tried

np.argpartition(first_array, -N)[-N:]

this works very good for an array without NaNs, but if there is NaNs the nan's are coming as the largest item because it is considered as infinity in python.

x = np.array([np.nan, 2, -1, 2, -4, -8, -9, 6, -3]).reshape(3, 3)
y = np.argpartition(x.ravel() , -3)[-3:]
z = x.ravel()[y]
# this is the result I am getting  === [2, 6, nan]
# but I need this ==== [2, 2, 6]
like image 652
Gokul Avatar asked Aug 08 '26 19:08

Gokul


1 Answers

Use count of NaNs to offset and thus compute indices and extract values -

In [200]: N = 3

In [201]: c = np.isnan(x).sum()

In [204]: idx = np.argpartition(x.ravel() , -N-c)[-N-c:-c]

In [207]: val = x.flat[idx]

In [208]: idx,val
Out[208]: (array([1, 3, 7]), array([2., 2., 6.]))
like image 54
Divakar Avatar answered Aug 10 '26 08:08

Divakar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!