Skip to content

Commit

Permalink
MAINT: address review
Browse files Browse the repository at this point in the history
  • Loading branch information
stsievert committed Jul 4, 2018
1 parent 91d43d7 commit 8e75640
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
7 changes: 4 additions & 3 deletions doc/whats_new/v0.20.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,10 @@ Misc
:issue:`10280` by `Joel Nothman`_ and :user:`Aman Dalmia <dalmia>`.

- A new random variable, :class:`utils.random.loguniform` implements a
log-uniform random variable. For example, the outcomes ``1``, ``10`` and
``100`` are all equally likely for ``loguniform(0, 2)``. See :issue:`11232`
by :user:`Scott Sievert <stsievert>` and :user:`Nathaniel Saul <sauln>`.
log-uniform random variable for use in RandomizedSearchCV. For example, the
outcomes ``1``, ``10`` and ``100`` are all equally likely for ``loguniform(0,
2)``. See :issue:`11232` by :user:`Scott Sievert <stsievert>` and
:user:`Nathaniel Saul <sauln>`.

Enhancements
............
Expand Down
46 changes: 25 additions & 21 deletions sklearn/utils/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,30 +202,36 @@ def random_choice_csc(n_samples, classes, class_probability=None,


class loguniform:
"""
A class supporting log-uniform random variables. It generates values
between ``base**a`` and ``base**b`` or
"""A class supporting log-uniform random variables.
Parameters
----------
low : float
The log minimum value
high : float
The log maximum value
base : float
The base for the exponent.
Methods
-------
rvs(self, size=None, random_state=None)
Generate log-uniform random variables
base**a <= loguniform(a, b, base=base).rvs() <= base**b
Notes
-----
This class generates values between ``base**low`` and ``base**high`` or
base**low <= loguniform(low, high, base=base).rvs() <= base**high
The logarithmic probability density function (PDF) is uniform. When
``x`` is a uniformly distributed random variable between 0 and 1, ``10**x``
are random variales that are equally likely to be the result of
``loguniform(0, 1, base=10).rvs()``.
are random variales that are equally likely to be returned.
"""
def __init__(self, low, high, base=10):
"""
Create a log-uniform random variable.
Parameters
----------
low : float
The log minimum value
high : float
The log maximum value
base : float
The base for the exponent.
"""
self._low = low
self._high = high
Expand All @@ -234,14 +240,13 @@ def __init__(self, low, high, base=10):
def rvs(self, size=None, random_state=None):
"""
Generates random variables with ``base**low <= rv <= base**high``
where ``base``, ``low`` and ``high`` are definited in ``__init__`` and
``rv`` is the return value of this function.
where ``rv`` is the return value of this function.
Parameters
----------
size : int or tuple, optional
The size of the random variable.
random_state : int, RandomState
random_state : int, RandomState, optional
A seed (int) or random number generator (RandomState).
Returns
Expand All @@ -250,7 +255,6 @@ def rvs(self, size=None, random_state=None):
Either a single log-uniform random variable or an array of them
"""
_rng = check_random_state(random_state)
a, b, base = self._low, self._high, self._base
unif = _rng.uniform(a, b, size=size)
rv = np.power(base, unif)
unif = _rng.uniform(self._low, self._high, size=size)
rv = np.power(self._base, unif)
return rv

0 comments on commit 8e75640

Please sign in to comment.