[New-bugs-announce] [issue34227] Weighted random.sample() (weighted sampling without replacement)
Piotr Jurkiewicz
report at bugs.python.org
Wed Jul 25 11:58:34 EDT 2018
New submission from Piotr Jurkiewicz <piotr.jerzy.jurkiewicz at gmail.com>:
Function random.choices(), which appeared in Python 3.6, allows to perform weighted random sampling with replacement. Function random.sample() performs random sampling without replacement, but cannot do it weighted.
I propose to enhance random.sample() to perform weighted sampling. That way all four possibilities will be supported:
- non-weighted sampling with replacement: random.choices(..., weights=None) (exists)
- weighted sampling with replacement: random.choices(..., weights=weights) (exists)
- non-weighted sampling without replacement: random.sample(..., weights=None) (exists)
- weighted sampling without replacement: random.sample(..., weights=weights) (NEW)
Rationale:
Weighted sampling without replacement is a popular problem. There are lot of questions on StackOverflow and similar sites how to implement it. Unfortunately, many proposed solutions are wrong, for example:
https://stackoverflow.com/a/353510/2178047
https://softwareengineering.stackexchange.com/a/233552/161807
or have excessive computational complexity (e.g. quadratic). There are lot of suggestions to use numpy.random.choice() to do that, which supports all four possibilities with a single function:
numpy.random.choice(a, size=None, replace=True, p=None)
But of course this is an overkill to install numpy just to do that.
I think that this should be possible with stdlib, without the need to implement it by yourself or to install numpy. Especially, that it can be implemented in 2 lines (plus 4 lines of error checking), as you can see in the PR.
----------
components: Library (Lib)
messages: 322367
nosy: piotrjurkiewicz
priority: normal
severity: normal
status: open
title: Weighted random.sample() (weighted sampling without replacement)
type: enhancement
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34227>
_______________________________________
More information about the New-bugs-announce
mailing list