Author piotrjurkiewicz
Recipients piotrjurkiewicz
Date 2018-07-25.15:58:34
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1532534314.27.0.56676864532.issue34227@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2018-07-25 15:58:34piotrjurkiewiczsetrecipients: + piotrjurkiewicz
2018-07-25 15:58:34piotrjurkiewiczsetmessageid: <1532534314.27.0.56676864532.issue34227@psf.upfronthosting.co.za>
2018-07-25 15:58:34piotrjurkiewiczlinkissue34227 messages
2018-07-25 15:58:34piotrjurkiewiczcreate