Search code examples
pythonarraysnumpyranking

Numpy Array Rank All Elements


I have a two-dimensional numpy array and I am wondering how I can create a new two-dimensional numpy array represent the ranking of the values based on all items in the original 2d array.

I would like to use the following array :

anArray = [[ 18.5,  25.9,   7.4,  11.1,  11.1]
           [ 33.3,  37. ,  14.8,  22.2,  25.9]
           [ 29.6,  29.6,  11.1,  14.8,  11.1]
           [ 25.9,  25.9,  14.8,  14.8,  11.1]
           [ 29.6,  25.9,  14.8,  11.1,   7.4]]

to create a new rank ordered array [based on all values and having same rank for multiple numbers] :

anOrder = [[ 6,  4,  9,  8,  8]
           [ 2,  1,  7,  5,  4]
           [ 3,  3,  8,  7,  8]
           [ 4,  4,  7,  7,  8]
           [ 3,  4,  7,  8,  9]]

Thank you.


Solution

  • That's a routine work for np.unique with its optional argument return_inverse that tags each element based on the uniqueness among other elements, like so -

    _,id = np.unique(anArray,return_inverse=True)
    out = (id.max() - id + 1).reshape(anArray.shape)
    

    Sample run -

    In [17]: anArray
    Out[17]: 
    array([[ 18.5,  25.9,   7.4,  11.1,  11.1],
           [ 33.3,  37. ,  14.8,  22.2,  25.9],
           [ 29.6,  29.6,  11.1,  14.8,  11.1],
           [ 25.9,  25.9,  14.8,  14.8,  11.1],
           [ 29.6,  25.9,  14.8,  11.1,   7.4]])
    
    In [18]: _,id = np.unique(anArray,return_inverse=True)
    
    In [19]: (id.max() - id + 1).reshape(anArray.shape)
    Out[19]: 
    array([[6, 4, 9, 8, 8],
           [2, 1, 7, 5, 4],
           [3, 3, 8, 7, 8],
           [4, 4, 7, 7, 8],
           [3, 4, 7, 8, 9]])