I am looking for an efficient way to do the following:
If my input is:
np.array([9,0,1,0,3,0])
I want my output to be:
np.array([0,3,2,3,1,3]) # 9 is the highest, so it gets rank 0
# 3 is the second highest, so it gets rank 1
# 1 is third highest, so it gets rank 2
# 0's are forth highest so they get rank 3
I am trying to apply the following to 2D matrix:
Input:
a = np.array([[9,0,1,0,3,0],
[0,1,2,3,4,5],
[0.01,0.3,2,100,1,1],
[0,0,0,0,1,1],
[4,4,4,4,4,4]])
Output:
>>> get_order_array(a)
array([[0, 3, 2, 3, 1, 3],
[5, 4, 3, 2, 1, 0],
[4, 3, 1, 0, 2, 2],
[1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0]])
I do can achieve the above with the following solution; however, I feel that its is very inefficient, so I was hoping that someone can suggest a better way to achieve my goal.
def get_order(x):
unique_x = np.unique(x)
step_1 = np.argsort(unique_x)[::-1]
temp_dict = dict(zip(unique_x, step_1))
return np.vectorize(temp_dict.get)(x)
def get_order_array(x):
new_array = np.empty(x.shape, dtype=np.int)
for i in xrange(x.shape[0]):
new_array[i] = get_order(x[i])
return new_array