|
11 | 11 | from numpy.testing import assert_allclose, assert_array_less |
12 | 12 | import pytest |
13 | 13 |
|
14 | | -from sklearn.linear_model._logistic import _logistic_regression_path |
15 | 14 | from sklearn.exceptions import ConvergenceWarning |
16 | | -from sklearn.utils.estimator_checks import check_estimator |
17 | 15 | from sklearn.linear_model import (LassoCV as sklearn_LassoCV, |
18 | | - Lasso as sklearn_Lasso, lasso_path, |
19 | | - LogisticRegression as sklearn_Logreg) |
| 16 | + Lasso as sklearn_Lasso, lasso_path) |
20 | 17 |
|
21 | 18 | from celer import celer_path |
22 | | -from celer.dropin_sklearn import Lasso, LassoCV, LogisticRegression |
| 19 | +from celer.dropin_sklearn import Lasso, LassoCV |
23 | 20 | from celer.utils.testing import build_dataset |
24 | 21 |
|
25 | 22 |
|
26 | | -@pytest.mark.parametrize("solver", ["celer", "celer-pn"]) |
27 | | -def test_celer_path_logreg(solver): |
28 | | - X, y = build_dataset( |
29 | | - n_samples=60, n_features=100, sparse_X=True) |
30 | | - y = np.sign(y) |
31 | | - alpha_max = norm(X.T.dot(y), ord=np.inf) / 2 |
32 | | - alphas = alpha_max * np.geomspace(1, 1e-2, 10) |
33 | | - |
34 | | - tol = 1e-11 |
35 | | - coefs, Cs, n_iters = _logistic_regression_path( |
36 | | - X, y, Cs=1. / alphas, fit_intercept=False, penalty='l1', |
37 | | - solver='liblinear', tol=tol) |
38 | | - |
39 | | - _, coefs_c, gaps = celer_path( |
40 | | - X, y, "logreg", alphas=alphas, tol=tol, verbose=0, |
41 | | - use_PN=(solver == "celer-pn")) |
42 | | - |
43 | | - assert_array_less(gaps, tol * len(y) * np.log(2)) |
44 | | - assert_allclose(coefs != 0, coefs_c.T != 0) |
45 | | - assert_allclose(coefs, coefs_c.T, atol=1e-5, rtol=1e-3) |
46 | | - |
47 | | - |
48 | | -@pytest.mark.parametrize("sparse_X", [True, False]) |
49 | | -def test_LogisticRegression(sparse_X): |
50 | | - np.random.seed(1409) |
51 | | - X, y = build_dataset( |
52 | | - n_samples=30, n_features=60, sparse_X=sparse_X) |
53 | | - y = np.sign(y) |
54 | | - alpha_max = norm(X.T.dot(y), ord=np.inf) / 2 |
55 | | - C = 20. / alpha_max |
56 | | - |
57 | | - tol = 1e-8 |
58 | | - clf1 = LogisticRegression(C=C, tol=tol, verbose=0) |
59 | | - clf1.fit(X, y) |
60 | | - |
61 | | - clf2 = sklearn_Logreg( |
62 | | - C=C, penalty='l1', solver='liblinear', fit_intercept=False, tol=tol) |
63 | | - clf2.fit(X, y) |
64 | | - assert_allclose(clf1.coef_, clf2.coef_, rtol=1e-3, atol=1e-5) |
65 | | - |
66 | | - # this uses float32 so we increase the tol else there are precision issues |
67 | | - clf1.tol = 1e-4 |
68 | | - check_estimator(clf1) |
69 | | - |
70 | | - # multinomial test, need to have a slightly lower tol |
71 | | - # for results to be comparable |
72 | | - y = np.random.choice(4, len(y)) |
73 | | - clf3 = LogisticRegression(C=C, tol=tol, verbose=0) |
74 | | - clf3.fit(X, y) |
75 | | - |
76 | | - clf4 = sklearn_Logreg( |
77 | | - C=C, penalty='l1', solver='liblinear', fit_intercept=False, tol=tol) |
78 | | - clf4.fit(X, y) |
79 | | - assert_allclose(clf3.coef_, clf4.coef_, rtol=1e-3, atol=1e-3) |
80 | | - |
81 | | - clf3.tol = 1e-3 |
82 | | - check_estimator(clf3) |
83 | | - |
84 | | - |
85 | 23 | @pytest.mark.parametrize("sparse_X, alphas, pb", |
86 | 24 | product([False, True], [None, 1], |
87 | 25 | ["lasso", "logreg"])) |
@@ -118,7 +56,7 @@ def test_convergence_warning(): |
118 | 56 | # Cause all warnings to always be triggered. |
119 | 57 | warnings.simplefilter("always") |
120 | 58 | clf.fit(X, y) |
121 | | - assert len(w) == 1 |
| 59 | + assert len(w) >= 1 |
122 | 60 | assert issubclass(w[-1].category, ConvergenceWarning) |
123 | 61 |
|
124 | 62 |
|
|
0 commit comments