-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
np.random.choice unusable for array dimensions > 1 #10835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You can sample a random set of row indices: row_i = np.random.choice(points.shape[0], ...)
points[row_i, :] This also allows for sampling a random set of columns. |
Maybe needs a line in the documentation of |
The problem with @njsmith's solution is that, say you have a list of arrays. The expected behavior for numpy.random.choice is for it to return a random object from the list. But attempting to do this can result in |
it seems there is a solution proposed in PR #7810 |
I think I just ran into this problem with a list of arrays. I want to pick a random array from a list of arrays: x = np.zeros((10,10)) This results in the unexpected error "ValueError: a must be 1-dimensional". Note that if x and y have different shapes, random.choice works as expected. |
The recommended method for making a random choice is now to use the For example, here's the example from top of this issue:
Here's the most recent example:
I don't think there are any plans to make improvements to the legacy random code, so I'm closing the issue, but we can reopen it if the demand is high. |
I too would like to sample from a 2d numpy array but I'm restricted to 1d arrays . :( |
FWIW the issue currently has 10 👍, which would rank it 12th of 1,864 among open issues on that metric. Perhaps it's time to consider reopening? At least it would be nice to improve discovery for the new recommended method (which does solve the problem AFAICT). I only found out about it because of @WarrenWeckesser's comment. |
Addresses a comment on numpygh-10835.
Those are from before adding
That is a good point. I opened PR gh-16075 to do that. |
think alternatively, |
Uh oh!
There was an error while loading. Please reload this page.
NumPy version 1.14.2
It's not possible to grab a random row from a 2d array using np.random.choice. Consider this array:
points = np.random.random((10,2))
Trying to get a random row this way fails
which might be reasonable. (A more reasonable behavior is perhaps for np.random.choice to take an optional axis= argument so that it can return a random slice along the axis, defaulting to a random element in the entire array.)
However, there is no way to directly grab a random row:
Whereas this can be done with random.sample:
The text was updated successfully, but these errors were encountered: