.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_existence_stopping.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_existence_stopping.py: =============================== Show existence of stopping time =============================== This is for L1 regression. .. GENERATED FROM PYTHON SOURCE LINES 8-84 .. image-sg:: /auto_examples/images/sphx_glr_plot_existence_stopping_001.png :alt: plot existence stopping :srcset: /auto_examples/images/sphx_glr_plot_existence_stopping_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out Out: .. code-block:: none usetex mode requires TeX. /home/circleci/.local/lib/python3.8/site-packages/sklearn/linear_model/_base.py:133: FutureWarning: The default of 'normalize' will be set to False in version 1.2 and deprecated in version 1.4. If you wish to scale the data, use Pipeline with a StandardScaler in a preprocessing stage. To reproduce the previous behavior: from sklearn.pipeline import make_pipeline model = make_pipeline(StandardScaler(with_mean=False), LassoLars()) If you wish to pass a sample_weight parameter, you need to pass it as a fit parameter to each step of the pipeline as follows: kwargs = {s[0] + '__sample_weight': sample_weight for s in model.steps} model.fit(X, y, **kwargs) Set parameter alpha to: original_alpha * np.sqrt(n_samples). warnings.warn( 0.1 1.7000000000000002 3.3000000000000003 4.9 6.5 | .. code-block:: default import numpy as np import matplotlib.pyplot as plt from numpy.linalg import norm from sklearn.linear_model import LassoLars from joblib import Parallel, delayed from iterreg.sparse import dual_primal from iterreg.utils import make_sparse_data from celer.plot_utils import configure_plt configure_plt() n, d = 200, 500 s = 75 rho = 0.2 np.random.seed(12) X, y, w0 = make_sparse_data( n, d, s=s, rho=rho, snr=None, w_type="ones", normalize=True) scal = 20 / norm(y) y *= scal clf = LassoLars(alpha=1e-20, fit_intercept=False) clf.fit(X, y) w_bp = clf.coef_ def wrapper(X, y, delta, max_iter, f_store, rep): print(delta, rep) np.random.seed(rep) noise = np.random.randn(y.shape[0]) y_noise = y + delta * noise / norm(noise) all_w = dual_primal( X, y_noise, step_ratio=1, max_iter=max_iter, f_store=f_store, verbose=False)[-1] return all_w all_wdict = dict() max_iter = 5_000 n_deltas = 5 deltas = np.linspace(0.1, 6.5, num=n_deltas) np.random.seed(1) n_reps = 1 res = np.zeros([n_deltas, n_reps]) for i, delta in enumerate(deltas): print(delta) all_w_rep = np.array(Parallel(n_jobs=-1)(delayed(wrapper)( X, y, delta, max_iter, 1, rep) for rep in range(n_reps))) all_wdict[delta] = all_w_rep[:1].copy() for rep in range(n_reps): res[i, rep] = np.argmin(norm(all_w_rep[rep] - w_bp, axis=1)) del all_w_rep fig3, ax = plt.subplots(constrained_layout=True, figsize=(9, 4)) n_points = 400 for delta in deltas: x_plt = np.arange(len(all_wdict[delta][0])) y_plt = norm(all_wdict[delta][0] - w_bp, axis=1) / norm(w_bp) ax.semilogy(x_plt[:n_points], y_plt[:n_points], label=r"$\delta={:.2f}$".format(delta)) ax.set_ylabel(r'$||w^\delta_k - w^\star||/ ||w^\star||$') ax.set_xlabel("Iteration $k$") plt.legend(ncol=3, fontsize=16, loc='upper right') plt.show(block=False) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 18.089 seconds) .. _sphx_glr_download_auto_examples_plot_existence_stopping.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_existence_stopping.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_existence_stopping.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_