• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: sorted in njit numba

Resolved: sorted in njit numba

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I have n x 3 ndarray and wanted to sort it by 3-rd column.In 3-rd column there MAY BE some np.inf. Anyway, I used sorted function. After that I used njit decorator on my main function but got different result… So I wrote that code to check what’s going on:
And there is really some differences. But when 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.argsort can be used to find the ordering of the value and the parameter kind can be tuned so to choose a stable algorithm (called "stable"). Additionally, np.argsort should also be faster than sorted on 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!

numba numpy python
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Reshape tensors of unknown shape with tf.function

26/03/2023

Resolved: Use Svelte Component as Slot

26/03/2023

Resolved: Vaadin 14.9 – Redirect on Session Destroy Event

26/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.