Skip to content
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

DOC: Improve doc for numpy.random.Generator.choice #18223

Merged
merged 4 commits into from
Jan 26, 2021

Conversation

SnoopJ
Copy link
Contributor

@SnoopJ SnoopJ commented Jan 25, 2021

The summary of numpy.random.Generator.choice() suggests that the function is restricted to 1-D inputs, but this is not the case.

This PR removes the "1-D" qualifier from the summary and adds an example demonstrating use of this function for sampling from an array of dimension greater than one.

@@ -665,6 +665,13 @@ cdef class Generator:
array([3,1,0]) # random
>>> #This is equivalent to rng.permutation(np.arange(5))[:3]

Generate a uniform random sample from a 2-D array, without
replacement:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer a wording that emphasizes that it chooses along the first axis. This wording can be read to mean that it selects values from all of the elements of the 2D array (i.e. equivalent to choice(x.ravel())).

@charris charris changed the title Improve doc for numpy.random.Generator.choice DOC: Improve doc for numpy.random.Generator.choice Jan 25, 2021
>>> rng.choice([[0, 1, 2], [3, 4, 5], [6, 7, 8]], 2, replace=False)
array([[3, 4, 5],
[0, 1, 2]])

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is raising a documentation error.

File "build/testenv/lib/python3.7/site-packages/numpy/random/_generator.cpython-37m-darwin.so", line ?, in Generator.choice
Failed example:
    rng.choice([[0, 1, 2], [3, 4, 5], [6, 7, 8]], 2, replace=False)
Expected:
    array([[3, 4, 5],
           [0, 1, 2]])
Got:
    array([[6, 7, 8],
           [3, 4, 5]])

You need to use a seed to make it repeatable, or add # may vary to the first output line. Grep to see examples.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it had occurred to me, but I guess I missed the # random in the other examples and couldn't find anything about it in the developer documentation. Cheers!

@charris charris merged commit fd99622 into numpy:master Jan 26, 2021
@charris
Copy link
Member

charris commented Jan 26, 2021

Thanks @SnoopJeDi .

@SnoopJ SnoopJ deleted the fix_random-generator-choice_doc branch April 1, 2022 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants