On Thu, Oct 8, 2026 at 10:15 AM Nolan Middleton via NumPy-Discussion <
[email protected]> wrote:

> Dear NumPy community,
>
> Currently, the random.choice() function takes two arguments: an array (or
> int), a, of things to randomly draw from, and an array, p, of
> probabilities, which must sum to one. However, there are some situations in
> which it may be easier/more comprehensible to supply a list of relative
> weights instead of probabilities, for instance if you are working with
> frequencies. I would like to suggest adding a keyword argument to the
> random.choice() function, maybe "weights," that, if supplied instead of p,
> will use the probabilities derived from weights (i.e., set p = [w /
> sum(weights) for w in weights]).
>

Since `p=np.array(weights) / sum(weights)` is a relatively trivial
expression that can be placed inline, we tend to not add another way to
specify the same thing. For me, I think the threshold for adding a new
argument would be if `choice` were written in such a way that it used the
`weights` directly in its algorithm and the normalization risked precision
loss, but I don't think that's an important effect here. I do think that it
*is* possible to rewrite the `choice` algorithm in such a way, but I think
the accuracy improvement would be marginal and not worth the
stream-changing churn. I'm open to demonstrations that unnormalized weights
are superior in terms of accuracy, though, especially if there are some
commonly-encountered edge cases. Sampling from unnormalized _logits_ with
the Gumbel Top-k Trick are even more interesting, but I _think_ we looked
at that before already (and it's easy enough to construct with what exists).

-- 
Robert Kern
_______________________________________________
NumPy-Discussion mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/numpy-discussion.python.org
Member address: [email protected]

Reply via email to