This question came up after I read this post .
There's a function numpy.argsort() that returns the indexes of the original array that would yield a sorted array. By applying this function twice you would get the rank of the original away.
Someone commented that "The first argsort returns a permutation (which if applied to the data would sort it). When argsort is applied to (this or any) permutation, it returns the inverse permutation (that if the 2 permutations are applied to each other in either order the result is the Identity). The second permutation if applied to a sorted data array would produce the unsorted data array, i.e. it is the rank."
But is there any mathematical way to explain it? I think there should be linear algebra formula to explain it.
OK I tried a toy example M=[D A E B C] whose rank is [3 0 4 1 2].
The rank is defined in this way:
If
then
Let's test by plugging in the toy example:
M*R = [D A E B C]*R =[A B C D E]
Therefore
Then the rank vector is
Mrank=[0 1 2 3 4]*R-1=[3 0 4 1 2]
Indeed!!!!
Now let's prove that the sorted index of the sorted index is the rank vector:
Let's assume that M1 is the index of M and that M2 is the index of M1 .
So we have these:
MN1=[A B C D E]
where
[0 1 2 3 4]*N1=M1
And
M1N2=[0 1 2 3 4]
where
[0 1 2 3 4]*N2=M2
So based on Formula 1,
R=N1
Then based on definition, the rank vector is
[0 1 2 3 4]*R-1=[0 1 2 3 4]*N1-1
Then, what is N1-1 ?
Based on Formula 2 and Formula 3,
[0 1 2 3 4]*N1N2=[0 1 2 3 4]
So
N1N2=I
Therefore,
N1-1 = N2
Plug it into Formula 5 we get the rank vector
[0 1 2 3 4]*N2
Based on Formula 4, it is exactly M2