data-science-ipython-notebooks/scikit-learn/scikit-learn-svm.ipynb

375 lines
260 KiB
Python
Raw Normal View History

2015-04-19 08:26:01 +08:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# scikit-learn-svm"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-05-31 21:46:59 +08:00
"Credits: Forked from [PyCon 2015 Scikit-learn Tutorial](https://github.com/jakevdp/sklearn_pycon2015) by Jake VanderPlas\n",
"\n",
2015-04-19 08:26:01 +08:00
"* Support Vector Machine Classifier\n",
"* Support Vector Machine with Kernels Classifier"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import seaborn; \n",
"from sklearn.linear_model import LinearRegression\n",
"from scipy import stats\n",
"import pylab as pl\n",
"\n",
"seaborn.set()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Support Vector Machine Classifier"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Support Vector Machines (SVMs) are a powerful supervised learning algorithm used for **classification** or for **regression**. SVMs draw a boundary between clusters of data. SVMs attempt to maximize the margin between sets of points. Many lines can be drawn to separate the points above:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAeMAAAFVCAYAAADc5IdQAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsvXd8G3d65/9BB9g7RVGFokSBRSySrGarN1uy1UnZluS6\n2b1k95JNsr/NZu+VzS+X7OVyv73b3OWSbdmsd23JtkiqWpKtZku2ZEsukkiKBaQoUZ29omMw8/tj\ngAFAgkQRMADJ5/168UXgO8DMdwhwPvOU7/NIOI4DQRAEQRCRQxrpCRAEQRDEZIfEmCAIgiAiDIkx\nQRAEQUQYEmOCIAiCiDAkx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Fjv8/2f/M0ZvPnh6yg0FQAA\nsu3ZeOPqa7j+wy8Dmsenv/kEX6y5iOkbMmBbbsCHLx3H/eZ7Ae2DIIjIQGJMRAVdJ9sxxzpnxHgc\n4sB9Ft1Z7eY2ExLhvWKbslMx5nstFgtSLyZBhpGW/9Kri6G70eTXHL489Dnm/3QedjXvQC5yscj0\nFF79eC/qvvcNbDabX/sgCCJykBgTUYGEGT0+Ota2aCAmLw7dkm6v2yzZ1jHfq9frkTKQ4nXbdMt0\ndLV1+DWHweoB5JpH5jlsr9mGL6ou+bUPgiAiB4kxERWon4lFl6RrxDgDBrYyewRm5D9Ltz+N44s+\nGDHeHNOCpArvQuskOTkZD2c/8rrtSsZVFDwzz685KNu9W+CJSIS1zeLXPgiCiBwkxkRUsGzHM6je\ndBhGGIUxO+z4/ZI/4JnvrhJtHgzD4LP3L+D833yEsz//EP19fT7fI5VKsfhXK/DW5rfxSdIF1Mnr\nUDmvCg1/p8OiHUt9vlf
"text/plain": [
"<matplotlib.figure.Figure at 0x105f65490>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from sklearn.datasets.samples_generator import make_blobs\n",
"X, y = make_blobs(n_samples=50, centers=2,\n",
" random_state=0, cluster_std=0.60)\n",
"\n",
"xfit = np.linspace(-1, 3.5)\n",
"plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='spring')\n",
"\n",
"# Draw three lines that couple separate the data\n",
"for m, b, d in [(1, 0.65, 0.33), (0.5, 1.6, 0.55), (-0.2, 2.9, 0.2)]:\n",
" yfit = m * xfit + b\n",
" plt.plot(xfit, yfit, '-k')\n",
" plt.fill_between(xfit, yfit - d, yfit + d, edgecolor='none', color='#AAAAAA', alpha=0.4)\n",
"\n",
"plt.xlim(-1, 3.5);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fit the model:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,\n",
" kernel='linear', max_iter=-1, probability=False, random_state=None,\n",
" shrinking=True, tol=0.001, verbose=False)"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sklearn.svm import SVC\n",
"clf = SVC(kernel='linear')\n",
"clf.fit(X, y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot the boundary:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def plot_svc_decision_function(clf, ax=None):\n",
" \"\"\"Plot the decision function for a 2D SVC\"\"\"\n",
" if ax is None:\n",
" ax = plt.gca()\n",
" x = np.linspace(plt.xlim()[0], plt.xlim()[1], 30)\n",
" y = np.linspace(plt.ylim()[0], plt.ylim()[1], 30)\n",
" Y, X = np.meshgrid(y, x)\n",
" P = np.zeros_like(X)\n",
" for i, xi in enumerate(x):\n",
" for j, yj in enumerate(y):\n",
" P[i, j] = clf.decision_function([xi, yj])\n",
" # plot the margins\n",
" ax.contour(X, Y, P, colors='k',\n",
" levels=[-1, 0, 1], alpha=0.5,\n",
" linestyles=['--', '-', '--'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the following plot the dashed lines touch a couple of the points known as *support vectors*, which are stored in the ``support_vectors_`` attribute of the classifier:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAdwAAAFRCAYAAADejRzzAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xd8VNeZ+P/PjDSjmVFDnQ4SghG9I0TvvRhsbAO2Yzt2\n4jhtk/1usrspu9lNNvltstndZNNjxxUMLmADNsVgTO+IImAAiV6FEEJlRtPu74+xBg33CtSmSHre\nr5dfWOfemfvMRejROfc85+gURUEIIYQQwaUPdwBCCCFEW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oF1B4fXH3mT2\n7xeEKTK1/K2H6PlUN7o7u6uOLZu6gqlvBybPmvvsdDr5fPpGlhQsDjh+Lfoau/7jAOOenfDQa+96\nYxvOVxwMPzmUuzF3OTLsGD1/3Jseg3s25SO1CpH0/dyayX32SUuL11yEIfLG44S4z+HPDzAxf4Kq\nXYeO5D3tcDqdoQ+qDsUHb2omWwDTubqf5e5ZsZN5BXNV7R3cHXCutT/0uvkbDtLzX7NYdPJRutOd\nAdUDeHrnUi588yyVlZX1jl8IETyScEXEK79ZTqo3VfOYpTIWp7M6xBHVLaZjDOVo/4bvTK77FwPn\nlWri0F5f2njr4bPdb628SZ+K3qr2R07PZ89rOx76eiFE8EnCFRFv8PShbO3wueaxm72LiYtTLyEZ\nLnmPjWFN37Wq9lJdKUyPqvN18X0TuKG/oXmsusvDe/DGG9pJOYYYdFcf+nIhRAhIwhURL7FdO249\nWcr16OsB7YeSDpHy5fSgXvvGpeus/9ZHbBu9iW2jPmX9Nz7i2vm6M5jBYCDrv6y8OeQtrumv4cXL\nZ6lb+fCFdUz6xtQ6Xzd
"text/plain": [
"<matplotlib.figure.Figure at 0x10efed990>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='spring')\n",
"plot_svc_decision_function(clf)\n",
"plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1],\n",
" s=200, facecolors='none');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use IPython's ``interact`` functionality to explore how the distribution of points affects the support vectors and the discriminative fit:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAd8AAAFVCAYAAACuK+XmAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xd8W+d1+P8PuDcJAlziFDWgRWpL1N57WcNaHvFIUsdJ\nHD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go7F58wdEq1FZWcm2H26kw7Zk0u+lcTRrH2UzLEz6l2mSbCT8Vsee2XT8\nTXZLD0MIF00uL2kwGEKAAKPRWH3/8QFgjtFovOVmdZn42IZ8sOgDFi5Z6GxAAHXN53f8eAezfjKr\nBUfW8oqKiggKCiIuTl2BSQjR7jzybKQ5Z74vArnANw0GQwcgBrjT2MpSR9T7nrRe67k9Z7nx1lXC\nLoVii7FhHwfjvjORgIAHM9FuXbtJ5/WdXQIvQBxx1C61c+eV4jbdXaaxz/jUthPc/cNN0k+mYg2q\n5c7AArr+sAcde8qZVlP5U93h/EvXuHX+Jp36dyEpJamlh+Mx/vQZt2UJCdGPXKc5e8u3gP8zGAz1\nkx1flEvOrdeZnacJflXhmXsLncsqDlawJH85s96Y51x25eglJpeOc/cWJN9OorS0FJ3OfZWhturK\nqUsEfMfO4oIHnx2bYOm1ZejX64mOjmm5wbUxlZWVnNpzghh9LN36dPfabY4SczE7X99K7925jKkY\nxrH44+yfuIspv55JSEiIV7Yp2qcmF9kwGo02o9H4nNFoHHn/z35vDEz4xu03rzPs3lCXZVFE0efT\nXK4ZrzqXZeRkcTbKTW0/4F5SITEx7S/QXHrHyOiCUarlcy7MZv/fpICDp2z73WZOjD7IkGf6kzI9\nng0z13L52EWvbGvXP27
"text/plain": [
"<matplotlib.figure.Figure at 0x1102e8390>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.html.widgets import interact\n",
"\n",
"def plot_svm(N=100):\n",
" X, y = make_blobs(n_samples=200, centers=2,\n",
" random_state=0, cluster_std=0.60)\n",
" X = X[:N]\n",
" y = y[:N]\n",
" clf = SVC(kernel='linear')\n",
" clf.fit(X, y)\n",
" plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='spring')\n",
" plt.xlim(-1, 4)\n",
" plt.ylim(-1, 6)\n",
" plot_svc_decision_function(clf, plt.gca())\n",
" plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1],\n",
" s=200, facecolors='none')\n",
" \n",
"interact(plot_svm, N=[10, 200], kernel='linear');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Support Vector Machine with Kernels Classifier\n",
"\n",
"Kernels are useful when the decision boundary is not linear. A Kernel is some functional transformation of the input data. SVMs have clever tricks to ensure kernel calculations are efficient. In the example below, a linear boundary is not useful in separating the groups of points:"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAeYAAAFRCAYAAAChXA4CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xd8FOe18PHfSlr1LtSFUAEt6m1XEr0bsCm2sQ3ucUuc\nxDdxypvk5tY3t6Xf+CZ5k+sWJzZgGxtsisFgG4Np0qpLSFohkCgChHrfot15/1gQErs0aaVdSc83\nn3w+1szOzNGw2rMz85znyCRJQhAEQRAEx+Bk7wAEQRAEQbhOJGZBEARBcCAiMQuCIAiCAxGJWRAE\nQRAciEjMgiAIguBARGIWBEEQBAfiYu8AAJqbu21asxUQ4El7e58tdzmhifMxnDgf14lzMZw4H8OJ\n83Gdrc9FcLCP7GbrJuUVs4uLs71DcCjifAwnzsd14lwMJ87HcOJ8XDee52JSJmZBEARBmKhEYhYE\nQRAEByISsyAIgiA4EJGYBUEQBMGBiMQsCIIgCA5EJGZBEARBcCAiMQuCIAiCAxGJWRAEQRAcyKhm\n/lIoFLnAzzUazZIbln8PeA5ovrroGxqNpnY0xxIEQRCEqWDEiVmhUPwIeALosbI6C3hSo9GUjHT/\ngiAIgjAVjeZWdh3wIGBtvs9s4KcKheIrhULxk1EcQxAEQRCmlBEnZo1Gsx0YuMnqrcA3gKXAfIVC\ncd9IjyMIgiAIU8lYdZd6RaPRdAEoFIo9QCaw52YvDgjwtPkE4cHBPjbd30Qnzsdw4nxcd7tz0dXV\nhclkwt/ff5wisi/x3hhOnI/rxutc2DwxKxQKP6Bc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xEJpORkKBApcod87t
"text/plain": [
"<matplotlib.figure.Figure at 0x10fbeddd0>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from sklearn.datasets.samples_generator import make_circles\n",
"X, y = make_circles(100, factor=.1, noise=.1)\n",
"\n",
"clf = SVC(kernel='linear').fit(X, y)\n",
"\n",
"plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='spring')\n",
"plot_svc_decision_function(clf);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A simple model that could be useful is a **radial basis function**:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAcwAAAFBCAYAAAD69Z+AAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzsvXmcW2d97/85i6SZ0Uia1TPjmfF43+04sZN4yZ5AQpwm\nARKS/IB7C71QaAvcttw2oaUb8IPb3hRK0zY/CqG9EBpCSEKcQDayx4TES7yvY489qz3j8azSSGf7\n/fHo6BxJR/s50iPpeb9e4IyWc56z6Pmc7/f5LpymaWAwGAwGg5EevtQDYDAYDAajHGCCyWAwGAxG\nFjDBZDAYDAYjC5hgMhgMBoORBUwwGQwGg8HIAiaYDAaDwWBkgZjuzdHRaZZzwmAwGIyqorXVx1m9\nzixMBoPBYDCygAkmg8FgMBhZwASTwWAwGIwsYILJYDAYDEYWMMFkMBgMBiMLmGAyGAwGg5EFTDAZ\nDAaDwcgCJpgMBoPBYGQBE0wGg8FgMLKACSaDwWAwGFnABJPBYDAYjCxggslgMBgMRhYwwWQwGAwG\nIwuYYDIYDAaDkQVMMBkMBoPByAImmAwGg8FgZAETTAaDwWAwsoAJJoPBYDAYWcAEk1G1qKoKVVVL\nPQwGg1EmMMFkVB1EKGVomgRBAAQB4HkAUADI0DStxCNkMBg0IpZ6AAxGsVBVFZomQdNkABw4TgDH\nceA4gOMAWY5AVRV4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tSSawPvfN265Xp+coEJpo1YCWUpx5LZNVmCgdlIqnq22deyTCwYX8xSZqm7bEQioejn\n+OgEltoaLY90g8Kp4EP
"text/plain": [
"<matplotlib.figure.Figure at 0x1104c6490>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"r = np.exp(-(X[:, 0] ** 2 + X[:, 1] ** 2))\n",
"\n",
"from mpl_toolkits import mplot3d\n",
"\n",
"def plot_3D(elev=30, azim=30):\n",
" ax = plt.subplot(projection='3d')\n",
" ax.scatter3D(X[:, 0], X[:, 1], r, c=y, s=50, cmap='spring')\n",
" ax.view_init(elev=elev, azim=azim)\n",
" ax.set_xlabel('x')\n",
" ax.set_ylabel('y')\n",
" ax.set_zlabel('r')\n",
"\n",
"interact(plot_3D, elev=[-90, 90], azip=(-180, 180));"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In three dimensions, there is a clear separation between the data. Run the SVM with the rbf kernel:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAeYAAAFRCAYAAAChXA4CAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xd4VOed6PHvjGbUu1DvEtKAhBBCdESRqAIEGDDNduzE\nTrJObnpu2vZkd+8mu9lNNsmNb2LHiYONMZgmRBdVCEQH0UYSQh313qae+8fAiLEGTJE0I+n98Pgx\nc8qcd17OzO+8XSZJEoIgCIIg2Ae5rRMgCIIgCEIfEZgFQRAEwY6IwCwIgiAIdkQEZkEQBEGwIyIw\nC4IgCIIdEYFZEARBEOyIwtYJAGho6BjQMVs+Pq60tHQP5FsOayI/LIn86CPywpLID0siP/oMdF74\n+3vIHrdvRJaYFQoHWyfBroj8sCTyo4/IC0siPyyJ/OgzlHkxIgOzIAiCIAxXIjALgiAIgh0RgVkQ\nBEEQ7IgIzIIgCIJgR0RgFgRBEAQ7IgKzI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sp86s+P/7GT53UyccSbP5wylG8rJfH10lRArbpYxtTnZ6r6I++E0NjYQFhY+xKkS\n7J0IzMKos/bnazk9v4CPdm9DbpDhnTGGlxauH5SqXqPRSGNjI3V1tYSFhWHR2bLb1LZdSCG3uAWA\nDBkeeOCHn8X7PHre8uVZSJL0oPSrxMHB4Ylpnzcv/ZnSvGTJMnp7e9Boeuno6KC9vZ2Ojg58fHys\nHn/06GEaGxtM6ZfJ8PDwICAgkJk7Z7Mv9yC6Dh0TViSzLNx6gBrJIidEc8v3NvOa/fvtqwipZPKY\n/sPyBEEEZmFUGjclgXF
"text/plain": [
"<matplotlib.figure.Figure at 0x110532b10>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"clf = SVC(kernel='rbf')\n",
"clf.fit(X, y)\n",
"\n",
"plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='spring')\n",
"plot_svc_decision_function(clf)\n",
"plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1],\n",
" s=200, facecolors='none');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"SVM additional notes:\n",
"* When using an SVM you need to choose the right values for parameters such as c and gamma. Model validation can help to determine these optimal values by trial and error.\n",
"* SVMs run in O(n^3) performance. LinearSVC is scalable, SVC does not seem to be scalable. For large data sets try transforming the data to a smaller space and use LinearSVC with rbf."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.9"
}
},
"nbformat": 4,
"nbformat_minor": 0
}