{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.datasets import make_circles\nfrom sklearn import svm, metrics","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"code","source":"def make_meshgrid(x, y, h=.02):\n    \"\"\"Create a mesh of points to plot in\n\n    Parameters\n    ----------\n    x: data to base x-axis meshgrid on\n    y: data to base y-axis meshgrid on\n    h: stepsize for meshgrid, optional\n\n    Returns\n    -------\n    xx, yy : ndarray\n    \"\"\"\n    x_min, x_max = x.min() - 1, x.max() + 1\n    y_min, y_max = y.min() - 1, y.max() + 1\n    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),\n                         np.arange(y_min, y_max, h))\n    return xx, yy\n\n\ndef plot_contours(ax, clf, xx, yy, **params):\n    \"\"\"Plot the decision boundaries for a classifier.\n\n    Parameters\n    ----------\n    ax: matplotlib axes object\n    clf: a classifier\n    xx: meshgrid ndarray\n    yy: meshgrid ndarray\n    params: dictionary of params to pass to contourf, optional\n    \"\"\"\n    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])\n    Z = Z.reshape(xx.shape)\n    out = ax.contourf(xx, yy, Z, **params)\n    return out","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fe86385bf8e02067ba42d07c63140edf7bf4c0dc"},"cell_type":"code","source":"samples = 500\ntrain_prop = 0.8\n\n# Make data\nx, y = make_circles(n_samples=samples, noise=0.05, random_state=123)\n\n# Plot\ndf = pd.DataFrame(dict(x=x[:, 0], y=x[:, 1], label=y))\n\ngroups = df.groupby('label')\n\nfig, ax = plt.subplots()\nax.margins(0.05)  # Optional, just adds 5% padding to the autoscaling\nfor name, group in groups:\n    ax.plot(group.x, group.y, marker='o', linestyle='', ms=6, label=name)\nax.legend()\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7cc954fc649afd5615e8e059b5056276a235358a"},"cell_type":"code","source":"# Minmax scale\n\nx = (x-x.min())/(x.max()-x.min())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"770654d316a54e765ed4cf361a43821876d969bd"},"cell_type":"code","source":"# Linear\nC = 1.0  # SVM regularization parameter\nmodels = svm.SVC(kernel='linear', C=C)\nmodels.fit(x, y)\n\n# title for the plots\ntitles = ('SVC with linear kernel')\n\n# Set-up 2x2 grid for plotting.\nfig, sub = plt.subplots()\nplt.subplots_adjust(wspace=0.4, hspace=0.4)\n\nX0, X1 = x[:, 0], x[:, 1]\nxx, yy = make_meshgrid(X0, X1)\n\nplot_contours(sub, models, xx, yy,\n              cmap=plt.cm.coolwarm, alpha=0.8)\nsub.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k')\nsub.set_xlim(-0.25, 1.25)\nsub.set_ylim(-0.25, 1.25)\nsub.set_xlabel('X')\nsub.set_ylabel('Y')\nsub.set_title(titles)\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"525a14147d230f454456b0a1b7739eac691d00e0"},"cell_type":"code","source":"# Poly 3\nC = 1.0  # SVM regularization parameter\nmodels = svm.SVC(kernel='poly', degree=3, C=C, gamma='auto')\nmodels.fit(x, y)\n\n# title for the plots\ntitles = ('SVC with 3rd degree polynomial kernel')\n\n# Set-up 2x2 grid for plotting.\nfig, sub = plt.subplots()\nplt.subplots_adjust(wspace=0.4, hspace=0.4)\n\nX0, X1 = x[:, 0], x[:, 1]\nxx, yy = make_meshgrid(X0, X1)\n\nplot_contours(sub, models, xx, yy,\n              cmap=plt.cm.coolwarm, alpha=0.8)\nsub.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k')\nsub.set_xlim(-0.25, 1.25)\nsub.set_ylim(-0.25, 1.25)\nsub.set_xlabel('X')\nsub.set_ylabel('Y')\nsub.set_title(titles)\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"94bc8696f3faeb60163d7e2132d49059fab0b643"},"cell_type":"code","source":"# RBF\nC = 1.0  # SVM regularization parameter\nmodels = svm.SVC(kernel='rbf', gamma=0.7, C=C)\nmodels.fit(x, y)\n\n# title for the plots\ntitles = ('SVC with RBF kernel')\n\n# Set-up 2x2 grid for plotting.\nfig, sub = plt.subplots()\nplt.subplots_adjust(wspace=0.4, hspace=0.4)\n\nX0, X1 = x[:, 0], x[:, 1]\nxx, yy = make_meshgrid(X0, X1)\n\nplot_contours(sub, models, xx, yy,\n              cmap=plt.cm.coolwarm, alpha=0.8)\nsub.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k')\nsub.set_xlim(-0.25, 1.25)\nsub.set_ylim(-0.25, 1.25)\nsub.set_xlabel('X')\nsub.set_ylabel('Y')\nsub.set_title(titles)\n\nplt.show()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}