|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# sklearn-porter\n", |
| 8 | + "\n", |
| 9 | + "Repository: https://github.com/nok/sklearn-porter\n", |
| 10 | + "\n", |
| 11 | + "## LinearSVC\n", |
| 12 | + "\n", |
| 13 | + "Documentation: [sklearn.svm.LinearSVC](http://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVC.html)" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "markdown", |
| 18 | + "metadata": {}, |
| 19 | + "source": [ |
| 20 | + "### Loading data:" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "code", |
| 25 | + "execution_count": 1, |
| 26 | + "metadata": {}, |
| 27 | + "outputs": [ |
| 28 | + { |
| 29 | + "name": "stdout", |
| 30 | + "output_type": "stream", |
| 31 | + "text": [ |
| 32 | + "((150, 4), (150,))\n" |
| 33 | + ] |
| 34 | + } |
| 35 | + ], |
| 36 | + "source": [ |
| 37 | + "from sklearn.datasets import load_iris\n", |
| 38 | + "\n", |
| 39 | + "iris_data = load_iris()\n", |
| 40 | + "X = iris_data.data\n", |
| 41 | + "y = iris_data.target\n", |
| 42 | + "\n", |
| 43 | + "print(X.shape, y.shape)" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "markdown", |
| 48 | + "metadata": {}, |
| 49 | + "source": [ |
| 50 | + "### Train classifier:" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": 2, |
| 56 | + "metadata": {}, |
| 57 | + "outputs": [ |
| 58 | + { |
| 59 | + "data": { |
| 60 | + "text/plain": [ |
| 61 | + "LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,\n", |
| 62 | + " intercept_scaling=1, loss='squared_hinge', max_iter=1000,\n", |
| 63 | + " multi_class='ovr', penalty='l2', random_state=0, tol=0.0001,\n", |
| 64 | + " verbose=0)" |
| 65 | + ] |
| 66 | + }, |
| 67 | + "execution_count": 2, |
| 68 | + "metadata": {}, |
| 69 | + "output_type": "execute_result" |
| 70 | + } |
| 71 | + ], |
| 72 | + "source": [ |
| 73 | + "from sklearn import svm\n", |
| 74 | + "\n", |
| 75 | + "clf = svm.LinearSVC(C=1., random_state=0)\n", |
| 76 | + "clf.fit(X, y)" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "markdown", |
| 81 | + "metadata": {}, |
| 82 | + "source": [ |
| 83 | + "### Transpile classifier:" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": 4, |
| 89 | + "metadata": { |
| 90 | + "scrolled": false |
| 91 | + }, |
| 92 | + "outputs": [ |
| 93 | + { |
| 94 | + "name": "stdout", |
| 95 | + "output_type": "stream", |
| 96 | + "text": [ |
| 97 | + "import java.io.File;\n", |
| 98 | + "import java.io.FileNotFoundException;\n", |
| 99 | + "import java.util.*;\n", |
| 100 | + "import com.google.gson.Gson;\n", |
| 101 | + "\n", |
| 102 | + "\n", |
| 103 | + "class LinearSVC {\n", |
| 104 | + "\n", |
| 105 | + " private class Classifier {\n", |
| 106 | + " private double[][] coefficients;\n", |
| 107 | + " private double[] intercepts;\n", |
| 108 | + " }\n", |
| 109 | + "\n", |
| 110 | + " private Classifier clf;\n", |
| 111 | + "\n", |
| 112 | + " public LinearSVC(String file) throws FileNotFoundException {\n", |
| 113 | + " String jsonStr = new Scanner(new File(file)).useDelimiter(\"\\\\Z\").next();\n", |
| 114 | + " this.clf = new Gson().fromJson(jsonStr, Classifier.class);\n", |
| 115 | + " }\n", |
| 116 | + "\n", |
| 117 | + " public int predict(double[] features) {\n", |
| 118 | + " int classIdx = 0;\n", |
| 119 | + " double classVal = Double.NEGATIVE_INFINITY;\n", |
| 120 | + " for (int i = 0, il = this.clf.intercepts.length; i < il; i++) {\n", |
| 121 | + " double prob = 0.;\n", |
| 122 | + " for (int j = 0, jl = this.clf.coefficients[0].length; j < jl; j++) {\n", |
| 123 | + " prob += this.clf.coefficients[i][j] * features[j];\n", |
| 124 | + " }\n", |
| 125 | + " if (prob + this.clf.intercepts[i] > classVal) {\n", |
| 126 | + " classVal = prob + this.clf.intercepts[i];\n", |
| 127 | + " classIdx = i;\n", |
| 128 | + " }\n", |
| 129 | + " }\n", |
| 130 | + " return classIdx;\n", |
| 131 | + " }\n", |
| 132 | + "\n", |
| 133 | + " public static void main(String[] args) throws FileNotFoundException {\n", |
| 134 | + " if (args.length > 0 && args[0].endsWith(\".json\")) {\n", |
| 135 | + "\n", |
| 136 | + " // Features:\n", |
| 137 | + " double[] features = new double[args.length-1];\n", |
| 138 | + " for (int i = 1, l = args.length; i < l; i++) {\n", |
| 139 | + " features[i - 1] = Double.parseDouble(args[i]);\n", |
| 140 | + " }\n", |
| 141 | + "\n", |
| 142 | + " // Parameters:\n", |
| 143 | + " String modelData = args[0];\n", |
| 144 | + "\n", |
| 145 | + " // Estimators:\n", |
| 146 | + " LinearSVC clf = new LinearSVC(modelData);\n", |
| 147 | + "\n", |
| 148 | + " // Prediction:\n", |
| 149 | + " int prediction = clf.predict(features);\n", |
| 150 | + " System.out.println(prediction);\n", |
| 151 | + "\n", |
| 152 | + " }\n", |
| 153 | + " }\n", |
| 154 | + "}\n", |
| 155 | + "CPU times: user 1.1 ms, sys: 1.6 ms, total: 2.7 ms\n", |
| 156 | + "Wall time: 1.36 ms\n" |
| 157 | + ] |
| 158 | + } |
| 159 | + ], |
| 160 | + "source": [ |
| 161 | + "%%time\n", |
| 162 | + "\n", |
| 163 | + "from sklearn_porter import Porter\n", |
| 164 | + "\n", |
| 165 | + "porter = Porter(clf)\n", |
| 166 | + "output = porter.export(export_data=True)\n", |
| 167 | + "\n", |
| 168 | + "print(output)" |
| 169 | + ] |
| 170 | + }, |
| 171 | + { |
| 172 | + "cell_type": "markdown", |
| 173 | + "metadata": {}, |
| 174 | + "source": [ |
| 175 | + "Parameters:" |
| 176 | + ] |
| 177 | + }, |
| 178 | + { |
| 179 | + "cell_type": "code", |
| 180 | + "execution_count": 5, |
| 181 | + "metadata": {}, |
| 182 | + "outputs": [ |
| 183 | + { |
| 184 | + "name": "stdout", |
| 185 | + "output_type": "stream", |
| 186 | + "text": [ |
| 187 | + "{\"coefficients\": [[0.184242094585, 0.451230000252, -0.807945877167, -0.450716600333], [0.0528774557485, -0.892149952286, 0.403980844596, -0.937682166145], [-0.850707843193, -0.986702149222, 1.38101044874, 1.86540956624]], \"intercepts\": [0.109562664067, 1.66367077767, -1.70961094165]}" |
| 188 | + ] |
| 189 | + } |
| 190 | + ], |
| 191 | + "source": [ |
| 192 | + "%%bash\n", |
| 193 | + "\n", |
| 194 | + "cat data.json" |
| 195 | + ] |
| 196 | + }, |
| 197 | + { |
| 198 | + "cell_type": "markdown", |
| 199 | + "metadata": { |
| 200 | + "hideOutput": false |
| 201 | + }, |
| 202 | + "source": [ |
| 203 | + "### Run classification in Java:" |
| 204 | + ] |
| 205 | + }, |
| 206 | + { |
| 207 | + "cell_type": "markdown", |
| 208 | + "metadata": {}, |
| 209 | + "source": [ |
| 210 | + "Save the transpiled estimator:" |
| 211 | + ] |
| 212 | + }, |
| 213 | + { |
| 214 | + "cell_type": "code", |
| 215 | + "execution_count": 6, |
| 216 | + "metadata": { |
| 217 | + "collapsed": true |
| 218 | + }, |
| 219 | + "outputs": [], |
| 220 | + "source": [ |
| 221 | + "with open('LinearSVC.java', 'w') as f:\n", |
| 222 | + " f.write(output)" |
| 223 | + ] |
| 224 | + }, |
| 225 | + { |
| 226 | + "cell_type": "markdown", |
| 227 | + "metadata": {}, |
| 228 | + "source": [ |
| 229 | + "Download the dependencies:" |
| 230 | + ] |
| 231 | + }, |
| 232 | + { |
| 233 | + "cell_type": "code", |
| 234 | + "execution_count": 7, |
| 235 | + "metadata": {}, |
| 236 | + "outputs": [ |
| 237 | + { |
| 238 | + "name": "stderr", |
| 239 | + "output_type": "stream", |
| 240 | + "text": [ |
| 241 | + "--2017-12-02 17:35:06-- http://central.maven.org/maven2/com/google/code/gson/gson/2.8.2/gson-2.8.2.jar\n", |
| 242 | + "Resolving central.maven.org... 151.101.36.209\n", |
| 243 | + "Connecting to central.maven.org|151.101.36.209|:80... connected.\n", |
| 244 | + "HTTP request sent, awaiting response... 200 OK\n", |
| 245 | + "Length: 232932 (227K) [application/java-archive]\n", |
| 246 | + "Saving to: 'gson-2.8.2.jar'\n", |
| 247 | + "\n", |
| 248 | + " 0K .......... .......... .......... .......... .......... 21% 1.87M 0s\n", |
| 249 | + " 50K .......... .......... .......... .......... .......... 43% 3.28M 0s\n", |
| 250 | + " 100K .......... .......... .......... .......... .......... 65% 6.27M 0s\n", |
| 251 | + " 150K .......... .......... .......... .......... .......... 87% 3.97M 0s\n", |
| 252 | + " 200K .......... .......... ....... 100% 171M=0.06s\n", |
| 253 | + "\n", |
| 254 | + "2017-12-02 17:35:06 (3.62 MB/s) - 'gson-2.8.2.jar' saved [232932/232932]\n", |
| 255 | + "\n" |
| 256 | + ] |
| 257 | + } |
| 258 | + ], |
| 259 | + "source": [ |
| 260 | + "%%bash\n", |
| 261 | + "\n", |
| 262 | + "wget http://central.maven.org/maven2/com/google/code/gson/gson/2.8.2/gson-2.8.2.jar" |
| 263 | + ] |
| 264 | + }, |
| 265 | + { |
| 266 | + "cell_type": "markdown", |
| 267 | + "metadata": {}, |
| 268 | + "source": [ |
| 269 | + "Compiling:" |
| 270 | + ] |
| 271 | + }, |
| 272 | + { |
| 273 | + "cell_type": "code", |
| 274 | + "execution_count": 8, |
| 275 | + "metadata": { |
| 276 | + "collapsed": true |
| 277 | + }, |
| 278 | + "outputs": [], |
| 279 | + "source": [ |
| 280 | + "%%bash\n", |
| 281 | + "\n", |
| 282 | + "javac -cp .:gson-2.8.2.jar LinearSVC.java" |
| 283 | + ] |
| 284 | + }, |
| 285 | + { |
| 286 | + "cell_type": "markdown", |
| 287 | + "metadata": {}, |
| 288 | + "source": [ |
| 289 | + "Prediction:" |
| 290 | + ] |
| 291 | + }, |
| 292 | + { |
| 293 | + "cell_type": "code", |
| 294 | + "execution_count": 9, |
| 295 | + "metadata": {}, |
| 296 | + "outputs": [ |
| 297 | + { |
| 298 | + "name": "stdout", |
| 299 | + "output_type": "stream", |
| 300 | + "text": [ |
| 301 | + "2\n" |
| 302 | + ] |
| 303 | + } |
| 304 | + ], |
| 305 | + "source": [ |
| 306 | + "%%bash\n", |
| 307 | + "\n", |
| 308 | + "java -cp .:gson-2.8.2.jar LinearSVC data.json 1 2 3 4" |
| 309 | + ] |
| 310 | + } |
| 311 | + ], |
| 312 | + "metadata": { |
| 313 | + "kernelspec": { |
| 314 | + "display_name": "Python 2", |
| 315 | + "language": "python", |
| 316 | + "name": "python2" |
| 317 | + }, |
| 318 | + "language_info": { |
| 319 | + "codemirror_mode": { |
| 320 | + "name": "ipython", |
| 321 | + "version": 2 |
| 322 | + }, |
| 323 | + "file_extension": ".py", |
| 324 | + "mimetype": "text/x-python", |
| 325 | + "name": "python", |
| 326 | + "nbconvert_exporter": "python", |
| 327 | + "pygments_lexer": "ipython2", |
| 328 | + "version": "2.7.13" |
| 329 | + } |
| 330 | + }, |
| 331 | + "nbformat": 4, |
| 332 | + "nbformat_minor": 2 |
| 333 | +} |
0 commit comments