kwarray.distributions
¶
Defines data structures for efficient repeated sampling of specific distributions (e.g. Normal, Uniform, Binomial) with specific parameters.
Inspired by ~/code/imgaug/imgaug/parameters.py
- Similar Libraries:
Todo
[ ] change sample shape to just a single num.
[ ] Some Distributions will output vectors. Maybe we could just postpend the dimensions?
[ ] Expose as kwstats?
Module Contents¶
Classes¶
Defaults to a uniform distribution over floats between 0 and 1 |
|
Example |
|
Example |
|
Uniform distribution over integers. |
|
>>> self = Normal(mean=100, rng=0)
|
|
self = Normal() |
|
self = Normal() |
|
Example |
|
A truncated normal distribution. |
- class kwarray.distributions.Uniform(low=0, high=1, rng=None)¶
Bases:
Distribution
Defaults to a uniform distribution over floats between 0 and 1
Example
>>> self = Uniform(rng=0) >>> self.sample() 0.548813... >>> float(self.sample(1)) 0.7151...
- Benchmark:
>>> import ubelt as ub >>> self = Uniform() >>> for timer in ub.Timerit(100, bestof=10): >>> with timer: >>> [self() for _ in range(100)] >>> for timer in ub.Timerit(100, bestof=10): >>> with timer: >>> self(100)
- sample(self, *shape)¶
- classmethod coerce(cls, arg)¶
- class kwarray.distributions.Exponential(scale=1, rng=None)¶
Bases:
Distribution
Example
>>> self = Exponential(rng=0) >>> # xdoctest: +REQUIRES(--show) >>> import kwplot >>> kwplot.autompl() >>> kwplot.figure(fnum=1, doclf=True) >>> self._show(500, bins=25)
- sample(self, *shape)¶
- class kwarray.distributions.Constant(value=0, rng=None)¶
Bases:
Distribution
Example
>>> self = Constant(42, rng=0) >>> self.sample() 42 >>> self.sample(3) array([42, 42, 42])
- sample(self, *shape)¶
- class kwarray.distributions.DiscreteUniform(min=0, max=1, rng=None)¶
Bases:
Distribution
Uniform distribution over integers.
- Parameters
min (int) – inclusive minimum
max (int) – exclusive maximum
Example
>>> self = DiscreteUniform.coerce(4) >>> self.sample(100)
- sample(self, *shape)¶
- classmethod coerce(cls, arg, rng=None)¶
- class kwarray.distributions.Normal(mean=0, std=1, rng=None)¶
Bases:
Distribution
>>> self = Normal(mean=100, rng=0) >>> self.sample() >>> self.sample(100)
- sample(self, *shape)¶
- class kwarray.distributions.Bernoulli(p=0.5, rng=None)¶
Bases:
Distribution
self = Normal() self.sample() self.sample(1)
- sample(self, *shape)¶
- classmethod coerce(cls, arg)¶
- class kwarray.distributions.Binomial(n=1, p=0.5, rng=None)¶
Bases:
Distribution
self = Normal() self.sample() self.sample(1)
- sample(self, *shape)¶
- class kwarray.distributions.Categorical(categories, weights=None, rng=None)¶
Bases:
Distribution
Example
>>> categories = [3, 5, 1] >>> weights = [.05, .5, .45] >>> self = Categorical(categories, weights, rng=0) >>> self.sample() 5 >>> list(self.sample(2)) [1, 1] >>> self.sample(2, 3) array([[5, 5, 1], [5, 1, 1]])
- sample(self, *shape)¶
- class kwarray.distributions.TruncNormal(mean=0, std=1, low=- np.inf, high=np.inf, rng=None)¶
Bases:
Distribution
A truncated normal distribution.
A normal distribution, but bounded by low and high values. Note this is much different from just using a clipped normal.
- Parameters
mean (float) – mean of the distribution
std (float) – standard deviation of the distribution
low (float) – lower bound
high (float) – upper bound
rng (np.random.RandomState)
Example
>>> self = TruncNormal(rng=0) >>> self() # output of this changes before/after scipy version 1.5 ...0.1226...
Example
>>> low = -np.pi / 16 >>> high = np.pi / 16 >>> std = np.pi / 8 >>> self = TruncNormal(low=low, high=high, std=std, rng=0) >>> shape = (3, 3) >>> data = self(*shape) >>> print(ub.repr2(data, precision=5)) np.array([[ 0.01841, 0.0817 , 0.0388 ], [ 0.01692, -0.0288 , 0.05517], [-0.02354, 0.15134, 0.18098]], dtype=np.float64)
- _update_internals(self)¶
- sample(self, *shape)¶