From 75b78f2e12a4947b0cfb81a1a601b691bb45ea02 Mon Sep 17 00:00:00 2001 From: MechCoder Date: Mon, 28 Jul 2014 18:53:48 +0200 Subject: [PATCH] DOC: Minor docfixes and cosmits --- sklearn/linear_model/coordinate_descent.py | 116 ++++++++++-------- .../tests/test_coordinate_descent.py | 11 +- 2 files changed, 77 insertions(+), 50 deletions(-) diff --git a/sklearn/linear_model/coordinate_descent.py b/sklearn/linear_model/coordinate_descent.py index e38dbc8b032cb..a9131fd19e6bd 100644 --- a/sklearn/linear_model/coordinate_descent.py +++ b/sklearn/linear_model/coordinate_descent.py @@ -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: @@ -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 ---------- @@ -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 ---------- @@ -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 ---------- @@ -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 ---------- @@ -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 ---------- @@ -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( @@ -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 ---------- @@ -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 ---------- @@ -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 ---------- diff --git a/sklearn/linear_model/tests/test_coordinate_descent.py b/sklearn/linear_model/tests/test_coordinate_descent.py index 7910a1ad3fd5b..b46cc2fe7228f 100644 --- a/sklearn/linear_model/tests/test_coordinate_descent.py +++ b/sklearn/linear_model/tests/test_coordinate_descent.py @@ -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. @@ -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()