question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

np.random.choice unusable for array dimensions > 1

See original GitHub issue

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

np.random.choice(points)

ValueError: a must be 1-dimensional

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:

np.random.choice(list(points))

ValueError: a must be 1-dimensional
np.random.choice(tuple(points))

ValueError: a must be 1-dimensional
np.random.choice([row for row in points])

ValueError: a must be 1-dimensional

Whereas this can be done with random.sample:

import random
random.sample(list(points), 1)

[array([0.77376144, 0.64678796])]

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:27
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
njsmithcommented, Apr 1, 2018

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.

3reactions
rgommerscommented, Apr 25, 2020

FWIW the issue currently has 10 +1, which would rank it 12th of 1,864 among open issues on that metric. Perhaps it’s time to consider reopening?

Those are from before adding Generator.choice, which solves the problem. We’d like to avoid changing the legacy functions at this point.

At least it would be nice to improve discovery for the new recommended method (which does solve the problem AFAICT).

That is a good point. I opened PR gh-16075 to do that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy.random.choice — NumPy v1.24 Manual
Generates a random sample from a given 1-D array ... If a is an int and less than zero, if a or p...
Read more >
how to randomly sample in 2D matrix in numpy - Stack Overflow
Just use a random index (in your case 2 because you have 3 dimensions): import numpy as np Space_Position = np.array(Space_Position) ...
Read more >
Numpy Random Seed, Explained - Sharp Sight
In this tutorial, I'll explain how to use the NumPy random seed function, which is also called np.random.seed or numpy.random.seed.
Read more >
numpy.random.choice — NumPy v1.9 Manual
... size=None, replace=True, p=None)¶. Generates a random sample from a given 1-D array ... Generate a uniform random sample from np.arange(5) of size...
Read more >
TFRecord and tf.train.Example | TensorFlow Core
Feature message type can accept one of the following three types (See the .proto file for ... feature0 = np.random.choice([False, True], n_observations)
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found