Need method to replicate "argpartition" for masked arrays in numpy

Viewed 62

As the title indicates I want to use numpy.argpartition on a masked array. I can, but .argpartition does not honor the mask (it emits a warning message notifying the user as well). This is not useful since the masked data corrupt the results of .argpartition.
Any suggestions for replacement methods? I need to know the indices of the k smallest values in a large 1D array.

Current ideas:

a) write my own implementation of .argpartition for masked arrays

b) my current data set has the feature that the masked values are all negative (which is why they corrupt the search for smallest values). which leads to two solutions

  1. I could sort through them and assign a very large number to the masked values...If I do this, I feel I could just drop used of masked arrays.
  2. I could count the number of masked elements = p and then argpartition on p+k elements. Removing the p elements from the list.

Neither a) or b) seem very pythonic or elegant.

0 Answers
Related