You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
_weighted_percentile NaN handling with array API #31368
As everything works, I don't think we need to do anything here (especially as we ultimately want to drop maintaining our own quantile function), but just thought it would be useful to document.
I think it would be nice to define a handling of NaNs for xp.sort (and other functions?) in the spec, in order to define the status quo as a standard (also for future array libraries that want to adopt the array api standard). Maybe we open an issue in the data-apis repo?
I agree that since the currently supported array libraries in scikit-learn all sort NaNs to the end (that is with the C order(?)), we're fine for now, and in case a new array library would handle this differently, there will be many places where this problem would pop up and then this will be re-discussed.
we assume arrays are stored by default C-contiguous (vs F-contiguous) (I don't have a default reference explaining this but you should be able to find articles on this).
I vaguely recall a note somewhere in the code about us assuming arrays are C-contiguous but I can't find it now.
Yea, I had read about this, but I think somewhere in numpy's docs. It's a question on what language (C or Fortran) the way of storing arrays is optimised for. I didn't find any directive on how scikit-learn deals with this. For the time being, I will just accept that we prefer C-ordered arrays then.
Coming back to this discussion and just to clarify something: the nan-ordering (nan values pushed to start or end of the sorted array) and the default memory layout (rowwise or columnwise contiguity) are two distinct questions, but both impact how correct our array API support: our code makes assumptions at different places about one or the other: we assume that other array API namespaces behave like numpy does: allocate 2D arrays with rowwise contiguity (C-contiguous) and sort nan values in the end positions of the sorted array.
The array API spec does not guarantee that all namespaces will behave this way for either question. Still, according to our existing tests, this is the case for all namespaces we tested so far.
The correct fix would be to inspect that this actually the case and adapt the code accordingly if it is not, but this would introduce complex code branches for hypothetical namespaces we have never encountered yet and that therefore might not exist.
I think we can leave our code this way and rely on our tests to check if this will always be the case for other namespaces: it's quite easy to tweak the test suite to launch it against a new namespace if we want to make sure that it works as expected with scikit-learn.
Activity
StefanieSenger commentedon May 16, 2025
I think it would be nice to define a handling of NaNs for
xp.sort(and other functions?) in the spec, in order to define the status quo as a standard (also for future array libraries that want to adopt the array api standard). Maybe we open an issue in thedata-apisrepo?lucyleeow commentedon May 16, 2025
Agreed, it's on my to-do list. The outcome may just be that the spec explicitly states that nans handling method is left to the package though.
lucyleeow commentedon May 19, 2025
So there is a note that states:
see: https://data-apis.org/array-api/latest/API_specification/sorting_functions.html
So while it is not guaranteed to work, I think our implementation is acceptable. Maybe it's just like our assumption that arrays are stored 'C' order.
StefanieSenger commentedon May 19, 2025
I see now, and I have also missed that note.
I agree that since the currently supported array libraries in scikit-learn all sort NaNs to the end (that is with the
Corder(?)), we're fine for now, and in case a new array library would handle this differently, there will be many places where this problem would pop up and then this will be re-discussed.Thanks for looking into this! ❤
lucyleeow commentedon May 19, 2025
we assume arrays are stored by default C-contiguous (vs F-contiguous) (I don't have a default reference explaining this but you should be able to find articles on this).
I vaguely recall a note somewhere in the code about us assuming arrays are C-contiguous but I can't find it now.
StefanieSenger commentedon May 19, 2025
Yea, I had read about this, but I think somewhere in numpy's docs. It's a question on what language (C or Fortran) the way of storing arrays is optimised for. I didn't find any directive on how scikit-learn deals with this. For the time being, I will just accept that we prefer C-ordered arrays then.
ogrisel commentedon Sep 11, 2025
Coming back to this discussion and just to clarify something: the nan-ordering (nan values pushed to start or end of the sorted array) and the default memory layout (rowwise or columnwise contiguity) are two distinct questions, but both impact how correct our array API support: our code makes assumptions at different places about one or the other: we assume that other array API namespaces behave like numpy does: allocate 2D arrays with rowwise contiguity (C-contiguous) and sort nan values in the end positions of the sorted array.
The array API spec does not guarantee that all namespaces will behave this way for either question. Still, according to our existing tests, this is the case for all namespaces we tested so far.
The correct fix would be to inspect that this actually the case and adapt the code accordingly if it is not, but this would introduce complex code branches for hypothetical namespaces we have never encountered yet and that therefore might not exist.
I think we can leave our code this way and rely on our tests to check if this will always be the case for other namespaces: it's quite easy to tweak the test suite to launch it against a new namespace if we want to make sure that it works as expected with scikit-learn.