n x 3ndarray and wanted to sort it by 3-rd column.In 3-rd column there MAY BE some
np.inf. Anyway, I used
sortedfunction. After that I used
njitdecorator on my main function but got different result… So I wrote that code to check what’s going on:
samples = [0, 1, 2, 442]– there is no differences. Why?
Answer:Your function tell to Numba/CPython to sort lines based on the second item which contains Inf values. Two infinity values are considered equal so a sorting algorithm can change the order of the lines having Inf values in the second item of the target lines. The different results is due to different algorithms being used. This is expected because the sorting algorithm is not guaranteed to be stable (ie. preserve the order of equal lines).
To solve the problem, you need to use a stable algorithm.
np.argsortcan be used to find the ordering of the value and the parameter
kindcan be tuned so to choose a stable algorithm (called
np.argsortshould also be faster than
sortedon Numpy arrays because it does not cause many lambda calls nor compute Numpy lines as slow CPython objects. This should do the job:
If you have better answer, please add a comment about this, thank you!