Ejemplo n.º 1
0
def test_convergence_warning():
    X, y = build_dataset(n_samples=10, n_features=10)
    tol = -1  # gap canot be negative, a covnergence warning should be raised
    alpha_max = np.max(np.abs(X.T.dot(y))) / X.shape[0]
    clf = Lasso(alpha_max / 10, max_iter=1, max_epochs=100, tol=tol)

    with warnings.catch_warnings(record=True) as w:
        # Cause all warnings to always be triggered.
        warnings.simplefilter("always")
        clf.fit(X, y)
        assert len(w) == 1
        assert issubclass(w[-1].category, ConvergenceWarning)
Ejemplo n.º 2
0
def test_dropin_lasso(sparse_X):
    """Test that our Lasso class behaves as sklearn's Lasso."""
    X, y, _, _ = build_dataset(n_samples=20, n_features=30, sparse_X=sparse_X)

    alpha_max = np.linalg.norm(X.T.dot(y), ord=np.inf) / X.shape[0]
    alpha = alpha_max / 2.
    clf = Lasso(alpha=alpha)
    clf.fit(X, y)

    clf2 = sklearn_Lasso(alpha=alpha)
    clf2.fit(X, y)
    np.testing.assert_allclose(clf.coef_, clf2.coef_, rtol=1e-5)

    check_estimator(Lasso)
Ejemplo n.º 3
0
def test_warm_start():
    """Test Lasso path convergence."""
    X, y = build_dataset(n_samples=100, n_features=100, sparse_X=True)
    n_samples, n_features = X.shape
    alpha_max = np.max(np.abs(X.T.dot(y))) / n_samples
    n_alphas = 10
    alphas = alpha_max * np.logspace(0, -2, n_alphas)

    reg1 = Lasso(tol=1e-6, warm_start=True, p0=10)
    reg1.coef_ = np.zeros(n_features)

    for alpha in alphas:
        reg1.set_params(alpha=alpha)
        reg1.fit(X, y)
        # refitting with warm start should take less than 2 iters:
        reg1.fit(X, y)
        # hack because assert_array_less does strict comparison...
        np.testing.assert_array_less(reg1.n_iter_, 2.01)
Ejemplo n.º 4
0
def test_dropin_lasso(sparse_X, fit_intercept):
    """Test that our Lasso class behaves as sklearn's Lasso."""
    X, y, _, _ = build_dataset(n_samples=20, n_features=30, sparse_X=sparse_X)

    alpha_max = np.linalg.norm(X.T.dot(y), ord=np.inf) / X.shape[0]
    alpha = alpha_max / 2.
    params = dict(alpha=alpha,
                  fit_intercept=fit_intercept,
                  tol=1e-10,
                  normalize=True)
    clf = Lasso(**params)
    clf.fit(X, y)

    clf2 = sklearn_Lasso(**params)
    clf2.fit(X, y)
    np.testing.assert_allclose(clf.coef_, clf2.coef_, rtol=1e-5)
    if fit_intercept:
        np.testing.assert_allclose(clf.intercept_, clf2.intercept_)

    check_estimator(Lasso)
Ejemplo n.º 5
0
def test_Lasso(sparse_X, fit_intercept, positive):
    """Test that our Lasso class behaves as sklearn's Lasso."""
    X, y = build_dataset(n_samples=20, n_features=30, sparse_X=sparse_X)
    if not positive:
        alpha_max = norm(X.T.dot(y), ord=np.inf) / X.shape[0]
    else:
        alpha_max = X.T.dot(y).max() / X.shape[0]

    alpha = alpha_max / 2.
    params = dict(alpha=alpha,
                  fit_intercept=fit_intercept,
                  tol=1e-10,
                  positive=positive)
    clf = Lasso(**params)
    clf.fit(X, y)

    clf2 = sklearn_Lasso(**params)
    clf2.fit(X, y)
    assert_allclose(clf.coef_, clf2.coef_, rtol=1e-5)
    if fit_intercept:
        assert_allclose(clf.intercept_, clf2.intercept_)