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 \n AAALEgAACxIB0t1+/AAAIABJREFUeJzsvXd8G3d65/9BB9g7RVGFokSBRSySrGarN1uy1UnZluS6 \n 2b1k95JNsr/NZu+VzS+X7OVyv73b3OWSbdmsd23JtkiqWpKtZku2ZEsukkiKBaQoUZ29omMw8/tj \n gAFAgkQRMADJ5/168UXgO8DMdwhwPvOU7/NIOI4DQRAEQRCRQxrpCRAEQRDEZIfE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\n xetY/rulyLPkCWNZbBZeObYHn771SQRn5psrhy5j242tI8ZnWGbAeFjv8/2f/M0ZvPnh6yg0FQAA \n su3ZeOPqa7j+wy8Dmsenv/kEX6y5iOkbMmBbbsCHLx3H/eZ7Ae2DIIjIQGJMRAVdJ9sxxzpnxHgc \n 4sB9Ft1Z7eY2ExLhvWKbslMx5nstFgtSLyZBhpGW/9Kri6G70eTXHL489Dnm/3QedjXvQC5yscj0 \n FF79eC/qvvcNbDabX/sgCCJykBgTUYGEGT0+Ota2aCAmLw7dkm6v2yzZ1jHfq9frkTKQ4nXbdMt0 \n dLV1+DWHweoB5JpH5jlsr9mGL6ou+bUPgiAiB4kxERWon4lFl6RrxDgDBrYyewRm5D9Ltz+N44s+ \n GDHeHNOCpArvQuskOTkZD2c/8rrtSsZVFDwzz685KNu9W+CJSIS1zeLXPgiCiBwkxkRUsGzHM6je \n dBhGGIUxO+z4/ZI/4JnvrhJtHgzD4LP3L+D833yEsz//EP19fT7fI5VKsfhXK/DW5rfxSdIF1Mnr \n UDmvCg1/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 " : " iV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amoqODzH39K/M44LBVmSrPvkPx8GkPnj3joaxVFoWD/McpulDFgwkDi4xNCELEQIhQk4TZRXHw8 \n C1Y8zr41u6k6UoEnUWHEl0aS2K5duENrVU7vtXH574uYd3ouZszc1N3kw9feY/xfp5CUmhzu8PwU \n RWHji2t5YfPzRPHF1pTFcOTEEfKNBxk0c2idry06ehbbDwoYc3AUae4B7Oi8g9tP3GXa92fV+/pu \n t5tTR09iibeQ1bNHUz+OEKIZ6RRFCdqbFxeXN/uby/ZPoRFJ99nr9bJp1lqeOrQ0oF1B4fXH3mT2 \n 7xeEKTK1/K2H6PlUN7o7u6uOLZu6gqlvBybPmvvsdDr5fPpGlhQsDjh+Lfoau/7jAOOenfDQa+96 \n YxvOVxwMPzmUuzF3OTLsGD1/3Jseg3s25SO1CpH0/dyayX32SUuL11yEIfLG44S4z+HPDzAxf4Kq \n XYeO5D3tcDqdoQ+qDsUHb2omWwDTubqf5e5ZsZN5BXNV7R3cHXCutT/0uvkbDtLzX7NYdPJRutOd \n AdUDeHrnUi588yyVlZX1jl8IETyScEXEK79ZTqo3VfOYpTIWp7M6xBHVLaZjDOVo/4bvTK77FwPn \n lWri0F5f2njr4bPdb628SZ+K3qr2R07PZ89rOx76eiFE8EnCFRFv8PShbO3wueaxm72LiYtTLyEZ \n LnmPjWFN37Wq9lJdKUyPqvN18X0TuKG/oXmsusvDe/DGG9pJOYYYdFcf+nIhRAhIwhURL7FdO249 \n Wcr16OsB7YeSDpHy5fSgXvvGpeus/9ZHbBu9iW2jPmX9Nz7i2vm6M5jBYCDrv6y8OeQtrumv4cXL \n Z6lb+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 \n AAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xd8W+d1+P8PuDcJAlziFDWgRWpL1N57WcNaHvFIUsdJ \n HDdJv2nSNm2SNkl/SRNnNnFi19sa1rRlbcnae09oU5siAU5wgADu7w+KEKELWiIFgCB53n7pZeHi \n AvchBN5zx3nO0SiKghBCCCF8J6ClByCEEEK0NxJ8hRBCCB+T4CuEEEL4mARfIYQQwsck+AohhBA+ \n JsFXCCGE8LGg5rzIYDD8EJgBBAN/NBqN73p0VEIIIUQb1uQzX4PBMBoYYjQahwKjgWwPj0kIIYRo \n 05pz5jsROGUwGFYDMcA/eXZIQgghRNvWnOCbAKQD06k7610LdPPkoIQQQoi2rDnBtwg4ZzQabcAF \n g8FQbTAY9EajsejhFRVFUTQazRMPUgghhGhFHhn4mhN8dwPfAX5jMBg6AJGAye3WNRoKC8ubsQnR \n FAkJ0fI5e5l8xt4nn7H3yWfsGwkJ0Y9cp8kJV0ajcR1wzGAwHKTukvOrRqNRujMIIYQQj6lZU42M \n RuMPPD0QIYQQor2QIhtCCCGEj0nwFUIIIXxMgq8QQgjhYxJ8hRBCCB+T4CuEEEL4mARfIYQQwsck \n +AohhBA+JsFXCCGE8DEJvkIIIYSPSfAVQgghfEyCrxBCCOFjEnyFEEIIH5PgK4QQQviYBF8hhBDC \n xyT4CiGEED7WrH6+wvdO7jxBwfJbBBcHUZ1pJfdrfemQleq17SmKwokdxyjaUoAjWKHj7M50ye3q \n te0JIUR7IsG3Fdj55na6/a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ain " : [
" <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 \n AAALEgAACxIB0t1+/AAAIABJREFUeJzs3Xd8FOe18PHfSlr1LtSFUAEt6m1XEr0bsCm2sQ3ucUuc \n xDdxypvk5tY3t6Xf+CZ5k+sWJzZgGxtsisFgG4Np0qpLSFohkCgChHrfot15/1gQErs0aaVdSc83 \n n3w+1szOzNGw2rMz85znyCRJQhAEQRAEx+Bk7wAEQRAEQbhOJGZBEARBcCAiMQuCIAiCAxGJWRAE \n QRAciEjMgiAIguBARGIWBEEQBAfiYu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/xwAUY3E8mPpTE7 \n Jt3eoQk3MBgMgx2e2tra8PJyIy4uHpUql+joGWJA1zgRiVkQrsrKUpKRkTXqesuAwED8/zuEd/5r \n CzklSrwGvDiWeALX5z2Yu2SBjaKdWGQyGZnLsmGZvSMRrOnt7R3s8NTf34ezszNpaRmsWrUUmHqP \n X+xNJGZhyrhWb3n8+Dny8hZbfPu35fOypPnJJO5Ooqa8Gl3vJeaplogJ+gWH09raOtjhaWBgAHd3 \n D+bMmUdmZjbe3t4EB/vQ3Nxt7zCnHJGYhUnPXG9ZhVqdT0tLM15ebkRHzyIyMmpMjyuTyUhMTxrT \n YwjC3ZIkifPnz1FYWEBdnfn5sb+/P0plDikpabi6uto5QkEkZmFSq6go4/DhQ8PqLVetWoqLy83L \n nQRhMjKZTIMVB5cvXwIgMjIKlSqXmTNniQ5PDkQkZmFSkySJgQHDsHpLcXtOmEp0Oh0VFWUUFanp \n 7OxEJpORkKBApcod87t
" 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 \n AAALEgAACxIB0t1+/AAAIABJREFUeJzsvXmcW2d97/85i6SZ0Uia1TPjmfF43+04sZN4yZ5AQpwm \n AR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62bh1hQSLlNb7z1rAy/gSJsEEs4TQF8yT \n KJSl6SDiBKmOj+dFuN3u6CSmQpJC0IUynTXgVCUZhjV0nF97LDqO4yAI8VNvslvXENTEIgx6pxdz \n 6zsaPCLVUAWJCWYJoEkoyY9NgX1C6UTZufy3aS2UOsYxchwXdYMBPM/D7a6Nfd8qby99JRlDRCt5 \n 8qg2nLSUMrl1461QvYE3gUTshkserVsNMMEsIrQJpYE++VeaRWndc9Oq0EIqUlsD6damJNP3nY6U \n ZBSbYl4+q9xRgCwbSNIcVFWNtUBL5dY19xx18h5MZWEylywjJwyhJDd0bkJp741dPNer/blz2bid \n smlOrWmGxZrPjzn12lS8gKZKgC+P5sm0BJTQNNvSck4QLQ7AA1DhcnliLtrEakaZGnjb6xGh5/w4 \n BRNMBylMKJO2VvBYUrkmyeRf/pS6QwrH8Vm3q0rdPFkwfa/061L0wM5DMvECFd/A25jard26espa \n skfETrduJVmXABNMRzALpX5TlzaYJ5VrUrb8TrlhXaYvf6G0MzLTyhq17rJhztsDAA3h8GySNVpt \n 61I0TbjlGtSSawPvfN265Xp+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 " : " 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DREcyfkGnWZTSz52XIUCjFiVejvhe4KtVpdBsx68Petj2zfB+x7oZQJ \n wiAwKoyP36d8/L6Bcvr0STIyFppfX9h+DtnPDGysXYscOWXvlLFr8TZW/D9TFbebmxtarRZJkpDJ \n HvuA/UQGgwG5XP7c5z8ro5eEEWNfNfYjtF6jb7jZke/l8PqO11A8+LmNqYmh+71udih3k/lT+609 \n EGxHPK4Jo0rgyhCKthYR3xtvsb2NNmRzBv/roNPpcHIyjTBsa21F+lcty2r7Ss9RuijeyPkC2375 \n KUt+uBy5XM6UKVO5fPkiqalTATh3Lp+Ghnr0ej06nQ6DwYBOp2PJkkwCA4P6XXPbto+orq5CqVSi \n UChRKhUoFEoyM5fj79+/M9aVK5dob2/HyckJd3cPPD098fDwwNPT66mq1FPfmMbBLQdZVrPMYnuD \n QwPKxU6POWtkqqutY+yxWHNQfsgVV7wOuNP7k16cnZ1tlDrBXonALIwqSbOTOfXNw+h+oyexx9Tr \n uUpRRc6qg6z+0suDfv1H12S+8NdzrK9Zw21uU001bbTRTjtttJG/7SyJr0wkIiKS4OAQrl+/Zj6v \n vLyM8vKyfu/b29tr9ZqhoWGA6aFAr9eh1+vp6el5bBpv375FVVVlv+0bNmwmMjKq3/aKinLkcjke \n Hh64u3vgO8aPsp86s+P/7GT53UyccSbP5wylG8rJfH10lRArbpYxtTnZ6r6I++E0NjYQFhY+xKkS \n 7J0IzMKos/bnazk9v4CPdm9DbpDhnTGGlxauH5SqXqPRSGNjI3V1tYSFhWHR2bLb1LZdSCG3uAWA \n DBkeeOCHn8X7PHre8uVZSJL0oPSrxMHB4Ylpnzcv/ZnSvGTJMnp7e9Boeuno6KC9vZ2Ojg58fHys \n Hn/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
}