how to make argsort result to be random between equal values?

前端 未结 2 1010
广开言路
广开言路 2020-12-01 21:51

Say you have a numpy vector [0,3,1,1,1] and you run argsort you will get [0,2,3,4,1] but all the ones are the same! What

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 22:14

    This is a bit of a hack, but if your array contains integers only you could add random values and argsort the result. np.random.rand gives you results in [0, 1) so in this case you're guaranteed to maintain the order for non-identical elements.

    >>> import numpy as np
    >>> arr = np.array([0,3,1,1,1])
    >>> np.argsort(arr + np.random.rand(*arr.shape))
    array([0, 4, 3, 2, 1])
    >>> np.argsort(arr + np.random.rand(*arr.shape))
    array([0, 3, 4, 2, 1])
    >>> np.argsort(arr + np.random.rand(*arr.shape))
    array([0, 3, 4, 2, 1])
    >>> np.argsort(arr + np.random.rand(*arr.shape))
    array([0, 2, 3, 4, 1])
    >>> np.argsort(arr + np.random.rand(*arr.shape))
    array([0, 2, 3, 4, 1])
    >>> np.argsort(arr + np.random.rand(*arr.shape))
    array([0, 4, 2, 3, 1])
    

    Here we see index 0 is always first in the argsort result and index 1 is last, but the rest of the results are in a random order.

    In general you could generate random values bounded by np.diff(np.sort(arr)).max(), but you might run into precision issues at some point.

提交回复
热议问题