Categorical Sampler
-----
Install from pip: `pip install categorical-sampler`
Let’s generate a probability distribution to get us started. First, sample a bunch of random numbers to determine probability “scores”.
>>> from random import random
>>> k = 10**6
>>> scores = [random() for i in range(k)]
>>> total = sum(scores)
>>> probabilities = [s / total for s in scores]
We've normalized the scores to sum to 1, i.e. make
them into proper probabilities, but actually the categorical sampler will do that for us, so it’s not necessary:
>>> from categorical import Categorical as C
>>> my_sampler = C(scores)
>>> print my_sampler.sample()
487702
Comparing to numpy, assuming we draw 1000 individual samples *individually*:
>>> from numpy.random import choice
>>> import time
>>>
>>> def time_numpy():
>>> start = time.time()
>>> for i in range(1000):
>>> choice(k, p=probabilities)
>>> print time.time() - start
>>>
>>> def time_my_alias():
>>> start = time.time()
>>> for i in range(1000):
>>> my_sampler.sample()
>>> print time.time() - start
>>>
>>> time_numpy()
31.0555009842
>>> time_my_alias()
0.0127031803131
Get the actual probability of a given outcome:
>>> my_sampler.get_probability(487702)
1.0911282101090306e-06
-----
Install from pip: `pip install categorical-sampler`
Let’s generate a probability distribution to get us started. First, sample a bunch of random numbers to determine probability “scores”.
>>> from random import random
>>> k = 10**6
>>> scores = [random() for i in range(k)]
>>> total = sum(scores)
>>> probabilities = [s / total for s in scores]
We've normalized the scores to sum to 1, i.e. make
them into proper probabilities, but actually the categorical sampler will do that for us, so it’s not necessary:
>>> from categorical import Categorical as C
>>> my_sampler = C(scores)
>>> print my_sampler.sample()
487702
Comparing to numpy, assuming we draw 1000 individual samples *individually*:
>>> from numpy.random import choice
>>> import time
>>>
>>> def time_numpy():
>>> start = time.time()
>>> for i in range(1000):
>>> choice(k, p=probabilities)
>>> print time.time() - start
>>>
>>> def time_my_alias():
>>> start = time.time()
>>> for i in range(1000):
>>> my_sampler.sample()
>>> print time.time() - start
>>>
>>> time_numpy()
31.0555009842
>>> time_my_alias()
0.0127031803131
Get the actual probability of a given outcome:
>>> my_sampler.get_probability(487702)
1.0911282101090306e-06
Metadata
Release files for categorical 0.1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| categorical-0.1.4.tar.gz | 4.4 kB | Without provenance | Details |
Release files / categorical-0.1.4.tar.gz
| Download URL | categorical-0.1.4.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e6b532baa2561284557ed9b4c41c17a579ef372951ea5dffa739fce0d04df6b2
|
|
BLAKE2b-256 checksum How to use checksums |
1c822373e9f91656dbc2c1b90052ba7541cf9629bf021eef665419115d1dd9bf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for categorical 0.1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.