Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DOC: Draw more attention to which functions in random are convenience wrappers #13035

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 2 commits into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion doc/source/user/numpy-for-matlab-users.rst
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ Linear Algebra Equivalents
``a``

* - ``rand(3,4)``
- ``random.rand(3,4)``
- ``random.rand(3,4)`` or ``random.random_sample((3, 4))``
- random 3x4 matrix

* - ``linspace(1,3,4)``
Expand Down
76 changes: 53 additions & 23 deletions numpy/random/mtrand/mtrand.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,12 @@ cdef class RandomState:

Random values in a given shape.

.. note::
This is a convenience function for users porting code from Matlab,
and wraps `numpy.random.random_sample`. That function takes a
tuple to specify the size of the output, which is consistent with
other NumPy functions like `numpy.zeros` and `numpy.ones`.

Create an array of the given shape and populate it with
random samples from a uniform distribution
over ``[0, 1)``.
Expand All @@ -1347,12 +1353,6 @@ cdef class RandomState:
--------
random

Notes
-----
This is a convenience function. If you want an interface that
takes a shape-tuple as the first argument, refer to
np.random.random_sample .

Examples
--------
>>> np.random.rand(3,2)
Expand All @@ -1372,16 +1372,17 @@ cdef class RandomState:

Return a sample (or samples) from the "standard normal" distribution.

If positive, int_like or int-convertible arguments are provided,
`randn` generates an array of shape ``(d0, d1, ..., dn)``, filled
with random floats sampled from a univariate "normal" (Gaussian)
distribution of mean 0 and variance 1 (if any of the :math:`d_i` are
floats, they are first converted to integers by truncation). A single
float randomly sampled from the distribution is returned if no
argument is provided.
.. note::
This is a convenience function for users porting code from Matlab,
and wraps `numpy.random.standard_normal`. That function takes a
tuple to specify the size of the output, which is consistent with
other NumPy functions like `numpy.zeros` and `numpy.ones`.

This is a convenience function. If you want an interface that takes a
tuple as the first argument, use `numpy.random.standard_normal` instead.
If positive int_like arguments are provided, `randn` generates an array
of shape ``(d0, d1, ..., dn)``, filled
Copy link
Member

Choose a reason for hiding this comment

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

Might as well justify the text a bit more.

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes the diff noisier and the two commits harder to separate, would prefer not to

with random floats sampled from a univariate "normal" (Gaussian)
Copy link
Member

Choose a reason for hiding this comment

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

Don't need quotes around "normal".

Copy link
Member Author

Choose a reason for hiding this comment

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

Inherited from the old docstring

distribution of mean 0 and variance 1. A single float randomly sampled
from the distribution is returned if no argument is provided.

Parameters
----------
Expand All @@ -1399,6 +1400,7 @@ cdef class RandomState:
See Also
--------
standard_normal : Similar, but takes a tuple as its argument.
normal : Also accepts mu and sigma arguments.

Notes
-----
Expand All @@ -1409,14 +1411,13 @@ cdef class RandomState:
Examples
--------
>>> np.random.randn()
2.1923875335537315 #random
2.1923875335537315 # random

Two-by-four array of samples from N(3, 6.25):

>>> 2.5 * np.random.randn(2, 4) + 3
array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], #random
[ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) #random

>>> 3 + 2.5 * np.random.randn(2, 4)
array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], # random
[ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) # random
"""
if len(args) == 0:
return self.standard_normal()
Expand Down Expand Up @@ -1536,20 +1537,43 @@ cdef class RandomState:
Returns
-------
out : float or ndarray
Drawn samples.
A floating-point array of shape ``size`` of drawn samples, or a
single sample if ``size`` was not specified.

Notes
-----
For random samples from :math:`N(\\mu, \\sigma^2)`, use one of::

mu + sigma * np.random.standard_normal(size=...)
np.random.normal(mu, sigma, size=...)

See Also
--------
normal :
Equivalent function with additional ``loc`` and ``scale`` arguments
for setting the mean and standard deviation.

Examples
--------
>>> np.random.standard_normal()
2.1923875335537315 #random

>>> s = np.random.standard_normal(8000)
>>> s
array([ 0.6888893 , 0.78096262, -0.89086505, ..., 0.49876311, #random
-0.38672696, -0.4685006 ]) #random
array([ 0.6888893 , 0.78096262, -0.89086505, ..., 0.49876311, # random
-0.38672696, -0.4685006 ]) # random
>>> s.shape
(8000,)
>>> s = np.random.standard_normal(size=(3, 4, 2))
>>> s.shape
(3, 4, 2)

Two-by-four array of samples from :math:`N(3, 6.25)`:

>>> 3 + 2.5 * np.random.standard_normal(size=(2, 4))
array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], # random
[ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) # random

"""
return cont0_array(self.internal_state, rk_gauss, size, self.lock)

Expand Down Expand Up @@ -1642,6 +1666,12 @@ cdef class RandomState:
... linewidth=2, color='r')
>>> plt.show()

Two-by-four array of samples from N(3, 6.25):

>>> np.random.normal(3, 2.5, size=(2, 4))
array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677], # random
[ 0.39924804, 4.68456316, 4.99394529, 4.84057254]]) # random

"""
cdef ndarray oloc, oscale
cdef double floc, fscale
Expand Down