From 4db495b4c294e1453bd763b607f62534bf705820 Mon Sep 17 00:00:00 2001 From: pianomania Date: Mon, 14 Mar 2016 14:17:01 +0800 Subject: [PATCH 1/9] doc: state that parameter C can receive an array --- sklearn/linear_model/randomized_l1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn/linear_model/randomized_l1.py b/sklearn/linear_model/randomized_l1.py index bef06f59911bc..3afd722cd60eb 100644 --- a/sklearn/linear_model/randomized_l1.py +++ b/sklearn/linear_model/randomized_l1.py @@ -380,7 +380,7 @@ class RandomizedLogisticRegression(BaseRandomizedLinearModel): Parameters ---------- - C : float, optional, default=1 + C : float, or array of shape [n_C], optional, default=1 The regularization parameter C in the LogisticRegression. scaling : float, optional, default=0.5 From 3535c00b3d660fee0b917ded4be960ecf87e2c3c Mon Sep 17 00:00:00 2001 From: pianomania Date: Wed, 16 Mar 2016 20:20:37 +0800 Subject: [PATCH 2/9] add more details in doc, and check the dim of C --- sklearn/linear_model/randomized_l1.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/sklearn/linear_model/randomized_l1.py b/sklearn/linear_model/randomized_l1.py index 3afd722cd60eb..d873bec527d4d 100644 --- a/sklearn/linear_model/randomized_l1.py +++ b/sklearn/linear_model/randomized_l1.py @@ -357,6 +357,9 @@ def _randomized_logistic(X, y, weights, mask, C=1., verbose=False, X *= (1 - weights) C = np.atleast_1d(np.asarray(C, dtype=np.float64)) + if C.ndim > 1: + raise ValueError("C should be 1-dim array, but got %d-dim array instead." % C.ndim) + scores = np.zeros((X.shape[1], len(C)), dtype=np.bool) for this_C, this_scores in zip(C, scores.T): @@ -380,8 +383,12 @@ class RandomizedLogisticRegression(BaseRandomizedLinearModel): Parameters ---------- - C : float, or array of shape [n_C], optional, default=1 + C : float or array of shape [n_reg_parameter], optional, default=1 The regularization parameter C in the LogisticRegression. + When C is an array, fit will take each regularization parameter in C + one by one for LogisticRegression and store results for each one + in all_scores_, where columns and rows represent corresponding + reg_parameters and features, respectively. scaling : float, optional, default=0.5 The alpha parameter in the stability selection article used to From 8c23d91355756b66a1dddf8b3b69de6091f78183 Mon Sep 17 00:00:00 2001 From: pianomania Date: Mon, 14 Mar 2016 14:17:01 +0800 Subject: [PATCH 3/9] doc: state that parameter C can receive an array --- sklearn/linear_model/randomized_l1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sklearn/linear_model/randomized_l1.py b/sklearn/linear_model/randomized_l1.py index 52e522e6dc4c8..7de925eb379aa 100644 --- a/sklearn/linear_model/randomized_l1.py +++ b/sklearn/linear_model/randomized_l1.py @@ -381,7 +381,7 @@ class RandomizedLogisticRegression(BaseRandomizedLinearModel): Parameters ---------- - C : float, optional, default=1 + C : float, or array of shape [n_C], optional, default=1 The regularization parameter C in the LogisticRegression. scaling : float, optional, default=0.5 From 3f6ea47639f4e04b50b26f4ad516fa2cd5a8e7a2 Mon Sep 17 00:00:00 2001 From: pianomania Date: Wed, 16 Mar 2016 20:20:37 +0800 Subject: [PATCH 4/9] add more details in doc, and check the dim of C --- sklearn/linear_model/randomized_l1.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/sklearn/linear_model/randomized_l1.py b/sklearn/linear_model/randomized_l1.py index 7de925eb379aa..78d40a375d0d7 100644 --- a/sklearn/linear_model/randomized_l1.py +++ b/sklearn/linear_model/randomized_l1.py @@ -354,6 +354,9 @@ def _randomized_logistic(X, y, weights, mask, C=1., verbose=False, X *= (1 - weights) C = np.atleast_1d(np.asarray(C, dtype=np.float64)) + if C.ndim > 1: + raise ValueError("C should be 1-dim array, but got %d-dim array instead." % C.ndim) + scores = np.zeros((X.shape[1], len(C)), dtype=np.bool) for this_C, this_scores in zip(C, scores.T): @@ -381,8 +384,12 @@ class RandomizedLogisticRegression(BaseRandomizedLinearModel): Parameters ---------- - C : float, or array of shape [n_C], optional, default=1 + C : float or array of shape [n_reg_parameter], optional, default=1 The regularization parameter C in the LogisticRegression. + When C is an array, fit will take each regularization parameter in C + one by one for LogisticRegression and store results for each one + in all_scores_, where columns and rows represent corresponding + reg_parameters and features, respectively. scaling : float, optional, default=0.5 The s parameter used to randomly scale the penalty of different From 7e44df433db1b4d68fb9b3e758eb2b446f67feb9 Mon Sep 17 00:00:00 2001 From: pianomania Date: Fri, 17 Feb 2017 16:06:50 +0800 Subject: [PATCH 5/9] a little modification --- sklearn/linear_model/randomized_l1.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sklearn/linear_model/randomized_l1.py b/sklearn/linear_model/randomized_l1.py index 78d40a375d0d7..04c6fc03cf1b8 100644 --- a/sklearn/linear_model/randomized_l1.py +++ b/sklearn/linear_model/randomized_l1.py @@ -356,7 +356,7 @@ def _randomized_logistic(X, y, weights, mask, C=1., verbose=False, C = np.atleast_1d(np.asarray(C, dtype=np.float64)) if C.ndim > 1: raise ValueError("C should be 1-dim array, but got %d-dim array instead." % C.ndim) - + scores = np.zeros((X.shape[1], len(C)), dtype=np.bool) for this_C, this_scores in zip(C, scores.T): @@ -386,10 +386,10 @@ class RandomizedLogisticRegression(BaseRandomizedLinearModel): ---------- C : float or array of shape [n_reg_parameter], optional, default=1 The regularization parameter C in the LogisticRegression. - When C is an array, fit will take each regularization parameter in C - one by one for LogisticRegression and store results for each one - in all_scores_, where columns and rows represent corresponding - reg_parameters and features, respectively. + When C is an array, fit will take each regularization parameter in C + one by one for LogisticRegression and store results for each one + in all_scores_, where columns and rows represent corresponding + reg_parameters and features. scaling : float, optional, default=0.5 The s parameter used to randomly scale the penalty of different From 96deb3118b2bc72d1d46e493c02203892b671a1c Mon Sep 17 00:00:00 2001 From: pianomania Date: Fri, 17 Feb 2017 19:16:56 +0800 Subject: [PATCH 6/9] minor modifications and make line length less than 79 characters --- sklearn/linear_model/randomized_l1.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sklearn/linear_model/randomized_l1.py b/sklearn/linear_model/randomized_l1.py index e77aafd275541..3fd8c166cfa15 100644 --- a/sklearn/linear_model/randomized_l1.py +++ b/sklearn/linear_model/randomized_l1.py @@ -355,7 +355,8 @@ def _randomized_logistic(X, y, weights, mask, C=1., verbose=False, C = np.atleast_1d(np.asarray(C, dtype=np.float64)) if C.ndim > 1: - raise ValueError("C should be 1-dim array, but got %d-dim array instead." % C.ndim) + raise ValueError("C should be 1-dim array, \ + but got %d-dimentional array instead." % C.ndim) scores = np.zeros((X.shape[1], len(C)), dtype=np.bool) @@ -384,7 +385,7 @@ class RandomizedLogisticRegression(BaseRandomizedLinearModel): Parameters ---------- - C : float or array of shape [n_reg_parameter], optional, default=1 + C : float or array-like of shape [n_reg_parameter], optional, default=1 The regularization parameter C in the LogisticRegression. When C is an array, fit will take each regularization parameter in C one by one for LogisticRegression and store results for each one From 19a4dc672a9ffdee92ffe703a7df5c34ae6e7216 Mon Sep 17 00:00:00 2001 From: pianomania Date: Fri, 17 Feb 2017 19:48:25 +0800 Subject: [PATCH 7/9] remove the backslash and correct typos --- sklearn/linear_model/randomized_l1.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sklearn/linear_model/randomized_l1.py b/sklearn/linear_model/randomized_l1.py index 3fd8c166cfa15..84ff0b9a2f6f8 100644 --- a/sklearn/linear_model/randomized_l1.py +++ b/sklearn/linear_model/randomized_l1.py @@ -355,8 +355,8 @@ def _randomized_logistic(X, y, weights, mask, C=1., verbose=False, C = np.atleast_1d(np.asarray(C, dtype=np.float64)) if C.ndim > 1: - raise ValueError("C should be 1-dim array, \ - but got %d-dimentional array instead." % C.ndim) + raise ValueError("C should be 1-dimensional array, " + "but got %d-dimensional array instead." % C.ndim) scores = np.zeros((X.shape[1], len(C)), dtype=np.bool) From dc85f4ea677fcc158ce9d4823abeaf93b6ec1851 Mon Sep 17 00:00:00 2001 From: pianomania Date: Fri, 17 Feb 2017 22:11:16 +0800 Subject: [PATCH 8/9] meet PEP8's E128 requirement --- sklearn/linear_model/randomized_l1.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sklearn/linear_model/randomized_l1.py b/sklearn/linear_model/randomized_l1.py index 84ff0b9a2f6f8..f6ab09353d655 100644 --- a/sklearn/linear_model/randomized_l1.py +++ b/sklearn/linear_model/randomized_l1.py @@ -356,7 +356,8 @@ def _randomized_logistic(X, y, weights, mask, C=1., verbose=False, C = np.atleast_1d(np.asarray(C, dtype=np.float64)) if C.ndim > 1: raise ValueError("C should be 1-dimensional array, " - "but got %d-dimensional array instead." % C.ndim) + "but got a %d-dimensional array instead: %d." + % (C.ndim, C)) scores = np.zeros((X.shape[1], len(C)), dtype=np.bool) From da9061701967f16a853f1c564ff0e2d5866a002d Mon Sep 17 00:00:00 2001 From: pianomania Date: Sat, 18 Feb 2017 11:31:18 +0800 Subject: [PATCH 9/9] use .format and add a test --- sklearn/linear_model/randomized_l1.py | 6 +++--- sklearn/linear_model/tests/test_randomized_l1.py | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/sklearn/linear_model/randomized_l1.py b/sklearn/linear_model/randomized_l1.py index f6ab09353d655..56d8ad764198e 100644 --- a/sklearn/linear_model/randomized_l1.py +++ b/sklearn/linear_model/randomized_l1.py @@ -355,9 +355,9 @@ def _randomized_logistic(X, y, weights, mask, C=1., verbose=False, C = np.atleast_1d(np.asarray(C, dtype=np.float64)) if C.ndim > 1: - raise ValueError("C should be 1-dimensional array, " - "but got a %d-dimensional array instead: %d." - % (C.ndim, C)) + raise ValueError("C should be 1-dimensional array-like, " + "but got a {}-dimensional array-like instead: {}." + .format(C.ndim, C)) scores = np.zeros((X.shape[1], len(C)), dtype=np.bool) diff --git a/sklearn/linear_model/tests/test_randomized_l1.py b/sklearn/linear_model/tests/test_randomized_l1.py index 95bffc0fbd33b..0ba2a113a12d8 100644 --- a/sklearn/linear_model/tests/test_randomized_l1.py +++ b/sklearn/linear_model/tests/test_randomized_l1.py @@ -100,6 +100,9 @@ def test_randomized_logistic(): feature_scores = clf.fit(X, y).scores_ assert_array_equal(np.argsort(F), np.argsort(feature_scores)) + clf = RandomizedLogisticRegression(verbose=False, C=[[1., 0.5]]) + assert_raises(ValueError, clf.fit, X, y) + def test_randomized_logistic_sparse(): # Check randomized sparse logistic regression on sparse data