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)
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)
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)
# 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) mll.fit(X, y)