When dealing with lists that contain duplicate data, np.argsort fails to
return index values that correspond to the actual sorting positions of the
data, as it does when handling arrays without duplicates.

Dear Author:

When I use the np.argsort function on an array without duplicate data, the
returned index values correspond to the sorting positions of the respective
data.😀

x = [1, 2, 5, 4]
rank = np.argsort(x)
print(rank)
# [0 1 3 2]

However, when there are duplicate values, the results from np.argsort
sometimes do not correspond to the sorting positions of the respective data
.

x = [1, 4, 1, 1, 2, 4, 5]
rank = np.argsort(x)
print(rank)
# [0 2 3 4 1 5 6]

Assuming a person frequently uses np.argsort to obtain positions by sorting
data without duplicates, introducing duplicate values in the data may lead
to inconspicuous errors in positions that are difficult to detect.
Moreover, as the dataset grows, identifying such issues may become even
more challenging.

For users in this situation, the desired results might be achieved using
the following function:

import numpy as np

def my_sort(x):
    arg_x = np.sort(x)
    rank = [np.where(arg_x == i)[0][0] for i in x]
    return np.array(rank)

x = [1, 4, 1, 1, 2, 4, 5]
rank_arg = np.argsort(x)
rank_position = my_sort(x)
print("rank_arg",rank_arg)
print("rank_position",rank_position)
# rank_arg [0 2 3 4 1 5 6]
# rank_position [0 4 0 0 3 4 6]

This method produces results consistent with np.argsort when applied to
arrays without duplicate values.

x = [1, 2, 5, 4]
rank_arg = np.argsort(x)
rank_position = my_sort(x)
print("rank_arg",rank_arg)
print("rank_position",rank_position)
# rank_arg [0 1 3 2]
# rank_position [0 1 3 2]

Although there is no issue with the documentation of np.argsort itself, the
need you've highlighted may be widespread. Therefore, it might be worth
considering the addition of a function, for example, np.position(data), to
enhance the functionality of numpy.

My device:

system: win10 64x python version: 3.9.11 numpy version: 1.21.2

Sincerely, Looking forward to your response! ❤❤❤❤
_______________________________________________
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com

Reply via email to