Skip to content

Commit

Permalink
DOC: Minor docfixes and cosmits
Browse files Browse the repository at this point in the history
  • Loading branch information
MechCoder committed Jul 28, 2014
1 parent 00f8c62 commit 75b78f2
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 50 deletions.
116 changes: 68 additions & 48 deletions sklearn/linear_model/coordinate_descent.py
Expand Up @@ -465,12 +465,10 @@ def enet_path(X, y, l1_ratio=0.5, eps=1e-3, n_alphas=100, alphas=None,
n_iters = []

rng = check_random_state(params.get('random_state', None))

shuffle = False
selection = params.get('selection', 'cyclic')
if selection == 'random':
shuffle = True

if selection not in ['random', 'cyclic']:
raise ValueError("selection should be either random or cyclic.")
shuffle = (selection == 'random')
models = []

if not multi_output:
Expand Down Expand Up @@ -626,13 +624,16 @@ class ElasticNet(LinearModel, RegressorMixin):
positive: bool, optional
When set to ``True``, forces the coefficients to be positive.
random_state : int, RandomState instance, or None (default)
The seed of the pseudo random number generator that selects
a random feature to update. Useful only when shuffle is set to True.
selection : str, default 'cyclic'
If set to 'random', a random coefficient is updated every iteration
rather than looping over features sequentially by default.
rather than looping over features sequentially by default. This
(setting to 'random') often leads to significantly faster convergence
especially when tol is higher than 1e-4.
random_state : int, RandomState instance, or None (default)
The seed of the pseudo random number generator that selects
a random feature to update. Useful only when selection is set to
'random'.
Attributes
----------
Expand Down Expand Up @@ -846,13 +847,16 @@ class Lasso(ElasticNet):
positive : bool, optional
When set to ``True``, forces the coefficients to be positive.
random_state : int, RandomState instance, or None (default)
The seed of the pseudo random number generator that selects
a random feature to update. Useful only when shuffle is set to True.
selection : str, default 'cyclic'
If set to 'random', a random coefficient is updated every iteration
rather than looping over features sequentially by default.
rather than looping over features sequentially by default. This
(setting to 'random') often leads to significantly faster convergence
especially when tol is higher than 1e-4.
random_state : int, RandomState instance, or None (default)
The seed of the pseudo random number generator that selects
a random feature to update. Useful only when selection is set to
'random'.
Attributes
----------
Expand Down Expand Up @@ -1266,13 +1270,16 @@ class LassoCV(LinearModelCV, RegressorMixin):
positive : bool, optional
If positive, restrict regression coefficients to be positive
random_state : int, RandomState instance, or None (default)
The seed of the pseudo random number generator that selects
a random feature to update. Useful only when shuffle is set to True.
selection : str, default 'cyclic'
If set to 'random', a random coefficient is updated every iteration
rather than looping over features sequentially by default.
rather than looping over features sequentially by default. This
(setting to 'random') often leads to significantly faster convergence
especially when tol is higher than 1e-4.
random_state : int, RandomState instance, or None (default)
The seed of the pseudo random number generator that selects
a random feature to update. Useful only when selection is set to
'random'.
Attributes
----------
Expand Down Expand Up @@ -1390,13 +1397,16 @@ class ElasticNetCV(LinearModelCV, RegressorMixin):
positive : bool, optional
When set to ``True``, forces the coefficients to be positive.
random_state : int, RandomState instance, or None (default)
The seed of the pseudo random number generator that selects
a random feature to update. Useful only when shuffle is set to True.
selection : str, default 'cyclic'
If set to 'random', a random coefficient is updated every iteration
rather than looping over features sequentially by default.
rather than looping over features sequentially by default. This
(setting to 'random') often leads to significantly faster convergence
especially when tol is higher than 1e-4.
random_state : int, RandomState instance, or None (default)
The seed of the pseudo random number generator that selects
a random feature to update. Useful only when selection is set to
'random'.
Attributes
----------
Expand Down Expand Up @@ -1532,13 +1542,16 @@ class MultiTaskElasticNet(Lasso):
When set to ``True``, reuse the solution of the previous call to fit as
initialization, otherwise, just erase the previous solution.
random_state : int, RandomState instance, or None (default)
The seed of the pseudo random number generator that selects
a random feature to update. Useful only when shuffle is set to True.
selection : str, default 'cyclic'
If set to 'random', a random coefficient is updated every iteration
rather than looping over features sequentially by default.
rather than looping over features sequentially by default. This
(setting to 'random') often leads to significantly faster convergence
especially when tol is higher than 1e-4.
random_state : int, RandomState instance, or None (default)
The seed of the pseudo random number generator that selects
a random feature to update. Useful only when selection is set to
'random'.
Attributes
----------
Expand Down Expand Up @@ -1647,9 +1660,7 @@ def fit(self, X, y):

if self.selection not in ['random', 'cyclic']:
raise ValueError("selection should be either random or cyclic.")
shuffle = False
if self.selection == 'random':
shuffle = True
shuffle = (self.selection == 'random')

self.coef_, self.dual_gap_, self.eps_, self.n_iter_ = \
cd_fast.enet_coordinate_descent_multi_task(
Expand Down Expand Up @@ -1708,13 +1719,16 @@ class MultiTaskLasso(MultiTaskElasticNet):
When set to ``True``, reuse the solution of the previous call to fit as
initialization, otherwise, just erase the previous solution.
random_state : int, RandomState instance, or None (default)
The seed of the pseudo random number generator that selects
a random feature to update. Useful only when shuffle is set to True.
selection : str, default 'cyclic'
If set to 'random', a random coefficient is updated every iteration
rather than looping over features sequentially by default.
rather than looping over features sequentially by default. This
(setting to 'random') often leads to significantly faster convergence
especially when tol is higher than 1e-4
random_state : int, RandomState instance, or None (default)
The seed of the pseudo random number generator that selects
a random feature to update. Useful only when selection is set to
'random'.
Attributes
----------
Expand Down Expand Up @@ -1837,13 +1851,16 @@ class MultiTaskElasticNetCV(LinearModelCV, RegressorMixin):
all the CPUs. Note that this is used only if multiple values for
l1_ratio are given.
random_state : int, RandomState instance, or None (default)
The seed of the pseudo random number generator that selects
a random feature to update. Useful only when shuffle is set to True.
selection : str, default 'cyclic'
If set to 'random', a random coefficient is updated every iteration
rather than looping over features sequentially by default.
rather than looping over features sequentially by default. This
(setting to 'random') often leads to significantly faster convergence
especially when tol is higher than 1e-4.
random_state : int, RandomState instance, or None (default)
The seed of the pseudo random number generator that selects
a random feature to update. Useful only when selection is set to
'random'.
Attributes
----------
Expand Down Expand Up @@ -1982,13 +1999,16 @@ class MultiTaskLassoCV(LinearModelCV, RegressorMixin):
all the CPUs. Note that this is used only if multiple values for
l1_ratio are given.
random_state : int, RandomState instance, or None (default)
The seed of the pseudo random number generator that selects
a random feature to update. Useful only when shuffle is set to True.
selection : str, default 'cyclic'
If set to 'random', a random coefficient is updated every iteration
rather than looping over features sequentially by default.
rather than looping over features sequentially by default. This
(setting to 'random') often leads to significantly faster convergence
especially when tol is higher than 1e-4.
random_state : int, RandomState instance, or None (default)
The seed of the pseudo random number generator that selects
a random feature to update. Useful only when selection is set to
'random'.
Attributes
----------
Expand Down
11 changes: 9 additions & 2 deletions sklearn/linear_model/tests/test_coordinate_descent.py
Expand Up @@ -516,8 +516,10 @@ def test_warm_start_convergence_with_regularizer_decrement():


def test_random_descent():
"""Test that both random and cyclic selection give the same results
when converged fully and using all conditions.
"""Test that both random and cyclic selection give the same results.
Ensure that the test models fully converge fully and check a wide
range of conditions.
"""

# This uses the coordinate descent algo using the gram trick.
Expand Down Expand Up @@ -556,6 +558,11 @@ def test_random_descent():
assert_almost_equal(clf_cyclic.intercept_, clf_random.intercept_)


# Raise error when selection is not in cyclic or random.
clf_random = ElasticNet(selection='invalid')
assert_raises(ValueError, clf_random.fit, X, y)


if __name__ == '__main__':
import nose
nose.runmodule()

0 comments on commit 75b78f2

Please sign in to comment.