{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Support Vector Regression (SVR) using linear and non-linear kernels\n\nToy example of 1D regression using linear, polynomial and RBF kernels.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: The scikit-learn developers\n# SPDX-License-Identifier: BSD-3-Clause\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nfrom sklearn.svm import SVR" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate sample data\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "X = np.sort(5 * np.random.rand(40, 1), axis=0)\ny = np.sin(X).ravel()\n\n# add noise to targets\ny[::5] += 3 * (0.5 - np.random.rand(8))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fit regression model\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "svr_rbf = SVR(kernel=\"rbf\", C=100, gamma=0.1, epsilon=0.1)\nsvr_lin = SVR(kernel=\"linear\", C=100, gamma=\"auto\")\nsvr_poly = SVR(kernel=\"poly\", C=100, gamma=\"auto\", degree=3, epsilon=0.1, coef0=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Look at the results\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "lw = 2\n\nsvrs = [svr_rbf, svr_lin, svr_poly]\nkernel_label = [\"RBF\", \"Linear\", \"Polynomial\"]\nmodel_color = [\"m\", \"c\", \"g\"]\n\nfig, axes = plt.subplots(nrows=1, ncols=3, figsize=(15, 10), sharey=True)\nfor ix, svr in enumerate(svrs):\n axes[ix].plot(\n X,\n svr.fit(X, y).predict(X),\n color=model_color[ix],\n lw=lw,\n label=\"{} model\".format(kernel_label[ix]),\n )\n axes[ix].scatter(\n X[svr.support_],\n y[svr.support_],\n facecolor=\"none\",\n edgecolor=model_color[ix],\n s=50,\n label=\"{} support vectors\".format(kernel_label[ix]),\n )\n axes[ix].scatter(\n X[np.setdiff1d(np.arange(len(X)), svr.support_)],\n y[np.setdiff1d(np.arange(len(X)), svr.support_)],\n facecolor=\"none\",\n edgecolor=\"k\",\n s=50,\n label=\"other training data\",\n )\n axes[ix].legend(\n loc=\"upper center\",\n bbox_to_anchor=(0.5, 1.1),\n ncol=1,\n fancybox=True,\n shadow=True,\n )\n\nfig.text(0.5, 0.04, \"data\", ha=\"center\", va=\"center\")\nfig.text(0.06, 0.5, \"target\", ha=\"center\", va=\"center\", rotation=\"vertical\")\nfig.suptitle(\"Support Vector Regression\", fontsize=14)\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.21" } }, "nbformat": 4, "nbformat_minor": 0 }