.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_auto_examples/kernel_ridge/plot_kernel_ridge_cv.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_kernel_ridge_plot_kernel_ridge_cv.py: Kernel ridge with cross-validation ================================== This example demonstrates how to solve kernel ridge regression with a cross-validation of the regularization parameter, using himalaya's estimator ``KernelRidgeCV``. .. GENERATED FROM PYTHON SOURCE LINES 11-13 Create a random dataset ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 13-19 .. code-block:: default import numpy as np np.random.seed(0) n_samples, n_features, n_targets = 10, 20, 4 X = np.random.randn(n_samples, n_features) Y = np.random.randn(n_samples, n_targets) .. GENERATED FROM PYTHON SOURCE LINES 20-24 Limit of GridSearchCV --------------------- In scikit-learn, one can use ``GridSearchCV`` to optimize hyperparameters over cross-validation. .. GENERATED FROM PYTHON SOURCE LINES 24-35 .. code-block:: default import sklearn.model_selection import sklearn.kernel_ridge estimator = sklearn.kernel_ridge.KernelRidge(kernel="linear") gscv = sklearn.model_selection.GridSearchCV( estimator=estimator, param_grid=dict(alpha=np.logspace(-2, 2, 5)), ) gscv.fit(X, Y) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none GridSearchCV(estimator=KernelRidge(), param_grid={'alpha': array([1.e-02, 1.e-01, 1.e+00, 1.e+01, 1.e+02])}) .. GENERATED FROM PYTHON SOURCE LINES 36-38 However, since ``GridSearchCV`` optimizes the average score over all targets, it returns a single value for alpha. .. GENERATED FROM PYTHON SOURCE LINES 38-40 .. code-block:: default gscv.best_params_ .. rst-class:: sphx-glr-script-out Out: .. code-block:: none {'alpha': 100.0} .. GENERATED FROM PYTHON SOURCE LINES 41-45 KernelRidgeCV ------------- To optimize each target independently, himalaya implements ``KernelRidgeCV``, which supports any cross-validation scheme compatible with scikit-learn. .. GENERATED FROM PYTHON SOURCE LINES 45-51 .. code-block:: default import himalaya.kernel_ridge model = himalaya.kernel_ridge.KernelRidgeCV(kernel="linear", alphas=np.logspace(-2, 2, 5)) model.fit(X, Y) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none KernelRidgeCV(alphas=array([1.e-02, 1.e-01, 1.e+00, 1.e+01, 1.e+02])) .. GENERATED FROM PYTHON SOURCE LINES 52-53 KernelRidgeCV returns a separate best alpha per target. .. GENERATED FROM PYTHON SOURCE LINES 53-54 .. code-block:: default model.best_alphas_ .. rst-class:: sphx-glr-script-out Out: .. code-block:: none array([1.e+00, 1.e-02, 1.e+02, 1.e+02]) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.025 seconds) .. _sphx_glr_download__auto_examples_kernel_ridge_plot_kernel_ridge_cv.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_kernel_ridge_cv.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_kernel_ridge_cv.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_