Skip to content

Commit

Permalink
[MRG] Fixed randomness of test_logreg_cv_penalty (#12624)
Browse files Browse the repository at this point in the history
* fixed randomness of test_logreg_cv_penalty

* Added comment about warm-starting coefficients
  • Loading branch information
NicolasHug authored and amueller committed Dec 17, 2018
1 parent 2904920 commit 03ac360
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions sklearn/linear_model/tests/test_logistic.py
Expand Up @@ -1150,14 +1150,31 @@ def test_logreg_l1_sparse_data():

@pytest.mark.filterwarnings('ignore: Default multi_class will') # 0.22
@pytest.mark.filterwarnings('ignore: You should specify a value') # 0.22
def test_logreg_cv_penalty():
# Test that the correct penalty is passed to the final fit.
X, y = make_classification(n_samples=50, n_features=20, random_state=0)
lr_cv = LogisticRegressionCV(penalty="l1", Cs=[1.0], solver='saga')
@pytest.mark.parametrize("random_seed", [42])
@pytest.mark.parametrize("penalty", ["l1", "l2"])
def test_logistic_regression_cv_refit(random_seed, penalty):
# Test that when refit=True, logistic regression cv with the saga solver
# converges to the same solution as logistic regression with a fixed
# regularization parameter.
# Internally the LogisticRegressionCV model uses a warm start to refit on
# the full data model with the optimal C found by CV. As the penalized
# logistic regression loss is convex, we should still recover exactly
# the same solution as long as the stopping criterion is strict enough (and
# that there are no exactly duplicated features when penalty='l1').
X, y = make_classification(n_samples=50, n_features=20,
random_state=random_seed)
common_params = dict(
solver='saga',
penalty=penalty,
random_state=random_seed,
max_iter=10000,
tol=1e-12,
)
lr_cv = LogisticRegressionCV(Cs=[1.0], refit=True, **common_params)
lr_cv.fit(X, y)
lr = LogisticRegression(penalty="l1", C=1.0, solver='saga')
lr = LogisticRegression(C=1.0, **common_params)
lr.fit(X, y)
assert_equal(np.count_nonzero(lr_cv.coef_), np.count_nonzero(lr.coef_))
assert_array_almost_equal(lr_cv.coef_, lr.coef_)


def test_logreg_predict_proba_multinomial():
Expand Down

0 comments on commit 03ac360

Please sign in to comment.