Convert numpy.array to order of elements when duplicate values are present

Akavall

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
Warren Weckesser

@Jaime's answer is great (as usual!). Here's an alternative, using scipy.stats.rankdata.

In rankdata's terminology, you want a "dense" ranking. You also want to rank the values in the opposite order than usual. To accomplish the reverse order, we'll pass -a to rankdata. We'll also subtract 1 from the ranking so the ranks begin at 0 instead of 1. Finally, you want to rank the rows of a two-dimensional array. rankdata works on one-dimensional data, so we'll have to loop over the rows.

Here's the code:

import numpy as np
from scipy.stats import rankdata


def get_order_array(a):
    b = np.empty(a.shape, dtype=int)
    for k, row in enumerate(a):
        b[k] = rankdata(-row, method='dense') - 1
    return b


if __name__ == "__main__":    
    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]])
    print get_order_array(a)

Output:

[[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]]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Order of elements in a numpy array

From Dev

Order of elements in a numpy array

From Java

Removing numpy array columns with the same non-missing value, when missing values present

From Dev

How to order a php array based on duplicate values

From Dev

How can I order an array with duplicate values?

From Dev

How to order a php array based on duplicate values

From Dev

numpy select fixed amount of values among duplicate values in array

From Dev

PHP - How to test a multidimensional array for duplicate element values in any order

From Dev

Sort array in ascending order and remove duplicate values in objective- c

From Dev

PHP - How to test a multidimensional array for duplicate element values in any order

From Dev

Removing elements from an array of objects based on duplicate values of multiple keys

From Dev

Removing elements from an array of objects based on duplicate values of multiple keys

From Dev

How do I duplicate the values for the last dimension in a numpy array?

From Dev

Checking for and indexing non-unique/duplicate values in a numpy array

From Dev

Checking for and indexing non-unique/duplicate values in a numpy array

From Dev

Sort numpy array by row and order matching values based on original array

From Dev

Sort numpy array by order of matching values of another array

From Dev

Numpy: np_array[index_order_array] changes the order of elements in an array?

From Dev

Convert Array to Hash removing duplicate keys and adding values at the same time

From Dev

How to sort an array of objects in the order of another array containing the unique values of a property present in each object?

From Dev

Convert python list with None values to numpy array with nan values

From Dev

Duplicate array elements in Ruby

From Dev

Duplicate Elements in Array (SML)

From Dev

Store duplicate array elements

From Dev

removing duplicate array elements

From Dev

JS - Duplicate Elements in the Array

From Dev

Array push, duplicate elements

From Dev

Delete duplicate elements in an array

From Dev

Remove duplicate elements in array

Related Related

  1. 1

    Order of elements in a numpy array

  2. 2

    Order of elements in a numpy array

  3. 3

    Removing numpy array columns with the same non-missing value, when missing values present

  4. 4

    How to order a php array based on duplicate values

  5. 5

    How can I order an array with duplicate values?

  6. 6

    How to order a php array based on duplicate values

  7. 7

    numpy select fixed amount of values among duplicate values in array

  8. 8

    PHP - How to test a multidimensional array for duplicate element values in any order

  9. 9

    Sort array in ascending order and remove duplicate values in objective- c

  10. 10

    PHP - How to test a multidimensional array for duplicate element values in any order

  11. 11

    Removing elements from an array of objects based on duplicate values of multiple keys

  12. 12

    Removing elements from an array of objects based on duplicate values of multiple keys

  13. 13

    How do I duplicate the values for the last dimension in a numpy array?

  14. 14

    Checking for and indexing non-unique/duplicate values in a numpy array

  15. 15

    Checking for and indexing non-unique/duplicate values in a numpy array

  16. 16

    Sort numpy array by row and order matching values based on original array

  17. 17

    Sort numpy array by order of matching values of another array

  18. 18

    Numpy: np_array[index_order_array] changes the order of elements in an array?

  19. 19

    Convert Array to Hash removing duplicate keys and adding values at the same time

  20. 20

    How to sort an array of objects in the order of another array containing the unique values of a property present in each object?

  21. 21

    Convert python list with None values to numpy array with nan values

  22. 22

    Duplicate array elements in Ruby

  23. 23

    Duplicate Elements in Array (SML)

  24. 24

    Store duplicate array elements

  25. 25

    removing duplicate array elements

  26. 26

    JS - Duplicate Elements in the Array

  27. 27

    Array push, duplicate elements

  28. 28

    Delete duplicate elements in an array

  29. 29

    Remove duplicate elements in array

HotTag

Archive