.. 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.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.py: Kernel ridge ============ This example demonstrates how to solve kernel ridge regression, using himalaya's estimator ``KernelRidge`` compatible with scikit-learn's API. .. GENERATED FROM PYTHON SOURCE LINES 10-12 Create a random dataset ----------------------- .. GENERATED FROM PYTHON SOURCE LINES 12-17 .. code-block:: default import numpy as np 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 18-22 Scikit-learn API ---------------- Himalaya implements a ``KernelRidge`` estimator, similar to the corresponding scikit-learn estimator, with similar parameters and methods. .. GENERATED FROM PYTHON SOURCE LINES 22-39 .. code-block:: default import sklearn.kernel_ridge import himalaya.kernel_ridge # Fit a scikit-learn model model_skl = sklearn.kernel_ridge.KernelRidge(kernel="linear", alpha=0.1) model_skl.fit(X, Y) # Fit a himalaya model model_him = himalaya.kernel_ridge.KernelRidge(kernel="linear", alpha=0.1) model_him.fit(X, Y) Y_pred_skl = model_skl.predict(X) Y_pred_him = model_him.predict(X) # The predictions are virtually identical. print(np.max(np.abs(Y_pred_skl - Y_pred_him))) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 7.105427357601002e-15 .. GENERATED FROM PYTHON SOURCE LINES 40-45 Small API difference -------------------- Since himalaya focuses on fitting multiple targets, the ``score`` method returns the score on each target separately, while scikit-learn returns the average score over targets. .. GENERATED FROM PYTHON SOURCE LINES 45-49 .. code-block:: default print(model_skl.score(X, Y)) print(model_him.score(X, Y)) print(model_him.score(X, Y).mean()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none 0.9999104439221304 [0.99990451 0.99985375 0.99994683 0.99993669] 0.9999104439221304 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.017 seconds) .. _sphx_glr_download__auto_examples_kernel_ridge_plot_kernel_ridge.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.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_kernel_ridge.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_