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
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.
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?
I have wondered about this in the past. One complication is simply that we
have no idea how much code out there was relying on the sample being a copy
and not a view...
I guess I can see why it's beneficial to sparse matrices, so I get it.
I suspect that this is not a trivial change in code, particularly because
of the separation of duties between the splitter and the indexation.
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.
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.
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.
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)
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.
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?
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 ;)
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.
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.
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.
Activity
jnothman commentedon Jun 6, 2019
mrocklin commentedon Jun 7, 2019
I think that even a conservative change like the following would be welcome:
In Dask at least the copy is free for us.
jnothman commentedon Jun 7, 2019
jnothman commentedon Jun 7, 2019
mrocklin commentedon Aug 14, 2019
We could also maybe send in a
cupy.random.RandomStateobject rather than anumpy.random.RandomStateobject so that we generate the random values on the GPU instead of on the CPU. This doesn't solve thenp.arangeproblem, but maybe that points us in another possible direction. We would still need to remove explicitnp.asarraycalls 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.
jakirkham commentedon Aug 14, 2019
Yeah with
np.arange, I was hoping we could write something like this...jakirkham commentedon Aug 14, 2019
There's also a call to
np.full. Currently that looks like this...We might want to generalize it to something like this...
mrocklin commentedon Aug 14, 2019
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.arangecalls withones_like/cumsumis maybe less welcome.jakirkham commentedon Aug 14, 2019
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.
jakirkham commentedon Aug 14, 2019
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
asarraywithout forcing array-like things to become NumPy arrays.cc @pentschev (whose involved in that discussion and also thinking about these things)
amueller commentedon Aug 14, 2019
Yeah it'll be a while till we can use numpy 1.17.
jakirkham commentedon Aug 14, 2019
Yeah it would be great if
RandomStatecreated the array and shuffled it for us, but wasn't finding the API we would need to do that..shuffledoes not seem to work if one doesrs.shuffle(5)for example.jakirkham commentedon Aug 21, 2019
Another concern that Ben helped identify earlier is that if we want to allow a user-defined
RandomStateobject, we'd want to make sure this passescheck_random_state. Currently that means the user-definedRandomStateobject would need to subclass NumPy'sRandomStateobject (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 theRandomStatesatisfies some abstract base class or has some particular methods?scikit-learn/sklearn/utils/validation.py
Lines 775 to 782 in 4b6273b
amueller commentedon Aug 21, 2019
@jakirkham I don't see why not.
thomasjpfan commentedon Aug 21, 2019
Which interface do you have in mind? The interface for cupy.random.RandomState and np.random.RandomState are pretty extensive.
amueller commentedon Aug 21, 2019
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
uniformmethod it's much more likely to be something that implements sampling than an integer, I think ;)jakirkham commentedon Sep 9, 2019
cc @ogrisel (as we discussed this during EuroSciPy)
ogrisel commentedon May 13, 2022
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.
ogrisel commentedon Nov 27, 2025
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.
ogrisel commentedon Nov 27, 2025
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.