I would like to apply a sort operation, row per row, only keeping values above a given threshold.
For this, I see I can use a masked array to apply the threshold.
However, argsort
keeps considering masked values (below the threshold) and replace them with a fill_value
.
However, I simply don't want any result if the value has been replaced with a NaN.
a = np.array([[0.522235,0.128270,0.708973],
[0.994557,0.844426,0.366608],
[0.986669,0.143659,0.395891],
[0.291339,0.421843,0.278869],
[0.250303,0.861475,0.904534],
[0.973436,0.360466,0.751913]])
threshold = 0.5
m_a = np.ma.masked_less_equal(a, threshold)
argsorted = m_a.argsort(-1)
This gives me:
array([[0, 2, 1],
[1, 0, 2],
[0, 1, 2],
[0, 1, 2],
[1, 2, 0],
[2, 0, 1]])
But I would like to get:
array([[0, NaN, 1],
[1, 0, NaN],
[0, NaN, NaN],
[NaN, NaN, NaN],
[NaN, 0, 1],
[ 1, NaN, 0]])
Any idea to get to this result?
Thanks for your help! Bests,