8000 ENH: random: Add the method `permuted` to Generator. by WarrenWeckesser · Pull Request #15121 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: random: Add the method permuted to Generator. #15121

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

Merged
merged 14 commits into from
Sep 2, 2020
Merged
Changes from 1 commit
Commits
10000
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
DOC: Fix examples and move method summary to the top.
  • Loading branch information
WarrenWeckesser committed Aug 20, 2020
commit f5d6f0b6fc1be998b272641d99460c88f3ed868d
58 changes: 27 additions & 31 deletions doc/source/reference/random/generator.rst
EB73
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,29 @@ Simple random data

Permutations
============
The methods for randomly permuting a sequence are

.. autosummary::
:toctree: generated/

~numpy.random.Generator.shuffle
~numpy.random.Generator.permutation
~numpy.random.Generator.permuted

The methods `Generator.shuffle`, `Generator.permutation` and
`Generator.permuted` are used for randomly permuting an input array.
The following describes the important differences among these methods.
The following table summarizes the behaviors of the methods.

+--------------+-------------------+------------------+
| method | copy/in-place | axis handling |
+==============+===================+==================+
| shuffle | in-place | as if 1d |
+--------------+-------------------+------------------+
| permutation | copy | as if 1d |
+--------------+-------------------+------------------+
| permuted | either (use 'out' | axis independent |
| | for in-place) | |
+--------------+-------------------+------------------+

The following subsections provide more details about the differences.

In-place vs. copy
~~~~~~~~~~~~~~~~~
Expand All @@ -51,6 +70,8 @@ By default, `Generator.permuted` returns a copy. To operate in-place with
`Generator.permuted`, pass the same array as the first argument *and* as
the value of the ``out`` parameter. For example,

>>> rg = np.random.default_rng()
>>> x = np.arange(0, 15).reshape(3, 5)
>>> x
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
Expand Down Expand Up @@ -82,9 +103,9 @@ array, and ``axis=1`` will rearrange the columns. For example
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
>>> rg.permutation(x, axis=1)
array([[ 0, 1, 4, 3, 2], # random
[ 5, 6, 9, 8, 7],
[10, 11, 14, 13, 12]])
array([[ 1, 3, 2, 0, 4], # random
[ 6, 8, 7, 5, 9],
[11, 13, 12, 10, 14]])

Note that the columns have been rearranged "in bulk": the values within
each column have not changed.
Expand Down Expand Up @@ -115,31 +136,6 @@ For example,
>>> a
['B', 'D', 'A', 'E', 'C'] # random


Permutation methods summary
~~~~~~~~~~~~~~~~~~~~~~~~~~~
The methods for randomly permuting a sequence are

.. autosummary::
:toctree: generated/

~numpy.random.Generator.shuffle
~numpy.random.Generator.permutation
~numpy.random.Generator.permuted

The following table summarizes the behaviors of the methods.

+--------------+-------------------+------------------+
| method | copy/in-place | axis handling |
+==============+===================+==================+
| shuffle | in-place | as if 1d |
+--------------+-------------------+------------------+
| permutation | copy | as if 1d |
+--------------+-------------------+------------------+
| permuted | either (use 'out' | axis independent |
| | for in-place) | |
+--------------+-------------------+------------------+

Distributions
=============
.. autosummary::
Expand Down
0