Skip to content

[QST] Updating indices creation for split #14036

Closed as not planned
Closed as not planned

Description

@quasiben

I am interested in using GridSearchCV machinery from sklearn with non-numpy libraries like cuDF or dask.array. In exploring this work, I think functions like _iter_test_indices and _iter_test_masks could be amended slightly to return slices instead of numpy arrays.

For example in _iter_test_indices:

def _iter_test_indices(self, X, y=None, groups=None):
n_samples = _num_samples(X)
indices = np.arange(n_samples)
if self.shuffle:
check_random_state(self.random_state).shuffle(indices)
n_splits = self.n_splits
fold_sizes = np.full(n_splits, n_samples // n_splits, dtype=np.int)
fold_sizes[:n_samples % n_splits] += 1
current = 0
for fold_size in fold_sizes:
start, stop = current, current + fold_size
yield indices[start:stop]
current = stop

If there is a shuffle, much of the code could be encapsulated in the shuffle conditional. And when there is
no shuffle, we can yield the appropriate slice object: slice(0, 71)...

I wanted to check here before proceeding with the work and/or if folks have any advice before I begin?

Activity

  1. jnothman commented on Jun 6, 2019

    @jnothman
    Member
  2. mrocklin commented on Jun 7, 2019

    @mrocklin

    I think that even a conservative change like the following would be welcome:

    chunk = x[slc].copy()

    In Dask at least the copy is free for us.

  3. jnothman commented on Jun 7, 2019

    @jnothman
    Member
  4. jnothman commented on Jun 7, 2019

    @jnothman
    Member
  5. mrocklin commented on Aug 14, 2019

    @mrocklin

    We could also maybe send in a cupy.random.RandomState object rather than a numpy.random.RandomState object so that we generate the random values on the GPU instead of on the CPU. This doesn't solve the np.arange problem, but maybe that points us in another possible direction. We would still need to remove explicit np.asarray calls on inputs, and allow array-like arguments through in some cases.

    This came up in conversation with @ogrisel a couple of months ago.

    Also cc @jakirkham , who has been looking at related problems recently.

  6. jakirkham commented on Aug 14, 2019

    @jakirkham
    Contributor

    Yeah with np.arange, I was hoping we could write something like this...

    np.cumsum(np.ones_like(X, dtype="i8", shape=(n_samples,))) - 1
  7. jakirkham commented on Aug 14, 2019

    @jakirkham
    Contributor

    There's also a call to np.full. Currently that looks like this...

    np.full(n_splits, n_samples // n_splits, dtype=np.int)

    We might want to generalize it to something like this...

    np.full_like(X, n_samples // n_splits, shape=(n_splits,), dtype=np.int)
  8. mrocklin commented on Aug 14, 2019

    @mrocklin

    Right, so that opens up a question for Scikit-Learn maintainers of if they're willing to accept code like this that is less intuitive in a few places for the case where the inputs are Numpy-like arrays.

    My guess is that accepting a user-defined RandomState object is fine (it looks like there is good precedent for this already) while replacing np.arange calls with ones_like/cumsum is maybe less welcome.

  9. jakirkham commented on Aug 14, 2019

    @jakirkham
    Contributor

    There's of course the point that this syntax only works with NumPy 1.17, which may be it's own kind of deal breaker. Anyways figured I'd float these snippets to start the conversation.

  10. jakirkham commented on Aug 14, 2019

    @jakirkham
    Contributor

    Also there's a separate discussion about having a way to cast between different arrays in a subthread of issue ( numpy/numpy#13831 ), which is ostensibly about handling asarray without forcing array-like things to become NumPy arrays.

    cc @pentschev (whose involved in that discussion and also thinking about these things)

  11. amueller commented on Aug 14, 2019

    @amueller
    Member

    Yeah it'll be a while till we can use numpy 1.17.

  12. jakirkham commented on Aug 14, 2019

    @jakirkham
    Contributor

    My guess is that accepting a user-defined RandomState object is fine...

    Yeah it would be great if RandomState created the array and shuffled it for us, but wasn't finding the API we would need to do that. .shuffle does not seem to work if one does rs.shuffle(5) for example.

  13. jakirkham commented on Aug 21, 2019

    @jakirkham
    Contributor

    Another concern that Ben helped identify earlier is that if we want to allow a user-defined RandomState object, we'd want to make sure this passes check_random_state. Currently that means the user-defined RandomState object would need to subclass NumPy's RandomState object (please see code below). This is a bit undesirable as we will end up carrying around a Cython base class that has some irrelevant state. Not sure how well this will behave in practice either. Would it be possible to generalize this check to merely make sure the RandomState satisfies some abstract base class or has some particular methods?

    if seed is None or seed is np.random:
    return np.random.mtrand._rand
    if isinstance(seed, numbers.Integral):
    return np.random.RandomState(seed)
    if isinstance(seed, np.random.RandomState):
    return seed
    raise ValueError('%r cannot be used to seed a numpy.random.RandomState'
    ' instance' % seed)

  14. amueller commented on Aug 21, 2019

    @amueller
    Member

    @jakirkham I don't see why not.

  15. thomasjpfan commented on Aug 21, 2019

    @thomasjpfan
    Member

    Would it be possible to generalize this check to merely make sure the RandomState satisfies some abstract base class or has some particular methods?

    Which interface do you have in mind? The interface for cupy.random.RandomState and np.random.RandomState are pretty extensive.

  16. amueller commented on Aug 21, 2019

    @amueller
    Member

    we kinda only need to distinguish it from an integer or None, right? We could check for a single common method existing. If we then try to use some obscure method and it's not implemented, well that's a reasonable error.
    If it has a uniform method it's much more likely to be something that implements sampling than an integer, I think ;)

  17. jakirkham commented on Sep 9, 2019

    @jakirkham
    Contributor

    cc @ogrisel (as we discussed this during EuroSciPy)

  18. ogrisel commented on May 13, 2022

    @ogrisel
    Member

    It would be great if someone wanted to try to prototype Array API spec support in scikit-learn Cross-Validation tools in a draft PR on top of the #22554 PR.

  19. ogrisel commented on Nov 27, 2025

    @ogrisel
    Member

    The current status is that CV splitters now all return integer arrays for indices, and the CV tools have been updating to convert the indexing arrays to the namespace and device of the data containers to resample.

  20. ogrisel commented on Nov 27, 2025

    @ogrisel
    Member

    The indexing data to move around is not that big so I think this is ok to do it this way.

    It's a pragmatic solution for now. The alternative would be to have array-agnostic way to deal with random number generators, but I don't think it's possible in the foreseeable future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions