Sampling algorithms implemented in pure Python 3.6+.
- Unlike the standard library
random
module, weighted sampling without replacement is implemented here. - The
Urn
class provides a general interface for sampling which is more efficient than callingsample
many times.
The software is available through GitHub, and through PyPI.
You may install the software using pip
.
pip install sampling
from sampling import sample
population = ['a', 'b', 'b', 'c']
weights = [7, 1, 2, 4]
# No replacement, no weights
samples = sample(population, size=2)
# With replacement and with weights
samples = sample(population, size=2, replace=True, weights=weights)
from sampling import Urn
import itertools
population = ['a', 'b', 'b', 'c', 'd', 'e']
weights = [7, 1, 2, 4, 3, 1]
# Create an urn and draw a single sample
urn = Urn(population, weights=weights)
sample = next(urn)
# Draw 2 more samples
samples = list(itertools.islice(urn, 2))
# Draw the remaining samples
remaining_samples = list(urn)
from sampling import Urn
population = ['a', 'b', 'b', 'c', 'd', 'e']
weights = [7, 1, 2, 4, 3, 1]
# Create an urn and draw a single sample
urn = Urn(population, replace=True, weights=weights)
# Enter an infinite loop of sampling with replacement
for element in urn:
# Perform logic here
if element == "a":
break
- For many common use cases, the NumPy
np.random.choice
function is faster than this pure Python implementation.
You are very welcome to scrutinize the code and make pull requests if you have suggestions and improvements. Your submitted code must be PEP8 compliant, and all tests must pass.