[SciPy-User] SciPy-User Digest, Vol 120, Issue 19

Johannes Radinger johannesradinger at gmail.com
Wed Aug 28 03:34:35 EDT 2013


Thank you Robert, that sounds like a very straight forward solution to that
task.
The working example following the given data can look like:

np.random.multinomial(300, y.flat, size=1).reshape(y.shape)


However, I had a similar discussion a while ago already in stackoverflow
where a different solution has been proposed:

np.bincount(np.searchsorted(np.cumsum(y), np.random.random(300)),
minlength=y.size).reshape(y.size)

respectively for numpy > 1.7
np.bincount(np.searchsorted(np.random.choice(y.size, 300, p=y.flat),
minlength=y.size).reshape(y.size)

So what I am wondering is what is the actual difference in the meaning
behind both approaches?
Do they actually provide results based on a totally different meaning of
"weighted random"? And what are the consequences then? As am not really
familiar with statistics maybe someone can clarify that?


Thanks a lot!

cheers,
Johannes


On Tue, Aug 27, 2013 at 7:00 PM, <scipy-user-request at scipy.org> wrote:

> Send SciPy-User mailing list submissions to
>         scipy-user at scipy.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         http://mail.scipy.org/mailman/listinfo/scipy-user
> or, via email, send a message with subject or body 'help' to
>         scipy-user-request at scipy.org
>
> You can reach the person managing the list at
>         scipy-user-owner at scipy.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of SciPy-User digest..."
>
>
> Today's Topics:
>
>    1. Populate an array with integers (randomly, weighted)
>       (Johannes Radinger)
>    2. Re: Populate an array with integers (randomly,    weighted)
>       (Robert Kern)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Tue, 27 Aug 2013 09:54:13 +0200
> From: Johannes Radinger <johannesradinger at gmail.com>
> Subject: [SciPy-User] Populate an array with integers (randomly,
>         weighted)
> To: scipy-user at scipy.org
> Message-ID:
>         <CABsGe_yqbiZG-N9Wzm4-kJSp-9wSXxSAgcV-x7c=
> JU_OCVic3Q at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> Hi,
>
> I'd like to randomly populate a numpy array (100,100) so that the
> numpy.sum() of the array equals exactly 300
> (e.g. distribute 300 rice grains on a chess board). The final array should
> only contain integers (so no floats) from
> 0 to X (max 300).  Furthermore I'd like to use a weighing factor, so that
> some of my array-cells are more probably to
> be populated than others. Therefore I have an array of the same size
> (100,100) and that is in numpy.sum = 1:
>
> x = numpy.random.random_sample((10,10))
> y = x/numpy.sum(x)
>
> Some side conditions: Of course if one cell has exactly 0 as weighing
> factor, also the result has to be 0 for that
> cell. If only one cell has a weighing factor at all (all others are 0) the
> entire 300 has to be assigned to that cell.
>
> Can that easily be achieved with Scipy/Numpy?
>
> Thank you so much for your help,
>
> Johannes
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
> http://mail.scipy.org/pipermail/scipy-user/attachments/20130827/a4b7164a/attachment-0001.html
>
> ------------------------------
>
> Message: 2
> Date: Tue, 27 Aug 2013 10:03:36 +0100
> From: Robert Kern <robert.kern at gmail.com>
> Subject: Re: [SciPy-User] Populate an array with integers (randomly,
>         weighted)
> To: SciPy Users List <scipy-user at scipy.org>
> Message-ID:
>         <
> CAF6FJis6fUhLQ_OF3XmzkiFVqvKmLJuD1JZB8Mq2f0ypju8gxw at mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> On Tue, Aug 27, 2013 at 8:54 AM, Johannes Radinger <
> johannesradinger at gmail.com> wrote:
> >
> > Hi,
> >
> > I'd like to randomly populate a numpy array (100,100) so that the
> numpy.sum() of the array equals exactly 300
> > (e.g. distribute 300 rice grains on a chess board). The final array
> should only contain integers (so no floats) from
> > 0 to X (max 300).  Furthermore I'd like to use a weighing factor, so that
> some of my array-cells are more probably to
> > be populated than others. Therefore I have an array of the same size
> (100,100) and that is in numpy.sum = 1:
> >
> > x = numpy.random.random_sample((10,10))
> > y = x/numpy.sum(x)
> >
> > Some side conditions: Of course if one cell has exactly 0 as weighing
> factor, also the result has to be 0 for that
> > cell. If only one cell has a weighing factor at all (all others are 0)
> the entire 300 has to be assigned to that cell.
>
> You are describing a multinomial distribution:
>
>
> http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.multinomial.html
>
> --
> Robert Kern
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
> http://mail.scipy.org/pipermail/scipy-user/attachments/20130827/661b044a/attachment-0001.html
>
> ------------------------------
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
> End of SciPy-User Digest, Vol 120, Issue 19
> *******************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20130828/caf9e0e5/attachment.html>


More information about the SciPy-User mailing list