pythonnumpynp.argsort

np.argsort(np.zeros(17)) gives strange order


As the title suggests, the np.argsort(np.zeros(n)) can gives right result if n<=16, otherwise, it will give a strange result, e.g.,

np.argsort(np.zeros(17))
# array([ 0, 14, 13, 12, 11, 10,  9, 15,  8,  6,  5,  4,  3,  2,  1,  7, 16], dtype=int64)

np.argsort(np.zeros(18))
# array([ 0, 15, 14, 13, 12, 11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1, 16, 17], dtype=int64)

Can anyone explain? Thanks!


Solution

  • try ‘stable’ mode will work.

    np.argsort(np.zeros(17), kind='stable')
    # array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16],
          dtype=int64)