def test_weights_group_lasso(): n_samples, n_features = 30, 50 X, y = build_dataset(n_samples, n_features, sparse_X=True) groups = 5 n_groups = n_features // groups np.random.seed(0) weights = np.abs(np.random.randn(n_groups)) tol = 1e-14 params = {'n_alphas': 10, 'tol': tol, 'verbose': 1} augmented_weights = np.repeat(weights, groups) alphas1, coefs1, gaps1 = celer_path( X, y, "grouplasso", groups=groups, weights=weights, eps=1e-2, **params) alphas2, coefs2, gaps2 = celer_path( X.multiply(1 / augmented_weights[None, :]), y, "grouplasso", groups=groups, eps=1e-2, **params) assert_allclose(alphas1, alphas2) assert_allclose( coefs1, coefs2 / augmented_weights[:, None], rtol=1e-3) assert_array_less(gaps1, tol * norm(y) ** 2 / len(y)) assert_array_less(gaps2, tol * norm(y) ** 2 / len(y))
def test_group_lasso_path(sparse_X): n_features = 50 X, y = build_dataset( n_samples=11, n_features=n_features, sparse_X=sparse_X) alphas, coefs, gaps = celer_path( X, y, "grouplasso", groups=5, eps=1e-2, n_alphas=10, tol=1e-8) tol = 1e-8 np.testing.assert_array_less(gaps, tol)
def plot_varying_sigma(corr, density, snr, max_iter=100): np.random.seed(0) # true coefficient vector has entries equal to 0 or 1 supp = np.random.choice(n_features, size=int(density * n_features), replace=False) w_true = np.zeros(n_features) w_true[supp] = 1 X_, y_, w_true = make_correlated_data( n_samples=int(n_samples * 4 / 3.), n_features=n_features, w_true=w_true, corr=corr, snr=snr, random_state=0) X, X_test, y, y_test = train_test_split(X_, y_, test_size=0.25) print('Starting computation for this setting') ratio = 10 * datadriven_ratio(X, y) _, _, _, all_w = dual_primal( X, y, step_ratio=ratio, rho=0.99, ret_all=True, max_iter=max_iter, f_store=1) fig, axarr = plt.subplots(2, 2, sharey='row', sharex='col', figsize=(4.2, 3.5), constrained_layout=True) scores = [f1_score(w != 0, w_true != 0) for w in all_w] mses = np.array([mean_squared_error(y_test, X_test @ w) for w in all_w]) axarr[0, 0].plot(scores) axarr[1, 0].plot(mses / np.mean(y_test ** 2)) axarr[0, 0].set_ylim(0, 1) axarr[0, 0].set_ylabel('F1 score') axarr[1, 0].set_ylabel("pred MSE left out") axarr[-1, 0].set_xlabel("CP iteration") axarr[0, 0].set_title('Iterative regularization') # last column: Lasso results alphas = norm(X.T @ y, ord=np.inf) / len(y) * np.geomspace(1, 1e-3) coefs = celer_path(X, y, 'lasso', alphas=alphas)[1].T axarr[0, 1].semilogx( alphas, [f1_score(coef != 0, w_true != 0) for coef in coefs]) axarr[1, 1].semilogx( alphas, np.array([mean_squared_error(y_test, X_test @ coef) for coef in coefs]) / np.mean(y_test ** 2)) axarr[-1, 1].set_xlabel(r'$\lambda$') axarr[0, 1].set_title("Lasso path") axarr[0, 1].invert_xaxis() plt.show(block=False) return fig
def test_group_lasso_path(sparse_X): n_features = 50 X, y = build_dataset(n_samples=11, n_features=n_features, sparse_X=sparse_X, n_informative_features=n_features)[:2] alphas, coefs, gaps = celer_path(X, y, "grouplasso", groups=5, eps=1e-2, n_alphas=10, tol=1e-8) tol = 1e-8 np.testing.assert_array_less(gaps, tol) check_estimator(GroupLasso)
def plot_varying_sigma(corr, density, snr, steps, max_iter=100, rho=0.99): np.random.seed(0) # true coefficient vector has entries equal to 0 or 1 supp = np.random.choice(n_features, size=int(density * n_features), replace=False) w_true = np.zeros(n_features) w_true[supp] = 1 X_, y_, w_true = make_correlated_data(n_samples=int(n_samples * 4 / 3.), n_features=n_features, w_true=w_true, corr=corr, snr=snr, random_state=0) X, X_test, y, y_test = train_test_split(X_, y_, test_size=0.25) print('Starting computation for this setting') fig, axarr = plt.subplots(4, 2, sharey='row', sharex='col', figsize=(7, 5), constrained_layout=True) fig.suptitle(r"Correlation=%.1f, $||w^*||_0$= %s, snr=%s" % (corr, (w_true != 0).sum(), snr)) for i, step in enumerate(steps): _, _, _, all_w = dual_primal(X, y, step=step, rho=rho, ret_all=True, max_iter=max_iter, f_store=1) scores = [f1_score(w != 0, w_true != 0) for w in all_w] supp_size = np.sum(all_w != 0, axis=1) mses = [mean_squared_error(y_test, X_test @ w) for w in all_w] axarr[0, 0].plot(scores, label=r"$\sigma=1 /%d ||X||$" % step) axarr[1, 0].semilogy(supp_size) axarr[2, 0].plot(norm(all_w - w_true, axis=1)) axarr[3, 0].plot(mses) axarr[0, 0].set_ylim(0, 1) axarr[0, 0].set_ylabel('F1 score for support') axarr[1, 0].set_ylabel(r"$||w_k||_0$") axarr[2, 0].set_ylabel(r'$\Vert w_k - w^*\Vert$') axarr[2, 0].set_xlabel("CP iteration") axarr[3, 0].set_ylabel("pred MSE left out") axarr[0, 0].legend(loc='lower right', fontsize=10) axarr[0, 0].set_title('Iterative regularization') # last column: Lasso results alphas = norm(X.T @ y, ord=np.inf) / len(y) * np.geomspace(1, 1e-3) coefs = celer_path(X, y, 'lasso', alphas=alphas)[1].T axarr[0, 1].semilogx(alphas, [f1_score(coef != 0, w_true != 0) for coef in coefs]) axarr[1, 1].semilogx(alphas, [np.sum(coef != 0) for coef in coefs]) axarr[2, 1].semilogx(alphas, [norm(coef - w_true) for coef in coefs]) axarr[3, 1].semilogx( alphas, [mean_squared_error(y_test, X_test @ coef) for coef in coefs]) axarr[3, 1].set_xlabel(r'$\lambda$') axarr[0, 1].set_title("Lasso path") for i in range(3): axarr[i, 1].set_xlim(*axarr[i, 1].get_xlim()[::-1]) plt.show(block=False)