Skip to content

Commit

Permalink
TST add unit testing for inf weights (#234)
Browse files Browse the repository at this point in the history
Co-authored-by: mathurinm <[email protected]>
  • Loading branch information
Badr-MOUFAD and mathurinm authored Apr 19, 2022
1 parent 64aed96 commit 0ed7282
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
29 changes: 28 additions & 1 deletion celer/tests/test_lasso.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import numpy as np
from numpy.linalg import norm
from numpy.testing import assert_allclose, assert_array_less
from numpy.testing import assert_allclose, assert_array_less, assert_array_equal
import pytest

from sklearn.exceptions import ConvergenceWarning
Expand Down Expand Up @@ -218,6 +218,33 @@ def test_weights_lasso():
np.testing.assert_raises(ValueError, clf1.fit, X=X, y=y)


@pytest.mark.parametrize("pb", ["lasso", "logreg"])
def test_infinite_weights(pb):
n_samples, n_features = 50, 100
X, y = build_dataset(n_samples, n_features)
if pb == "logreg":
y = np.sign(y)

np.random.seed(1)
weights = np.abs(np.random.randn(n_features))
n_inf = n_features // 10
inf_indices = np.random.choice(n_features, size=n_inf, replace=False)
weights[inf_indices] = np.inf

alpha = norm(X.T @ y / weights, ord=np.inf) / n_samples / 100

tol = 1e-8
_, coefs, dual_gaps = celer_path(
X, y, pb=pb, alphas=[alpha], weights=weights, tol=tol)

if pb == "logreg":
assert_array_less(dual_gaps[0], tol * n_samples * np.log(2))
else:
assert_array_less(dual_gaps[0], tol * norm(y) ** 2 / 2.)

assert_array_equal(coefs[inf_indices], 0)


def test_zero_iter():
X, y = build_dataset(n_samples=30, n_features=50)

Expand Down
3 changes: 1 addition & 2 deletions celer/tests/test_logreg.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Author: Mathurin Massias <[email protected]>
# License: BSD 3 clause

import pytest
import numpy as np
from numpy.linalg import norm

from numpy.testing import assert_allclose, assert_array_less
from numpy.testing import assert_allclose, assert_array_less, assert_array_equal
from sklearn.linear_model._logistic import _logistic_regression_path
from sklearn.utils.estimator_checks import check_estimator
from sklearn.linear_model import LogisticRegression as sklearn_Logreg
Expand Down
28 changes: 27 additions & 1 deletion celer/tests/test_mtl.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import pytest
import warnings
import itertools

import numpy as np
from numpy.linalg import norm
from numpy.testing import assert_allclose, assert_array_less
from numpy.testing import assert_allclose, assert_array_less, assert_array_equal

from sklearn.utils.estimator_checks import check_estimator
from sklearn.linear_model import MultiTaskLassoCV as sklearn_MultiTaskLassoCV
Expand Down Expand Up @@ -241,5 +243,29 @@ def test_check_weights():
np.testing.assert_raises(ValueError, clf.fit, X=X, y=y)


def test_infinite_weights_group():
n_samples, n_features = 50, 100
X, y = build_dataset(n_samples, n_features)

np.random.seed(1)
group_size = 5
weights = np.abs(np.random.randn(n_features // group_size))
n_inf = 3
inf_indices = np.random.choice(
n_features // group_size, size=n_inf, replace=False)
weights[inf_indices] = np.inf
alpha_max = np.max(
norm((X.T @ y).reshape(-1, group_size), 2, axis=1)
) / n_samples

clf = GroupLasso(
alpha=alpha_max / 100., weights=weights, groups=group_size, tol=1e-8
).fit(X, y)

assert_array_less(clf.dual_gap_, clf.tol * norm(y) ** 2 / 2)
assert_array_equal(
norm(clf.coef_.reshape(-1, group_size), axis=1)[inf_indices], 0)


if __name__ == "__main__":
pass

0 comments on commit 0ed7282

Please sign in to comment.