.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/svm/plot_svm_regression.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 or to run this example in your browser via JupyterLite or Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_svm_plot_svm_regression.py: =================================================================== Support Vector Regression (SVR) using linear and non-linear kernels =================================================================== Toy example of 1D regression using linear, polynomial and RBF kernels. .. GENERATED FROM PYTHON SOURCE LINES 9-15 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from sklearn.svm import SVR .. GENERATED FROM PYTHON SOURCE LINES 16-18 Generate sample data -------------------- .. GENERATED FROM PYTHON SOURCE LINES 18-24 .. code-block:: Python X = np.sort(5 * np.random.rand(40, 1), axis=0) y = np.sin(X).ravel() # add noise to targets y[::5] += 3 * (0.5 - np.random.rand(8)) .. GENERATED FROM PYTHON SOURCE LINES 25-27 Fit regression model -------------------- .. GENERATED FROM PYTHON SOURCE LINES 27-31 .. code-block:: Python svr_rbf = SVR(kernel="rbf", C=100, gamma=0.1, epsilon=0.1) svr_lin = SVR(kernel="linear", C=100, gamma="auto") svr_poly = SVR(kernel="poly", C=100, gamma="auto", degree=3, epsilon=0.1, coef0=1) .. GENERATED FROM PYTHON SOURCE LINES 32-34 Look at the results ------------------- .. GENERATED FROM PYTHON SOURCE LINES 34-77 .. code-block:: Python lw = 2 svrs = [svr_rbf, svr_lin, svr_poly] kernel_label = ["RBF", "Linear", "Polynomial"] model_color = ["m", "c", "g"] fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 10), sharey=True) for ix, svr in enumerate(svrs): axes[ix].plot( X, svr.fit(X, y).predict(X), color=model_color[ix], lw=lw, label="{} model".format(kernel_label[ix]), ) axes[ix].scatter( X[svr.support_], y[svr.support_], facecolor="none", edgecolor=model_color[ix], s=50, label="{} support vectors".format(kernel_label[ix]), ) axes[ix].scatter( X[np.setdiff1d(np.arange(len(X)), svr.support_)], y[np.setdiff1d(np.arange(len(X)), svr.support_)], facecolor="none", edgecolor="k", s=50, label="other training data", ) axes[ix].legend( loc="upper center", bbox_to_anchor=(0.5, 1.1), ncol=1, fancybox=True, shadow=True, ) fig.text(0.5, 0.04, "data", ha="center", va="center") fig.text(0.06, 0.5, "target", ha="center", va="center", rotation="vertical") fig.suptitle("Support Vector Regression", fontsize=14) plt.show() .. image-sg:: /auto_examples/svm/images/sphx_glr_plot_svm_regression_001.png :alt: Support Vector Regression :srcset: /auto_examples/svm/images/sphx_glr_plot_svm_regression_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.366 seconds) .. _sphx_glr_download_auto_examples_svm_plot_svm_regression.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/main?urlpath=lab/tree/notebooks/auto_examples/svm/plot_svm_regression.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/?path=auto_examples/svm/plot_svm_regression.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_svm_regression.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_svm_regression.py ` .. include:: plot_svm_regression.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_