Esempio n. 1
0
def test_dirty(gaussian_data, fit_intercept, normalize, alpha, beta, 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()
    beta_max = abs(Xty).max()
    alpha *= alpha_max / n_samples
    beta *= beta_max / n_samples
    est = DirtyModel(alpha=alpha,
                     beta=beta,
                     fit_intercept=fit_intercept,
                     normalize=normalize,
                     positive=positive)
    est.fit(X, y)
    assert hasattr(est, 'is_fitted_')
    if est.alpha <= est.beta:
        assert_allclose(est.coef_specific_, 0.)
    elif est.alpha > len(X)**0.5 * est.beta:
        assert_allclose(est.coef_shared_, 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)
Esempio n. 2
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)
Esempio n. 3
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)
Esempio n. 4
0
xty = np.array([xx.T.dot(yy) for xx, yy in zip(X, y)])
beta_max = abs(xty).max() / n_samples
alpha_max = np.linalg.norm(xty, axis=0).max() / n_samples
alphas = np.linspace(0.02, 1.1, 40) * alpha_max
betas = np.linspace(0.02, 1.1, 40) * beta_max

alphas_mesh, betas_mesh = np.meshgrid(alphas, alphas)
alphas_mesh = alphas_mesh.flatten()
betas_mesh = betas_mesh.flatten()

###########################################################
# For each (alpha, beta) we check if W_1 = 0 or W_2 = 0

type_of_model = []
for alpha, beta in zip(alphas_mesh, betas_mesh):
    dirty = DirtyModel(alpha=alpha, beta=beta)
    dirty.fit(X, y)
    W1 = abs(dirty.coef_shared_).max()
    W2 = abs(dirty.coef_specific_).max()
    if W1 and not W2:
        type_of_model.append(0)
    elif W2 and not W1:
        type_of_model.append(1)
    elif W1 and W2:
        type_of_model.append(2)
    else:
        type_of_model.append(3)
type_of_model = np.array(type_of_model)

###################################################
# Plot nature of model depending on hyperparameters
Esempio n. 5
0
def test_warmstart(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.6 * alpha_max / n_samples
    alpha2 = 0.05 * alpha_max / n_samples

    est = DirtyModel(alpha=alpha1, beta=alpha1, warm_start=True, tol=1e-5)
    est.fit(X, y)
    est.alpha = alpha2
    est.fit(X, y)
    assert hasattr(est, 'is_fitted_')
    assert_allclose(est.coef_specific_, 0.)
    coef1 = est.coef_.copy()

    est = DirtyModel(alpha=alpha2, beta=alpha1, tol=1e-5)
    est.fit(X, y)
    coef2 = est.coef_

    assert_allclose(coef1, coef2, 1e-4)