I have a numpy array as follows,
arr = np.array([0.166667, 0., 0., 0.333333, 0., 0.166667, 0.166667, np.nan]
I wish to rank above array in descending order such that the highest value gets 1. and np.nan gets the last value but without incrementing the rank during value repetitions!
Expectation:
ranks = [2, 3, 3, 1, 3, 2, 2, 4]
i.e.
>>>>
1 0.333333
2 0.166667
2 0.166667
2 0.166667
3 0.0
3 0.0
3 0.0
4 -inf
What I have accomplished so far is below,
I used np.argsort twice and filled the np.nan value with the lowest float possible but the ranks increment even with the same value!
# The Logic
arr = np.nan_to_num(arr, nan=float('-inf'))
ranks = list(np.argsort(np.argsort(arr)[::-1]) + 1)
# Pretty Print
sorted_ = sorted([(r, a) for a, r, in zip(arr, ranks)], key=lambda v: v[0])
for r, a in sorted_:
print(r, a)
>>>>
1 0.333333
2 0.166667
3 0.166667
4 0.166667
5 0.0
6 0.0
7 0.0
8 -inf
Any idea on how to manage the ranks without increments?
https://repl.it/@MilindDalvi/MidnightblueUnselfishCategories