Beispiel #1
0
def test_multitasklasso(gaussian_data, fit_intercept, normalize, alpha):

    X, y = gaussian_data
    X = [X[0], X[0]]
    n_samples = y.shape[1]

    Xty = np.array([xx.T.dot(yy) for xx, yy in zip(X, y)])
    alpha_max = np.linalg.norm(Xty, axis=0).max()
    alpha *= alpha_max / n_samples
    est = GroupLasso(alpha=alpha,
                     fit_intercept=fit_intercept,
                     normalize=normalize)
    est.fit(X, y)
    assert hasattr(est, 'is_fitted_')

    mtlasso = MultiTaskLasso(alpha=alpha,
                             fit_intercept=fit_intercept,
                             normalize=normalize)
    mtlasso.fit(X[0], y.T)
    assert_allclose(est.coef_, mtlasso.coef_.T, rtol=1e-2)
Beispiel #2
0
def test_grouplasso(gaussian_data, fit_intercept, normalize, alpha, positive):

    X, y = gaussian_data
    n_samples = y.shape[1]

    Xty = np.array([xx.T.dot(yy) for xx, yy in zip(X, y)])
    alpha_max = np.linalg.norm(Xty, axis=0).max()
    alpha *= alpha_max / n_samples
    est = GroupLasso(alpha=alpha,
                     fit_intercept=fit_intercept,
                     normalize=normalize,
                     positive=positive)
    est.fit(X, y)
    assert hasattr(est, 'is_fitted_')
    assert_allclose(est.coef_specific_, 0.)
    if positive:
        assert est.coef_.min() >= 0.
    var_y = np.var(y, axis=1)
    r2 = 1 - (est.residuals_**2).mean(1) / var_y
    scores = est.score(X, y)
    assert_allclose(r2, scores)
Beispiel #3
0
def test_zero_alpha(gaussian_data):
    X, y = gaussian_data
    est0 = IndRewLasso(alpha=len(X) * [0.])
    est1 = IndLasso(alpha=len(X) * [0.])
    est2 = GroupLasso(alpha=0.)
    est3 = DirtyModel(alpha=0., beta=0.)
    est4 = MultiLevelLasso(alpha=0.)
    est5 = ReMTW(alpha=0., beta=0.)
    est6 = MTW(alpha=0., beta=0.)

    models = [est0, est1, est2, est3, est4, est5, est6]
    for model in models:
        with pytest.warns(UserWarning, match='LinearRegression'):
            model.fit(X, y)
Beispiel #4
0
def test_slow_convergence(gaussian_data):
    X, y = gaussian_data
    max_iter = 1
    est0 = IndRewLasso(alpha=len(X) * [0.01], max_iter=max_iter)
    est1 = IndLasso(alpha=len(X) * [0.01], max_iter=max_iter)
    est2 = GroupLasso(alpha=0.01, max_iter=max_iter)
    est3 = DirtyModel(alpha=0.01, beta=0.01, max_iter=max_iter)
    est4 = MultiLevelLasso(alpha=0.01, max_iter=max_iter)
    est5 = ReMTW(alpha=0.01, beta=0.01, max_iter=max_iter)
    est6 = MTW(alpha=0.01, beta=0.01, max_iter=max_iter)

    models = [est0, est1, est2, est3, est4, est5, est6]
    for model in models:
        with pytest.warns(ConvergenceWarning, match='number of iterations'):
            model.fit(X, y)
Beispiel #5
0
def test_set_params_gl(gaussian_data):

    X, y = gaussian_data
    n_samples = y.shape[1]

    Xty = np.array([xx.T.dot(yy) for xx, yy in zip(X, y)])
    alpha_max = np.linalg.norm(Xty, axis=0).max()
    alpha1 = 0.05 * alpha_max / n_samples
    alpha2 = 0.6 * alpha_max / n_samples

    coef0 = GroupLasso(alpha=alpha1).fit(X, y).coef_

    est = GroupLasso(alpha=alpha2)
    est.fit(X, y)
    est.set_params(alpha=alpha1)
    coef1 = est.fit(X, y).coef_

    assert_allclose(coef0, coef1, 1e-4)
# add noise
y += 0.2 * np.std(y) + rng.randn(n_tasks, n_samples)

##########################################################
# Lasso fit

alpha = 0.5 * np.ones(n_tasks)
lasso = IndLasso(alpha=alpha)
lasso.fit(X, y)

##########################################################
# Group Lasso fit

alpha = 1.75
gl = GroupLasso(alpha=alpha)
gl.fit(X, y)

##########################################################
# Dirty models fit

alpha = 0.8
beta = 0.4
dirty = DirtyModel(alpha=alpha, beta=beta)
dirty.fit(X, y)

##########################################################
# Multilevel Lasso fit

alpha = 0.25
mll = MultiLevelLasso(alpha=alpha)