|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import numpy as np\n", |
| 10 | + "from sklearn.svm import OneClassSVM\n", |
| 11 | + "\n", |
| 12 | + "import matplotlib.pyplot as plt\n", |
| 13 | + "import matplotlib.font_manager\n", |
| 14 | + "\n", |
| 15 | + "from scipy import optimize\n", |
| 16 | + "import pandas as pd\n", |
| 17 | + "import seaborn as sns\n", |
| 18 | + "from funclib.iolib import folder_open\n", |
| 19 | + "from dblib import mssql\n", |
| 20 | + "\n", |
| 21 | + "\n", |
| 22 | + "from sklearn.covariance import EllipticEnvelope\n", |
| 23 | + "from sklearn.svm import OneClassSVM\n", |
| 24 | + "import matplotlib.pyplot as plt\n", |
| 25 | + "import matplotlib.font_manager\n", |
| 26 | + "from sklearn.datasets import load_boston\n", |
| 27 | + "\n", |
| 28 | + "\n", |
| 29 | + "INCH = 2.54\n", |
| 30 | + "\n", |
| 31 | + "sns.set()\n", |
| 32 | + "\n", |
| 33 | + "#gey = [\"#FFFFFF\", \"#999999\", \"#666666\", \"#333333\", \"#000000\"]\n", |
| 34 | + "#grey = [\"#FFFFFF\", \"#111111\"]\n", |
| 35 | + "#sns.set_palette(sns.color_palette(\"cubehelix\", 8))\n", |
| 36 | + "\n", |
| 37 | + "sns.set(font=\"Times New Roman\", font_scale=1.2, rc={\"lines.linewidth\": 1})\n", |
| 38 | + "sns.set_style('ticks') #rc={'axes.grid':True}\n", |
| 39 | + "\n", |
| 40 | + "def cm2inch1(v):\n", |
| 41 | + " '''(int|float)->int|float\n", |
| 42 | + " '''\n", |
| 43 | + " return v / INCH\n", |
| 44 | + "\n", |
| 45 | + "INCH = 2.54\n", |
| 46 | + "\n", |
| 47 | + "class FigWidths(Enum):\n", |
| 48 | + " minimal = 3\n", |
| 49 | + " single_col = 9\n", |
| 50 | + " one_and_a_half_col = 14\n", |
| 51 | + " two_col = 19\n", |
| 52 | + "\n", |
| 53 | + "def getwidth(sz, as_inch=True):\n", |
| 54 | + " '''(Enum:FigWidths|float, bool)->float\n", |
| 55 | + "\n", |
| 56 | + " Get publication fig widths in cm or inches\n", |
| 57 | + " '''\n", |
| 58 | + " assert isinstance(sz, FigWidths)\n", |
| 59 | + " return sz.value/INCH if as_inch else sz.value\n", |
| 60 | + "\n", |
| 61 | + "def getheight(width, aspect, width_is_cm=True):\n", |
| 62 | + " '''float|Enum:plotlib.FigWidths, float|None\n", |
| 63 | + " Get width in inches according to target aspect\n", |
| 64 | + "\n", |
| 65 | + " width:target width, or Enum instance plotlib.FigWidths\n", |
| 66 | + " aspect:ratio of width to height i.e. w/h\n", |
| 67 | + " '''\n", |
| 68 | + " assert isinstance(width, (float, int, FigWidths))\n", |
| 69 | + "\n", |
| 70 | + " if isinstance(width, FigWidths):\n", |
| 71 | + " w_inch = width.value / INCH\n", |
| 72 | + " else:\n", |
| 73 | + " w_inch = width / INCH if width_is_cm else width\n", |
| 74 | + " return w_inch / aspect\n", |
| 75 | + " \n", |
| 76 | + "def label_point(x, y, val, ax):\n", |
| 77 | + " a = pd.concat({'x': x, 'y': y, 'val': val}, axis=1)\n", |
| 78 | + " for i, point in a.iterrows():\n", |
| 79 | + " ax.text(point['x'], point['y'], str(point['val']))\n", |
| 80 | + " \n", |
| 81 | + " \n", |
| 82 | + " \n" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "code", |
| 87 | + "execution_count": null, |
| 88 | + "metadata": {}, |
| 89 | + "outputs": [], |
| 90 | + "source": [ |
| 91 | + "\n", |
| 92 | + "\n", |
| 93 | + "# Get data\n", |
| 94 | + "X2 = load_boston()['data'][:, [5, 12]] # \"banana\"-shaped\n", |
| 95 | + "\n", |
| 96 | + "# Define \"classifiers\" to be used\n", |
| 97 | + "classifiers = {\"OCSVM\": OneClassSVM(nu=0.261, gamma=0.05)}\n", |
| 98 | + "colors = ['m', 'g', 'b']\n", |
| 99 | + "legend1 = {}\n", |
| 100 | + "legend2 = {}\n", |
| 101 | + "\n", |
| 102 | + "# Learn a frontier for outlier detection with several classifiers\n", |
| 103 | + "xx2, yy2 = np.meshgrid(np.linspace(3, 10, 500), np.linspace(-5, 45, 500))\n", |
| 104 | + "for i, (clf_name, clf) in enumerate(classifiers.items()):\n", |
| 105 | + " plt.figure(2)\n", |
| 106 | + " clf.fit(X2)\n", |
| 107 | + " Z2 = clf.decision_function(np.c_[xx2.ravel(), yy2.ravel()])\n", |
| 108 | + " Z2 = Z2.reshape(xx2.shape)\n", |
| 109 | + " legend2[clf_name] = plt.contour(xx2, yy2, Z2, levels=[0], linewidths=2, colors=colors[i])\n", |
| 110 | + "\n", |
| 111 | + "legend1_values_list = list(legend1.values())\n", |
| 112 | + "legend1_keys_list = list(legend1.keys())\n", |
| 113 | + "\n", |
| 114 | + "# Plot the results (= shape of the data points cloud)\n", |
| 115 | + "plt.figure(1) # two clusters\n", |
| 116 | + "plt.title(\"Outlier detection on a real data set (boston housing)\")\n", |
| 117 | + "plt.scatter(X1[:, 0], X1[:, 1], color='black')\n", |
| 118 | + "bbox_args = dict(boxstyle=\"round\", fc=\"0.8\")\n", |
| 119 | + "arrow_args = dict(arrowstyle=\"->\")\n", |
| 120 | + "plt.annotate(\"several confounded points\", xy=(24, 19),\n", |
| 121 | + " xycoords=\"data\", textcoords=\"data\",\n", |
| 122 | + " xytext=(13, 10), bbox=bbox_args, arrowprops=arrow_args)\n", |
| 123 | + "plt.xlim((xx1.min(), xx1.max()))\n", |
| 124 | + "plt.ylim((yy1.min(), yy1.max()))\n", |
| 125 | + "plt.legend((legend1_values_list[0].collections[0],\n", |
| 126 | + " legend1_values_list[1].collections[0],\n", |
| 127 | + " legend1_values_list[2].collections[0]),\n", |
| 128 | + " (legend1_keys_list[0], legend1_keys_list[1], legend1_keys_list[2]),\n", |
| 129 | + " loc=\"upper center\",\n", |
| 130 | + " prop=matplotlib.font_manager.FontProperties(size=12))\n", |
| 131 | + "plt.ylabel(\"accessibility to radial highways\")\n", |
| 132 | + "plt.xlabel(\"pupil-teacher ratio by town\")\n", |
| 133 | + "\n", |
| 134 | + "legend2_values_list = list(legend2.values())\n", |
| 135 | + "legend2_keys_list = list(legend2.keys())\n", |
| 136 | + "\n", |
| 137 | + "plt.figure(2) # \"banana\" shape\n", |
| 138 | + "plt.title(\"Outlier detection on a real data set (boston housing)\")\n", |
| 139 | + "plt.scatter(X2[:, 0], X2[:, 1], color='black')\n", |
| 140 | + "plt.xlim((xx2.min(), xx2.max()))\n", |
| 141 | + "plt.ylim((yy2.min(), yy2.max()))\n", |
| 142 | + "plt.legend((legend2_values_list[0].collections[0],\n", |
| 143 | + " legend2_values_list[1].collections[0],\n", |
| 144 | + " legend2_values_list[2].collections[0]),\n", |
| 145 | + " (legend2_keys_list[0], legend2_keys_list[1], legend2_keys_list[2]),\n", |
| 146 | + " loc=\"upper center\",\n", |
| 147 | + " prop=matplotlib.font_manager.FontProperties(size=12))\n", |
| 148 | + "plt.ylabel(\"% lower status of the population\")\n", |
| 149 | + "plt.xlabel(\"average number of rooms per dwelling\")\n", |
| 150 | + "\n", |
| 151 | + "plt.show()" |
| 152 | + ] |
| 153 | + } |
| 154 | + ], |
| 155 | + "metadata": { |
| 156 | + "kernelspec": { |
| 157 | + "display_name": "Python 3", |
| 158 | + "language": "python", |
| 159 | + "name": "python3" |
| 160 | + }, |
| 161 | + "language_info": { |
| 162 | + "codemirror_mode": { |
| 163 | + "name": "ipython", |
| 164 | + "version": 3 |
| 165 | + }, |
| 166 | + "file_extension": ".py", |
| 167 | + "mimetype": "text/x-python", |
| 168 | + "name": "python", |
| 169 | + "nbconvert_exporter": "python", |
| 170 | + "pygments_lexer": "ipython3", |
| 171 | + "version": "3.5.4" |
| 172 | + } |
| 173 | + }, |
| 174 | + "nbformat": 4, |
| 175 | + "nbformat_minor": 2 |
| 176 | +} |
0 commit comments