[SciPy-Dev] numpy.random.* call convention

Robert Kern robert.kern at gmail.com
Thu Jun 7 01:44:38 EDT 2018


On Wed, Jun 6, 2018 at 10:26 PM Robert Rehammar <robert.open at rehammar.se>
wrote:
>
> Dear num-py developers,
>
> For the methods in numpy.random, the distributions typically have the
> signature (a, b, ...[, size]). a, b, ... are parameters to the
> distributions and size is optional that can be used to control how may
> samples to draw and the shape of the returned structure. The parameters
> can also be array_like to draw from different (parametrized)
distributions.
>
> However, it is not what I can see, possible to have the parameters
> array_like and at the same time use size!=None.

Sure, you can! Let `shape` be `a.shape` (or the broadcasted shape of all of
those parameters. *Prepend* your desired number (or shape) of draws to this
`shape` to get the `size` that you need to specify. So if I have two
different `scale` parameters for a normal distribution, and I want 12 draws
from each in a (3,4) shape (for whatever forsaken reason):

[~]
|4> np.random.normal(0.0, [1.0, 2.0], size=(3, 4, 2))
array([[[ 1.72551057,  2.33545059],
        [-1.45966289,  4.81820745],
        [-0.13912257,  1.79127867],
        [ 0.27693464,  1.45313416]],

       [[ 1.41031607,  3.18113465],
        [ 1.64033152,  1.47355763],
        [ 1.18554024, -1.11605743],
        [ 0.73556545,  2.44352574]],

       [[-0.42889339,  3.88389374],
        [-0.24146162,  0.54163374],
        [ 0.53821574,  0.07862412],
        [ 0.7418073 , -2.35439217]]])

It's *not* a particularly intuitive API, obviously, but it should let you
do everything that you want to do.

--
Robert Kern
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scipy-dev/attachments/20180606/65cd5665/attachment.html>


More information about the SciPy-Dev mailing list