Skip to content

Commit

Permalink
Better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mathurinm committed Apr 13, 2018
1 parent d48550b commit 74e7664
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions celer/tests/test_dense.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def build_dataset(n_samples=50, n_features=200, n_informative_features=10,
n_targets=1):
n_targets=1, sparse_X=False):
"""
build an ill-posed linear regression problem with many noisy features and
comparatively few samples
Expand All @@ -17,34 +17,44 @@ def build_dataset(n_samples=50, n_features=200, n_informative_features=10,
else:
w = random_state.randn(n_features)
w[n_informative_features:] = 0.0
X = random_state.randn(n_samples, n_features)
y = np.dot(X, w)
X_test = random_state.randn(n_samples, n_features)
y_test = np.dot(X_test, w)
if sparse_X:
X = sparse.random(n_samples, n_features, density=0.5, format='csc',
random_state=random_state)
X_test = sparse.random(n_samples, n_features, density=0.5,
format='csc', random_state=random_state)
else:
X = random_state.randn(n_samples, n_features)
X_test = random_state.randn(n_samples, n_features)
y = X.dot(w)
y_test = X_test.dot(w)
return X, y, X_test, y_test


def test_celer_path_dense():

"""Test Lasso path computation on dense data."""
X, y, _, _ = build_dataset(n_samples=50, n_features=50, n_targets=1)

alpha_max = np.max(np.abs(X.T.dot(y)))
n_alphas = 10
alphas = alpha_max * np.logspace(0, -2, n_alphas)

tol = 1e-6
betas, thetas, gaps = celer_path(X, y, alphas=alphas, tol=tol, verbose=1)
alphas, coefs, gaps, thetas = celer_path(X, y, alphas=alphas, tol=tol,
return_thetas=True)
betas = coefs.T
np.testing.assert_array_less(gaps, tol)


def test_celer_path_sparse():

X, y, _, _ = build_dataset(n_samples=50, n_features=50, n_targets=1)
X = sparse.csc_matrix(X)
"""Test Lasso path computation on sparse data."""
X, y, _, _ = build_dataset(n_samples=50, n_features=50, n_targets=1,
sparse_X=True)
alpha_max = np.max(np.abs(X.T.dot(y)))
n_alphas = 10
alphas = alpha_max * np.logspace(0, -2, n_alphas)

tol = 1e-6
betas, thetas, gaps = celer_path(X, y, alphas=alphas, tol=tol, verbose=1)
alphas, coefs, gaps, thetas = celer_path(X, y, alphas=alphas, tol=tol,
return_thetas=True)
betas = coefs.T
np.testing.assert_array_less(gaps, tol)

0 comments on commit 74e7664

Please sign in to comment.