Skip to content

_weighted_percentile NaN handling with array API #31368

Open

Description

@lucyleeow

There isn't necessarily anything to fix here, but I thought it would be useful to open this for documentation, at least.


_weighted_percentile added support for NaN in #29034 and support for array APIs in #29431.

Our implementation relys on sort putting NaN values at the end:

largest_value_per_column = array[
sorted_idx[-1, ...], xp.arange(n_features, device=device)
]
# NaN values get sorted to end (largest value)
if xp.any(xp.isnan(largest_value_per_column)):

AFAICT (confirmed by @ev-br) array API specs do not specify how sort should handle NaN, which means it is left to individual packages to determine.

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.

cc @StefanieSenger @ogrisel

Activity

  1. added
    Needs Triage
    on May 15, 2025
  2. StefanieSenger commented on May 16, 2025

    @StefanieSenger
    Member

    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?

  3. lucyleeow commented on May 16, 2025

    @lucyleeow
    MemberAuthor

    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.

  4. lucyleeow commented on May 19, 2025

    @lucyleeow
    MemberAuthor

    So there is a note that states:

    the sort order of NaNs and signed zeros is unspecified and thus implementation-dependent.

    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.

  5. StefanieSenger commented on May 19, 2025

    @StefanieSenger
    Member

    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 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.

    Thanks for looking into this! ❤

  6. removed
    Needs Triage
    on May 19, 2025
  7. lucyleeow commented on May 19, 2025

    @lucyleeow
    MemberAuthor

    that is with the C order(?)

    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.

  8. StefanieSenger commented on May 19, 2025

    @StefanieSenger
    Member

    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.

  9. ogrisel commented on Sep 11, 2025

    @ogrisel
    Member

    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.

  10. moved this to Discussion in Array APIon Nov 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    • Status
      Discussion

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions