.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_packages_scikit-learn_auto_examples_plot_linear_model_cv.py: ================================================================ Use the RidgeCV and LassoCV to set the regularization parameter ================================================================ Load the diabetes dataset .. code-block:: python from sklearn.datasets import load_diabetes data = load_diabetes() X, y = data.data, data.target print(X.shape) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none (442, 10) Compute the cross-validation score with the default hyper-parameters .. code-block:: python from sklearn.model_selection import cross_val_score from sklearn.linear_model import Ridge, Lasso for Model in [Ridge, Lasso]: model = Model() print('%s: %s' % (Model.__name__, cross_val_score(model, X, y).mean())) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Ridge: 0.4101758336587286 Lasso: 0.3375597834274947 We compute the cross-validation score as a function of alpha, the strength of the regularization for Lasso and Ridge .. code-block:: python import numpy as np from matplotlib import pyplot as plt alphas = np.logspace(-3, -1, 30) plt.figure(figsize=(5, 3)) for Model in [Lasso, Ridge]: scores = [cross_val_score(Model(alpha), X, y, cv=3).mean() for alpha in alphas] plt.plot(alphas, scores, label=Model.__name__) plt.legend(loc='lower left') plt.xlabel('alpha') plt.ylabel('cross validation score') plt.tight_layout() plt.show() .. image:: /packages/scikit-learn/auto_examples/images/sphx_glr_plot_linear_model_cv_001.png :class: sphx-glr-single-img **Total running time of the script:** ( 0 minutes 0.355 seconds) .. _sphx_glr_download_packages_scikit-learn_auto_examples_plot_linear_model_cv.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: plot_linear_model_cv.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: plot_linear_model_cv.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_