.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "packages/scikit-learn/auto_examples/plot_linear_model_cv.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` 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 ================================================================ .. GENERATED FROM PYTHON SOURCE LINES 10-11 Load the diabetes dataset .. GENERATED FROM PYTHON SOURCE LINES 11-17 .. 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 .. code-block:: none (442, 10) .. GENERATED FROM PYTHON SOURCE LINES 18-19 Compute the cross-validation score with the default hyper-parameters .. GENERATED FROM PYTHON SOURCE LINES 19-26 .. 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(f"{Model.__name__}: {cross_val_score(model, X, y).mean()}") .. rst-class:: sphx-glr-script-out .. code-block:: none Ridge: 0.410174971340889 Lasso: 0.3375593674654274 .. GENERATED FROM PYTHON SOURCE LINES 27-29 We compute the cross-validation score as a function of alpha, the strength of the regularization for Lasso and Ridge .. GENERATED FROM PYTHON SOURCE LINES 29-45 .. code-block:: Python import numpy as np import matplotlib.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-sg:: /packages/scikit-learn/auto_examples/images/sphx_glr_plot_linear_model_cv_001.png :alt: plot linear model cv :srcset: /packages/scikit-learn/auto_examples/images/sphx_glr_plot_linear_model_cv_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.420 seconds) .. _sphx_glr_download_packages_scikit-learn_auto_examples_plot_linear_model_cv.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_linear_model_cv.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_linear_model_cv.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_linear_model_cv.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_