From 65439b93d3cc598dcc257a67269fea2249c42a7c Mon Sep 17 00:00:00 2001 From: Darius Morawiec Date: Mon, 4 Dec 2017 21:47:57 +0100 Subject: [PATCH] Add prediction in the used programming language --- .../js/basics_embedded.ipynb | 65 +- .../js/basics_imported.ipynb | 92 +- .../classifier/BernoulliNB/js/basics.ipynb | 68 +- .../DecisionTreeClassifier/js/basics.ipynb | 67 +- .../js/basics_embedded.ipynb | 70 +- .../ExtraTreesClassifier/js/basics.ipynb | 979 +++++++++--------- .../classifier/GaussianNB/js/basics.ipynb | 68 +- .../KNeighborsClassifier/js/basics.ipynb | 66 +- .../js/basics_imported.ipynb | 97 +- .../classifier/LinearSVC/js/basics.ipynb | 64 +- .../classifier/MLPClassifier/js/basics.ipynb | 10 +- .../classifier/NuSVC/js/basics.ipynb | 72 +- .../js/basics_embedded.ipynb | 62 +- .../estimator/classifier/SVC/js/basics.ipynb | 66 +- .../regressor/MLPRegressor/js/basics.ipynb | 62 +- 15 files changed, 1219 insertions(+), 689 deletions(-) diff --git a/examples/estimator/classifier/AdaBoostClassifier/js/basics_embedded.ipynb b/examples/estimator/classifier/AdaBoostClassifier/js/basics_embedded.ipynb index 1c64b624..d953a087 100644 --- a/examples/estimator/classifier/AdaBoostClassifier/js/basics_embedded.ipynb +++ b/examples/estimator/classifier/AdaBoostClassifier/js/basics_embedded.ipynb @@ -96,32 +96,6 @@ "metadata": { "scrolled": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "CPU times: user 3.48 ms, sys: 2.33 ms, total: 5.8 ms\n", - "Wall time: 3.99 ms\n" - ] - } - ], - "source": [ - "%%time\n", - "\n", - "from sklearn_porter import Porter\n", - "\n", - "porter = Porter(clf, language='js')\n", - "output = porter.export()\n", - "\n", - "with open('AdaBoostClassifier.js', 'w') as f:\n", - " f.write(output)" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, "outputs": [ { "name": "stdout", @@ -383,14 +357,21 @@ " console.log(prediction);\n", "\n", " }\n", - "}" + "}\n", + "CPU times: user 2.7 ms, sys: 1.61 ms, total: 4.31 ms\n", + "Wall time: 2.94 ms\n" ] } ], "source": [ - "%%bash\n", + "%%time\n", "\n", - "cat AdaBoostClassifier.js" + "from sklearn_porter import Porter\n", + "\n", + "porter = Porter(clf, language='js')\n", + "output = porter.export()\n", + "\n", + "print(output)" ] }, { @@ -400,6 +381,32 @@ "### Run classification in JavaScript:" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Save the transpiled estimator:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "with open('AdaBoostClassifier.js', 'w') as f:\n", + " f.write(output)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Prediction:" + ] + }, { "cell_type": "code", "execution_count": 6, diff --git a/examples/estimator/classifier/AdaBoostClassifier/js/basics_imported.ipynb b/examples/estimator/classifier/AdaBoostClassifier/js/basics_imported.ipynb index 61fb78bb..a61f6c58 100644 --- a/examples/estimator/classifier/AdaBoostClassifier/js/basics_imported.ipynb +++ b/examples/estimator/classifier/AdaBoostClassifier/js/basics_imported.ipynb @@ -96,39 +96,6 @@ "metadata": { "scrolled": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "CPU times: user 1.98 ms, sys: 1.49 ms, total: 3.47 ms\n", - "Wall time: 2.37 ms\n" - ] - } - ], - "source": [ - "%%time\n", - "\n", - "from sklearn_porter import Porter\n", - "\n", - "porter = Porter(clf, language='js')\n", - "output = porter.export(export_data=True)\n", - "\n", - "with open('AdaBoostClassifier.js', 'w') as f:\n", - " f.write(output)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Classifier:" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, "outputs": [ { "name": "stdout", @@ -241,17 +208,26 @@ " // Prediction:\n", " clf.predict(features).then(function(prediction) {\n", " console.log(prediction);\n", + " }, function(error) {\n", + " console.log(error);\n", " });\n", "\n", " }\n", - "}" + "}\n", + "CPU times: user 2.71 ms, sys: 2.25 ms, total: 4.96 ms\n", + "Wall time: 3.57 ms\n" ] } ], "source": [ - "%%bash\n", + "%%time\n", + "\n", + "from sklearn_porter import Porter\n", "\n", - "cat AdaBoostClassifier.js" + "porter = Porter(clf, language='js')\n", + "output = porter.export(export_data=True)\n", + "\n", + "print(output)" ] }, { @@ -263,14 +239,14 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "[{\"indices\": [3, -2, 3, 2, 3, -2, -2, 3, -2, -2, 2, 0, -2, -2, -2], \"thresholds\": [0.800000011921, -2.0, 1.75, 4.94999980927, 1.65000009537, -2.0, -2.0, 1.54999995232, -2.0, -2.0, 4.85000038147, 5.94999980927, -2.0, -2.0, -2.0], \"classes\": [[0.333333333333, 0.333333333333, 0.333333333333], [0.333333333333, 0.0, 0.0], [0.0, 0.333333333333, 0.333333333333], [0.0, 0.326666666667, 0.0333333333333], [0.0, 0.313333333333, 0.00666666666667], [0.0, 0.313333333333, 0.0], [0.0, 0.0, 0.00666666666667], [0.0, 0.0133333333333, 0.0266666666667], [0.0, 0.0, 0.02], [0.0, 0.0133333333333, 0.00666666666667], [0.0, 0.00666666666667, 0.3], [0.0, 0.00666666666667, 0.0133333333333], [0.0, 0.00666666666667, 0.0], [0.0, 0.0, 0.0133333333333], [0.0, 0.0, 0.286666666667]], \"childrenRight\": [2, -1, 10, 7, 6, -1, -1, 9, -1, -1, 14, 13, -1, -1, -1], \"childrenLeft\": [1, -1, 3, 4, 5, -1, -1, 8, -1, -1, 11, 12, -1, -1, -1]}, {\"indices\": [2, 2, -2, 3, 0, -2, -2, 1, -2, -2, -2], \"thresholds\": [5.14999961853, 2.45000004768, -2.0, 1.75, 4.94999980927, -2.0, -2.0, 3.15000009537, -2.0, -2.0, -2.0], \"classes\": [[8.3290724464e-05, 0.499957521731, 0.499959187545], [8.3290724464e-05, 0.499957521731, 2.66530318285e-05], [8.3290724464e-05, 0.0, 0.0], [0.0, 0.499957521731, 2.66530318285e-05], [0.0, 0.499955855916, 4.99744346784e-06], [0.0, 1.66581448928e-06, 1.66581448928e-06], [0.0, 0.499954190102, 3.33162897856e-06], [0.0, 1.66581448928e-06, 2.16555883606e-05], [0.0, 0.0, 1.99897738714e-05], [0.0, 1.66581448928e-06, 1.66581448928e-06], [0.0, 0.0, 0.499932534513]], \"childrenRight\": [10, 3, -1, 7, 6, -1, -1, 9, -1, -1, -1], \"childrenLeft\": [1, 2, -1, 4, 5, -1, -1, 8, -1, -1, -1]}, {\"indices\": [3, 2, 3, -2, -2, -2, 2, 3, 0, -2, -2, -2, -2], \"thresholds\": [1.54999995232, 4.94999980927, 0.800000011921, -2.0, -2.0, -2.0, 5.14999961853, 1.84999990463, 5.40000009537, -2.0, -2.0, -2.0, -2.0], \"classes\": [[2.67881771865e-08, 0.499919588597, 0.500080384615], [2.67881771865e-08, 0.000184731094993, 0.499696643102], [2.67881771865e-08, 0.000184731094993, 0.0], [2.67881771865e-08, 0.0, 0.0], [0.0, 0.000184731094993, 0.0], [0.0, 0.0, 0.499696643102], [0.0, 0.499734857502, 0.000383741512437], [0.0, 0.499734857502, 0.00022295245966], [0.0, 0.499734857502, 0.000111475694067], [0.0, 0.0, 0.000111473015249], [0.0, 0.499734857502, 2.67881771865e-09], [0.0, 0.0, 0.000111476765594], [0.0, 0.0, 0.000160789052777]], \"childrenRight\": [6, 5, 4, -1, -1, -1, 12, 11, 10, -1, -1, -1, -1], \"childrenLeft\": [1, 2, 3, -1, -1, -1, 7, 8, 9, -1, -1, -1, -1]}, {\"indices\": [3, 3, 2, 3, -2, -2, -2, 0, 1, -2, -2, -2, 1, -2, 2, -2, -2], \"thresholds\": [1.75, 1.54999995232, 4.94999980927, 0.800000011921, -2.0, -2.0, -2.0, 6.94999980927, 2.59999990463, -2.0, -2.0, -2.0, 3.15000009537, -2.0, 4.94999980927, -2.0, -2.0], \"classes\": [[9.25765397376e-11, 0.499136211999, 0.500863787909], [9.25765397376e-11, 0.499024872662, 0.00172782900859], [9.25765397376e-11, 6.38407213652e-07, 0.00172688816469], [9.25765397376e-11, 6.38407213652e-07, 0.0], [9.25765397376e-11, 0.0, 0.0], [0.0, 6.38407213652e-07, 0.0], [0.0, 0.0, 0.00172688816469], [0.0, 0.499024234255, 9.40843895869e-07], [0.0, 0.499024234255, 3.85236589785e-07], [0.0, 0.0, 3.85236589785e-07], [0.0, 0.499024234255, 0.0], [0.0, 0.0, 5.55607306084e-07], [0.0, 0.000111339336392, 0.4991359589], [0.0, 0.0, 0.499135573641], [0.0, 0.000111339336392, 3.85258808154e-07], [0.0, 0.000111339336392, 0.0], [0.0, 0.0, 3.85258808154e-07]], \"childrenRight\": [12, 7, 6, 5, -1, -1, -1, 11, 10, -1, -1, -1, 14, -1, 16, -1, -1], \"childrenLeft\": [1, 2, 3, 4, -1, -1, -1, 8, 9, -1, -1, -1, 13, -1, 15, -1, -1]}]" + "[{\"childrenLeft\": [1, -1, 3, 4, 5, -1, -1, 8, -1, -1, 11, 12, -1, -1, -1], \"childrenRight\": [2, -1, 10, 7, 6, -1, -1, 9, -1, -1, 14, 13, -1, -1, -1], \"classes\": [[0.333333333333, 0.333333333333, 0.333333333333], [0.333333333333, 0.0, 0.0], [0.0, 0.333333333333, 0.333333333333], [0.0, 0.326666666667, 0.0333333333333], [0.0, 0.313333333333, 0.00666666666667], [0.0, 0.313333333333, 0.0], [0.0, 0.0, 0.00666666666667], [0.0, 0.0133333333333, 0.0266666666667], [0.0, 0.0, 0.02], [0.0, 0.0133333333333, 0.00666666666667], [0.0, 0.00666666666667, 0.3], [0.0, 0.00666666666667, 0.0133333333333], [0.0, 0.00666666666667, 0.0], [0.0, 0.0, 0.0133333333333], [0.0, 0.0, 0.286666666667]], \"indices\": [3, -2, 3, 2, 3, -2, -2, 3, -2, -2, 2, 0, -2, -2, -2], \"thresholds\": [0.800000011921, -2.0, 1.75, 4.94999980927, 1.65000009537, -2.0, -2.0, 1.54999995232, -2.0, -2.0, 4.85000038147, 5.94999980927, -2.0, -2.0, -2.0]}, {\"childrenLeft\": [1, 2, -1, 4, 5, -1, -1, 8, -1, -1, -1], \"childrenRight\": [10, 3, -1, 7, 6, -1, -1, 9, -1, -1, -1], \"classes\": [[8.3290724464e-05, 0.499957521731, 0.499959187545], [8.3290724464e-05, 0.499957521731, 2.66530318285e-05], [8.3290724464e-05, 0.0, 0.0], [0.0, 0.499957521731, 2.66530318285e-05], [0.0, 0.499955855916, 4.99744346784e-06], [0.0, 1.66581448928e-06, 1.66581448928e-06], [0.0, 0.499954190102, 3.33162897856e-06], [0.0, 1.66581448928e-06, 2.16555883606e-05], [0.0, 0.0, 1.99897738714e-05], [0.0, 1.66581448928e-06, 1.66581448928e-06], [0.0, 0.0, 0.499932534513]], \"indices\": [2, 2, -2, 3, 0, -2, -2, 1, -2, -2, -2], \"thresholds\": [5.14999961853, 2.45000004768, -2.0, 1.75, 4.94999980927, -2.0, -2.0, 3.15000009537, -2.0, -2.0, -2.0]}, {\"childrenLeft\": [1, 2, 3, -1, -1, -1, 7, 8, 9, -1, -1, -1, -1], \"childrenRight\": [6, 5, 4, -1, -1, -1, 12, 11, 10, -1, -1, -1, -1], \"classes\": [[2.67881771865e-08, 0.499919588597, 0.500080384615], [2.67881771865e-08, 0.000184731094993, 0.499696643102], [2.67881771865e-08, 0.000184731094993, 0.0], [2.67881771865e-08, 0.0, 0.0], [0.0, 0.000184731094993, 0.0], [0.0, 0.0, 0.499696643102], [0.0, 0.499734857502, 0.000383741512437], [0.0, 0.499734857502, 0.00022295245966], [0.0, 0.499734857502, 0.000111475694067], [0.0, 0.0, 0.000111473015249], [0.0, 0.499734857502, 2.67881771865e-09], [0.0, 0.0, 0.000111476765594], [0.0, 0.0, 0.000160789052777]], \"indices\": [3, 2, 3, -2, -2, -2, 2, 3, 0, -2, -2, -2, -2], \"thresholds\": [1.54999995232, 4.94999980927, 0.800000011921, -2.0, -2.0, -2.0, 5.14999961853, 1.84999990463, 5.40000009537, -2.0, -2.0, -2.0, -2.0]}, {\"childrenLeft\": [1, 2, 3, 4, -1, -1, -1, 8, 9, -1, -1, -1, 13, -1, 15, -1, -1], \"childrenRight\": [12, 7, 6, 5, -1, -1, -1, 11, 10, -1, -1, -1, 14, -1, 16, -1, -1], \"classes\": [[9.25765397376e-11, 0.499136211999, 0.500863787909], [9.25765397376e-11, 0.499024872662, 0.00172782900859], [9.25765397376e-11, 6.38407213652e-07, 0.00172688816469], [9.25765397376e-11, 6.38407213652e-07, 0.0], [9.25765397376e-11, 0.0, 0.0], [0.0, 6.38407213652e-07, 0.0], [0.0, 0.0, 0.00172688816469], [0.0, 0.499024234255, 9.40843895869e-07], [0.0, 0.499024234255, 3.85236589785e-07], [0.0, 0.0, 3.85236589785e-07], [0.0, 0.499024234255, 0.0], [0.0, 0.0, 5.55607306084e-07], [0.0, 0.000111339336392, 0.4991359589], [0.0, 0.0, 0.499135573641], [0.0, 0.000111339336392, 3.85258808154e-07], [0.0, 0.000111339336392, 0.0], [0.0, 0.0, 3.85258808154e-07]], \"indices\": [3, 3, 2, 3, -2, -2, -2, 0, 1, -2, -2, -2, 1, -2, 2, -2, -2], \"thresholds\": [1.75, 1.54999995232, 4.94999980927, 0.800000011921, -2.0, -2.0, -2.0, 6.94999980927, 2.59999990463, -2.0, -2.0, -2.0, 3.15000009537, -2.0, 4.94999980927, -2.0, -2.0]}]" ] } ], @@ -282,42 +258,40 @@ }, { "cell_type": "markdown", - "metadata": { - "hideCode": true - }, + "metadata": {}, "source": [ - "File size:" + "### Run classification in JavaScript:" ] }, { - "cell_type": "code", - "execution_count": 7, + "cell_type": "markdown", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "4.0K\tdata.json\n" - ] - } - ], "source": [ - "%%bash\n", - "\n", - "du -h data.json" + "Save the transpiled estimator:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "with open('AdaBoostClassifier.js', 'w') as f:\n", + " f.write(output)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Run classification in JavaScript:" + "Prediction:" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -331,7 +305,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "127.0.0.1 - - [24/Nov/2017 01:01:32] \"GET /data.json HTTP/1.1\" 200 -\n" + "127.0.0.1 - - [04/Dec/2017 21:09:25] \"GET /data.json HTTP/1.1\" 200 -\n" ] } ], diff --git a/examples/estimator/classifier/BernoulliNB/js/basics.ipynb b/examples/estimator/classifier/BernoulliNB/js/basics.ipynb index a8468293..bc2c07fe 100644 --- a/examples/estimator/classifier/BernoulliNB/js/basics.ipynb +++ b/examples/estimator/classifier/BernoulliNB/js/basics.ipynb @@ -82,7 +82,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { "scrolled": false }, @@ -133,9 +133,9 @@ " var features = process.argv.slice(2);\n", "\n", " // Parameters:\n", - " var priors = [-1.0986122886681096, -1.0986122886681096, -1.0986122886681096];\n", - " var negProbs = [[-3.9512437185814138, -3.9512437185814138, -3.9512437185814138, -3.9512437185814138], [-3.9512437185814138, -3.9512437185814138, -3.9512437185814138, -3.9512437185814138], [-3.9512437185814138, -3.9512437185814138, -3.9512437185814138, -3.9512437185814138]];\n", - " var delProbs = [[3.931825632724312, 3.931825632724312, 3.931825632724312], [3.931825632724312, 3.931825632724312, 3.931825632724312], [3.931825632724312, 3.931825632724312, 3.931825632724312], [3.931825632724312, 3.931825632724312, 3.931825632724312]];\n", + " var priors = [-1.09861228867, -1.09861228867, -1.09861228867];\n", + " var negProbs = [[-3.95124371858, -3.95124371858, -3.95124371858, -3.95124371858], [-3.95124371858, -3.95124371858, -3.95124371858, -3.95124371858], [-3.95124371858, -3.95124371858, -3.95124371858, -3.95124371858]];\n", + " var delProbs = [[3.93182563272, 3.93182563272, 3.93182563272], [3.93182563272, 3.93182563272, 3.93182563272], [3.93182563272, 3.93182563272, 3.93182563272], [3.93182563272, 3.93182563272, 3.93182563272]];\n", "\n", " // Estimator:\n", " var clf = new BernoulliNB(priors, negProbs, delProbs);\n", @@ -143,11 +143,15 @@ " console.log(prediction);\n", "\n", " }\n", - "}\n" + "}\n", + "CPU times: user 855 µs, sys: 802 µs, total: 1.66 ms\n", + "Wall time: 858 µs\n" ] } ], "source": [ + "%%time\n", + "\n", "from sklearn_porter import Porter\n", "\n", "porter = Porter(clf, language='js')\n", @@ -155,6 +159,60 @@ "\n", "print(output)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Run classification in JavaScript:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Save the transpiled estimator:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "with open('BernoulliNB.js', 'w') as f:\n", + " f.write(output)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Prediction:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "%%bash\n", + "\n", + "if hash node 2/dev/null; then\n", + " node BernoulliNB.js 1 2 3 4\n", + "fi" + ] } ], "metadata": { diff --git a/examples/estimator/classifier/DecisionTreeClassifier/js/basics.ipynb b/examples/estimator/classifier/DecisionTreeClassifier/js/basics.ipynb index 2967b88e..27809527 100644 --- a/examples/estimator/classifier/DecisionTreeClassifier/js/basics.ipynb +++ b/examples/estimator/classifier/DecisionTreeClassifier/js/basics.ipynb @@ -82,6 +82,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "\n", "### Transpile classifier:" ] }, @@ -135,9 +136,9 @@ " // Parameters:\n", " var lChilds = [1, -1, 3, 4, 5, -1, -1, 8, -1, 10, -1, -1, 13, 14, -1, -1, -1];\n", " var rChilds = [2, -1, 12, 7, 6, -1, -1, 9, -1, 11, -1, -1, 16, 15, -1, -1, -1];\n", - " var thresholds = [2.45000004768, -2.0, 1.75, 4.94999980927, 1.65000009537, -2.0, -2.0, 1.54999995232, -2.0, 6.94999980927, -2.0, -2.0, 4.85000038147, 3.09999990463, -2.0, -2.0, -2.0];\n", - " var indices = [2, 2, 3, 2, 3, 2, 2, 3, 2, 0, 2, 2, 2, 1, 2, 2, 2];\n", - " var classes = [[50, 50, 50], [50, 0, 0], [0, 50, 50], [0, 49, 5], [0, 47, 1], [0, 47, 0], [0, 0, 1], [0, 2, 4], [0, 0, 3], [0, 2, 1], [0, 2, 0], [0, 0, 1], [0, 1, 45], [0, 1, 2], [0, 0, 2], [0, 1, 0], [0, 0, 43]];\n", + " var thresholds = [0.800000011921, -2.0, 1.75, 4.94999980927, 1.65000009537, -2.0, -2.0, 1.54999995232, -2.0, 6.94999980927, -2.0, -2.0, 4.85000038147, 5.94999980927, -2.0, -2.0, -2.0];\n", + " var indices = [3, -2, 3, 2, 3, -2, -2, 3, -2, 0, -2, -2, 2, 0, -2, -2, -2];\n", + " var classes = [[50, 50, 50], [50, 0, 0], [0, 50, 50], [0, 49, 5], [0, 47, 1], [0, 47, 0], [0, 0, 1], [0, 2, 4], [0, 0, 3], [0, 2, 1], [0, 2, 0], [0, 0, 1], [0, 1, 45], [0, 1, 2], [0, 1, 0], [0, 0, 2], [0, 0, 43]];\n", "\n", " // Prediction:\n", " var clf = new DecisionTreeClassifier(lChilds, rChilds, thresholds, indices, classes);\n", @@ -145,11 +146,15 @@ " console.log(prediction);\n", "\n", " }\n", - "}\n" + "}\n", + "CPU times: user 904 µs, sys: 738 µs, total: 1.64 ms\n", + "Wall time: 951 µs\n" ] } ], "source": [ + "%%time\n", + "\n", "from sklearn_porter import Porter\n", "\n", "porter = Porter(clf, language='js')\n", @@ -157,6 +162,60 @@ "\n", "print(output)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Run classification in JavaScript:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Save the transpiled estimator:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "with open('DecisionTreeClassifier.js', 'w') as f:\n", + " f.write(output)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Prediction:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "%%bash\n", + "\n", + "if hash node 2/dev/null; then\n", + " node DecisionTreeClassifier.js 1 2 3 4\n", + "fi" + ] } ], "metadata": { diff --git a/examples/estimator/classifier/DecisionTreeClassifier/js/basics_embedded.ipynb b/examples/estimator/classifier/DecisionTreeClassifier/js/basics_embedded.ipynb index 6ce43403..88d9fd87 100644 --- a/examples/estimator/classifier/DecisionTreeClassifier/js/basics_embedded.ipynb +++ b/examples/estimator/classifier/DecisionTreeClassifier/js/basics_embedded.ipynb @@ -52,7 +52,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -66,7 +66,7 @@ " splitter='best')" ] }, - "execution_count": 4, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -87,7 +87,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": { "scrolled": false }, @@ -109,7 +109,7 @@ " this.predict = function(features) {\n", " var classes = new Array(3);\n", " \n", - " if (features[2] <= 2.45000004768) {\n", + " if (features[3] <= 0.800000011921) {\n", " classes[0] = 50; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", @@ -131,7 +131,7 @@ " classes[1] = 0; \n", " classes[2] = 3; \n", " } else {\n", - " if (features[0] <= 6.94999980927) {\n", + " if (features[2] <= 5.44999980927) {\n", " classes[0] = 0; \n", " classes[1] = 2; \n", " classes[2] = 0; \n", @@ -178,11 +178,15 @@ " console.log(prediction);\n", "\n", " }\n", - "}\n" + "}\n", + "CPU times: user 1.96 ms, sys: 1.02 ms, total: 2.98 ms\n", + "Wall time: 2.63 ms\n" ] } ], "source": [ + "%%time\n", + "\n", "from sklearn_porter import Porter\n", "\n", "porter = Porter(clf, language='js')\n", @@ -190,6 +194,60 @@ "\n", "print(output)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Run classification in JavaScript:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Save the transpiled estimator:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "with open('DecisionTreeClassifier.js', 'w') as f:\n", + " f.write(output)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Prediction:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "%%bash\n", + "\n", + "if hash node 2/dev/null; then\n", + " node DecisionTreeClassifier.js 1 2 3 4\n", + "fi" + ] } ], "metadata": { diff --git a/examples/estimator/classifier/ExtraTreesClassifier/js/basics.ipynb b/examples/estimator/classifier/ExtraTreesClassifier/js/basics.ipynb index f9a4b102..0cf59637 100644 --- a/examples/estimator/classifier/ExtraTreesClassifier/js/basics.ipynb +++ b/examples/estimator/classifier/ExtraTreesClassifier/js/basics.ipynb @@ -22,7 +22,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -51,7 +51,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -59,12 +59,13 @@ "text/plain": [ "ExtraTreesClassifier(bootstrap=False, class_weight=None, criterion='gini',\n", " max_depth=None, max_features='auto', max_leaf_nodes=None,\n", + " min_impurity_decrease=0.0, min_impurity_split=None,\n", " min_samples_leaf=1, min_samples_split=2,\n", " min_weight_fraction_leaf=0.0, n_estimators=15, n_jobs=1,\n", " oob_score=False, random_state=0, verbose=0, warm_start=False)" ] }, - "execution_count": 3, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -94,19 +95,29 @@ "name": "stdout", "output_type": "stream", "text": [ - "var Brain = function() {\n", + "var ExtraTreesClassifier = function() {\n", + "\n", + " var findMax = function(nums) {\n", + " var index = 0;\n", + " for (var i = 0; i < nums.length; i++) {\n", + " index = nums[i] > nums[index] ? i : index;\n", + " }\n", + " return index;\n", + " };\n", + "\n", + " var trees = new Array();\n", "\n", - " var predict_00 = function(atts) {\n", + " trees.push(function(features) {\n", " var classes = new Array(3);\n", " \n", - " if (atts[0] <= 5.5211283675173766) {\n", - " if (atts[1] <= 2.5423691467210121) {\n", - " if (atts[2] <= 2.3285940238943343) {\n", + " if (features[0] <= 5.52112836752) {\n", + " if (features[1] <= 2.54236914672) {\n", + " if (features[2] <= 2.32859402389) {\n", " classes[0] = 1; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 1.4511234333012406) {\n", + " if (features[3] <= 1.4511234333) {\n", " classes[0] = 0; \n", " classes[1] = 8; \n", " classes[2] = 0; \n", @@ -117,7 +128,7 @@ " }\n", " }\n", " } else {\n", - " if (atts[3] <= 0.83878367200965764) {\n", + " if (features[3] <= 0.83878367201) {\n", " classes[0] = 46; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", @@ -128,15 +139,15 @@ " }\n", " }\n", " } else {\n", - " if (atts[3] <= 1.8936930649824477) {\n", - " if (atts[2] <= 5.1807613674845001) {\n", - " if (atts[3] <= 0.2542593119501258) {\n", + " if (features[3] <= 1.89369306498) {\n", + " if (features[2] <= 5.18076136748) {\n", + " if (features[3] <= 0.25425931195) {\n", " classes[0] = 1; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 1.2979109919829468) {\n", - " if (atts[1] <= 3.1466755002784144) {\n", + " if (features[3] <= 1.29791099198) {\n", + " if (features[1] <= 3.14667550028) {\n", " classes[0] = 0; \n", " classes[1] = 8; \n", " classes[2] = 0; \n", @@ -146,24 +157,24 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[3] <= 1.4917742899580739) {\n", + " if (features[3] <= 1.49177428996) {\n", " classes[0] = 0; \n", " classes[1] = 17; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[2] <= 4.6486109417083465) {\n", + " if (features[2] <= 4.64861094171) {\n", " classes[0] = 0; \n", " classes[1] = 7; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 1.7802776400056488) {\n", - " if (atts[3] <= 1.5065926898187254) {\n", - " if (atts[2] <= 4.899703297413823) {\n", + " if (features[3] <= 1.78027764001) {\n", + " if (features[3] <= 1.50659268982) {\n", + " if (features[2] <= 4.89970329741) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[2] <= 4.9878533508461969) {\n", + " if (features[2] <= 4.98785335085) {\n", " classes[0] = 0; \n", " classes[1] = 2; \n", " classes[2] = 0; \n", @@ -179,7 +190,7 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[1] <= 3.1244712178243654) {\n", + " if (features[1] <= 3.12447121782) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 5; \n", @@ -204,37 +215,32 @@ " classes[2] = 34; \n", " }\n", " }\n", - " var i = 0, class_idx = 0, class_val = classes[0];\n", - " for (i = 1; i < 3; i++) {\n", - " if (classes[i] > class_val) {\n", - " class_idx = i;\n", - " class_val = classes[i];\n", - " }\n", - " }\n", - " return class_idx;\n", - " };\n", - " var predict_01 = function(atts) {\n", + " \n", + " return findMax(classes);\n", + " });\n", + " \n", + " trees.push(function(features) {\n", " var classes = new Array(3);\n", " \n", - " if (atts[3] <= 1.5504955501838129) {\n", - " if (atts[2] <= 2.5923285292922946) {\n", + " if (features[3] <= 1.55049555018) {\n", + " if (features[2] <= 2.59232852929) {\n", " classes[0] = 50; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[0] <= 5.7051897734310577) {\n", + " if (features[0] <= 5.70518977343) {\n", " classes[0] = 0; \n", " classes[1] = 21; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[1] <= 2.4211563618020882) {\n", - " if (atts[1] <= 2.2376366618358423) {\n", - " if (atts[3] <= 1.0852253511944903) {\n", + " if (features[1] <= 2.4211563618) {\n", + " if (features[1] <= 2.23763666184) {\n", + " if (features[3] <= 1.08522535119) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[2] <= 4.5587103432317777) {\n", + " if (features[2] <= 4.55871034323) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", @@ -250,24 +256,24 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[1] <= 2.8877833170885867) {\n", - " if (atts[3] <= 1.2729508337904472) {\n", + " if (features[1] <= 2.88778331709) {\n", + " if (features[3] <= 1.27295083379) {\n", " classes[0] = 0; \n", " classes[1] = 4; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[0] <= 6.5790113411443398) {\n", - " if (atts[2] <= 4.7893806273809885) {\n", + " if (features[0] <= 6.57901134114) {\n", + " if (features[2] <= 4.78938062738) {\n", " classes[0] = 0; \n", " classes[1] = 2; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[0] <= 6.1217669667164261) {\n", + " if (features[0] <= 6.12176696672) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", " } else {\n", - " if (atts[2] <= 4.9162291827513958) {\n", + " if (features[2] <= 4.91622918275) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", @@ -293,20 +299,20 @@ " }\n", " }\n", " } else {\n", - " if (atts[1] <= 3.1730407214994258) {\n", - " if (atts[3] <= 2.2706219597859412) {\n", - " if (atts[0] <= 5.7034701487511237) {\n", + " if (features[1] <= 3.1730407215) {\n", + " if (features[3] <= 2.27062195979) {\n", + " if (features[0] <= 5.70347014875) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 3; \n", " } else {\n", - " if (atts[2] <= 5.6371861017671403) {\n", - " if (atts[0] <= 6.61522923341691) {\n", - " if (atts[2] <= 5.5676840774650911) {\n", - " if (atts[1] <= 2.9151389085288946) {\n", - " if (atts[0] <= 6.3676007760812388) {\n", - " if (atts[0] <= 6.2352298441972724) {\n", - " if (atts[3] <= 1.652702599886597) {\n", + " if (features[2] <= 5.63718610177) {\n", + " if (features[0] <= 6.61522923342) {\n", + " if (features[2] <= 5.56768407747) {\n", + " if (features[1] <= 2.91513890853) {\n", + " if (features[0] <= 6.36760077608) {\n", + " if (features[0] <= 6.2352298442) {\n", + " if (features[3] <= 1.65270259989) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", @@ -336,7 +342,7 @@ " classes[2] = 3; \n", " }\n", " } else {\n", - " if (atts[0] <= 6.7180041929368821) {\n", + " if (features[0] <= 6.71800419294) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", @@ -358,9 +364,9 @@ " classes[2] = 6; \n", " }\n", " } else {\n", - " if (atts[3] <= 2.1245702483609095) {\n", - " if (atts[3] <= 1.8196133595321136) {\n", - " if (atts[0] <= 6.5204934295107302) {\n", + " if (features[3] <= 2.12457024836) {\n", + " if (features[3] <= 1.81961335953) {\n", + " if (features[0] <= 6.52049342951) {\n", " classes[0] = 0; \n", " classes[1] = 3; \n", " classes[2] = 0; \n", @@ -381,27 +387,22 @@ " }\n", " }\n", " }\n", - " var i = 0, class_idx = 0, class_val = classes[0];\n", - " for (i = 1; i < 3; i++) {\n", - " if (classes[i] > class_val) {\n", - " class_idx = i;\n", - " class_val = classes[i];\n", - " }\n", - " }\n", - " return class_idx;\n", - " };\n", - " var predict_02 = function(atts) {\n", + " \n", + " return findMax(classes);\n", + " });\n", + " \n", + " trees.push(function(features) {\n", " var classes = new Array(3);\n", " \n", - " if (atts[3] <= 1.5465266340960862) {\n", - " if (atts[2] <= 4.6071317040414321) {\n", - " if (atts[2] <= 3.3003282579766613) {\n", - " if (atts[3] <= 0.38658752666916157) {\n", + " if (features[3] <= 1.5465266341) {\n", + " if (features[2] <= 4.60713170404) {\n", + " if (features[2] <= 3.30032825798) {\n", + " if (features[3] <= 0.386587526669) {\n", " classes[0] = 41; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[2] <= 2.1556475054731816) {\n", + " if (features[2] <= 2.15564750547) {\n", " classes[0] = 9; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", @@ -417,13 +418,13 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[1] <= 2.269675323772093) {\n", + " if (features[1] <= 2.26967532377) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", " } else {\n", - " if (atts[2] <= 5.4827000154137897) {\n", - " if (atts[2] <= 4.9972260125339991) {\n", + " if (features[2] <= 5.48270001541) {\n", + " if (features[2] <= 4.99722601253) {\n", " classes[0] = 0; \n", " classes[1] = 7; \n", " classes[2] = 0; \n", @@ -440,8 +441,8 @@ " }\n", " }\n", " } else {\n", - " if (atts[3] <= 1.6032070119961934) {\n", - " if (atts[0] <= 6.5187490263266428) {\n", + " if (features[3] <= 1.603207012) {\n", + " if (features[0] <= 6.51874902633) {\n", " classes[0] = 0; \n", " classes[1] = 3; \n", " classes[2] = 0; \n", @@ -451,14 +452,14 @@ " classes[2] = 1; \n", " }\n", " } else {\n", - " if (atts[3] <= 1.8081436115623295) {\n", - " if (atts[1] <= 2.5996223526415174) {\n", + " if (features[3] <= 1.80814361156) {\n", + " if (features[1] <= 2.59962235264) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 2; \n", " } else {\n", - " if (atts[1] <= 3.0266239479308128) {\n", - " if (atts[3] <= 1.7788371596579802) {\n", + " if (features[1] <= 3.02662394793) {\n", + " if (features[3] <= 1.77883715966) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", @@ -468,8 +469,8 @@ " classes[2] = 8; \n", " }\n", " } else {\n", - " if (atts[2] <= 5.5827159596680156) {\n", - " if (atts[1] <= 3.1452745616104556) {\n", + " if (features[2] <= 5.58271595967) {\n", + " if (features[1] <= 3.14527456161) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -492,37 +493,32 @@ " }\n", " }\n", " }\n", - " var i = 0, class_idx = 0, class_val = classes[0];\n", - " for (i = 1; i < 3; i++) {\n", - " if (classes[i] > class_val) {\n", - " class_idx = i;\n", - " class_val = classes[i];\n", - " }\n", - " }\n", - " return class_idx;\n", - " };\n", - " var predict_03 = function(atts) {\n", + " \n", + " return findMax(classes);\n", + " });\n", + " \n", + " trees.push(function(features) {\n", " var classes = new Array(3);\n", " \n", - " if (atts[0] <= 5.1096510251748901) {\n", - " if (atts[3] <= 0.34237436990699954) {\n", + " if (features[0] <= 5.10965102517) {\n", + " if (features[3] <= 0.342374369907) {\n", " classes[0] = 31; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 0.42014141078495837) {\n", + " if (features[3] <= 0.420141410785) {\n", " classes[0] = 3; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[1] <= 3.3013719233224972) {\n", - " if (atts[2] <= 2.3218753875601132) {\n", + " if (features[1] <= 3.30137192332) {\n", + " if (features[2] <= 2.32187538756) {\n", " classes[0] = 1; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[0] <= 4.9582561902467441) {\n", - " if (atts[1] <= 2.4726812725055445) {\n", + " if (features[0] <= 4.95825619025) {\n", + " if (features[1] <= 2.47268127251) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", @@ -545,26 +541,26 @@ " }\n", " }\n", " } else {\n", - " if (atts[2] <= 5.0059938824178367) {\n", - " if (atts[3] <= 0.59215586323442948) {\n", + " if (features[2] <= 5.00599388242) {\n", + " if (features[3] <= 0.592155863234) {\n", " classes[0] = 14; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[2] <= 4.8869158585494921) {\n", - " if (atts[2] <= 4.5845956777760826) {\n", + " if (features[2] <= 4.88691585855) {\n", + " if (features[2] <= 4.58459567778) {\n", " classes[0] = 0; \n", " classes[1] = 32; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[1] <= 3.0013521708088704) {\n", - " if (atts[0] <= 6.2818121532768014) {\n", - " if (atts[3] <= 1.3566014997210807) {\n", + " if (features[1] <= 3.00135217081) {\n", + " if (features[0] <= 6.28181215328) {\n", + " if (features[3] <= 1.35660149972) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 1.4692521397047558) {\n", + " if (features[3] <= 1.4692521397) {\n", " classes[0] = 0; \n", " classes[1] = 2; \n", " classes[2] = 0; \n", @@ -586,22 +582,22 @@ " }\n", " }\n", " } else {\n", - " if (atts[0] <= 6.024789407414449) {\n", + " if (features[0] <= 6.02478940741) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 3; \n", " } else {\n", - " if (atts[3] <= 1.5357312864913262) {\n", + " if (features[3] <= 1.53573128649) {\n", " classes[0] = 0; \n", " classes[1] = 2; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[1] <= 2.5827080563561564) {\n", + " if (features[1] <= 2.58270805636) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", " } else {\n", - " if (atts[0] <= 6.5669079160763015) {\n", + " if (features[0] <= 6.56690791608) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 2; \n", @@ -616,9 +612,9 @@ " }\n", " }\n", " } else {\n", - " if (atts[2] <= 5.1863245386304202) {\n", - " if (atts[1] <= 2.7092082675123628) {\n", - " if (atts[3] <= 1.8776005740851462) {\n", + " if (features[2] <= 5.18632453863) {\n", + " if (features[1] <= 2.70920826751) {\n", + " if (features[3] <= 1.87760057409) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", @@ -639,24 +635,19 @@ " }\n", " }\n", " }\n", - " var i = 0, class_idx = 0, class_val = classes[0];\n", - " for (i = 1; i < 3; i++) {\n", - " if (classes[i] > class_val) {\n", - " class_idx = i;\n", - " class_val = classes[i];\n", - " }\n", - " }\n", - " return class_idx;\n", - " };\n", - " var predict_04 = function(atts) {\n", + " \n", + " return findMax(classes);\n", + " });\n", + " \n", + " trees.push(function(features) {\n", " var classes = new Array(3);\n", " \n", - " if (atts[3] <= 1.3441161325132915) {\n", - " if (atts[1] <= 3.0101406622719056) {\n", - " if (atts[0] <= 5.5513347352769937) {\n", - " if (atts[1] <= 2.4504689497176879) {\n", - " if (atts[0] <= 5.3493399647294266) {\n", - " if (atts[0] <= 4.8750152915599827) {\n", + " if (features[3] <= 1.34411613251) {\n", + " if (features[1] <= 3.01014066227) {\n", + " if (features[0] <= 5.55133473528) {\n", + " if (features[1] <= 2.45046894972) {\n", + " if (features[0] <= 5.34933996473) {\n", + " if (features[0] <= 4.87501529156) {\n", " classes[0] = 1; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", @@ -671,7 +662,7 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[1] <= 2.8545732504476669) {\n", + " if (features[1] <= 2.85457325045) {\n", " classes[0] = 0; \n", " classes[1] = 3; \n", " classes[2] = 0; \n", @@ -692,8 +683,8 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[2] <= 4.5412213265156476) {\n", - " if (atts[3] <= 1.6796973785314397) {\n", + " if (features[2] <= 4.54122132652) {\n", + " if (features[3] <= 1.67969737853) {\n", " classes[0] = 0; \n", " classes[1] = 10; \n", " classes[2] = 0; \n", @@ -703,11 +694,11 @@ " classes[2] = 1; \n", " }\n", " } else {\n", - " if (atts[3] <= 1.8681215891414575) {\n", - " if (atts[3] <= 1.7082770221162646) {\n", - " if (atts[2] <= 5.4587785419790711) {\n", - " if (atts[1] <= 2.6322218231672321) {\n", - " if (atts[1] <= 2.2075641354665572) {\n", + " if (features[3] <= 1.86812158914) {\n", + " if (features[3] <= 1.70827702212) {\n", + " if (features[2] <= 5.45877854198) {\n", + " if (features[1] <= 2.63222182317) {\n", + " if (features[1] <= 2.20756413547) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -717,15 +708,15 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[1] <= 2.9750656646611109) {\n", - " if (atts[0] <= 6.5700013628278997) {\n", - " if (atts[3] <= 1.4378231510535273) {\n", + " if (features[1] <= 2.97506566466) {\n", + " if (features[0] <= 6.57000136283) {\n", + " if (features[3] <= 1.43782315105) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 1.5344588928285092) {\n", - " if (atts[0] <= 6.46420655970892) {\n", + " if (features[3] <= 1.53445889283) {\n", + " if (features[0] <= 6.46420655971) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -757,18 +748,18 @@ " classes[2] = 2; \n", " }\n", " } else {\n", - " if (atts[1] <= 2.8407804212030414) {\n", + " if (features[1] <= 2.8407804212) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 3; \n", " } else {\n", - " if (atts[2] <= 6.150836971984635) {\n", - " if (atts[1] <= 3.1337532458575437) {\n", + " if (features[2] <= 6.15083697198) {\n", + " if (features[1] <= 3.13375324586) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 6; \n", " } else {\n", - " if (atts[2] <= 5.8292593574657765) {\n", + " if (features[2] <= 5.82925935747) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", @@ -792,23 +783,18 @@ " }\n", " }\n", " }\n", - " var i = 0, class_idx = 0, class_val = classes[0];\n", - " for (i = 1; i < 3; i++) {\n", - " if (classes[i] > class_val) {\n", - " class_idx = i;\n", - " class_val = classes[i];\n", - " }\n", - " }\n", - " return class_idx;\n", - " };\n", - " var predict_05 = function(atts) {\n", + " \n", + " return findMax(classes);\n", + " });\n", + " \n", + " trees.push(function(features) {\n", " var classes = new Array(3);\n", " \n", - " if (atts[3] <= 1.7676213332657358) {\n", - " if (atts[1] <= 3.4545610617739615) {\n", - " if (atts[2] <= 3.6502854621611109) {\n", - " if (atts[2] <= 3.1891586420402427) {\n", - " if (atts[3] <= 0.95101981363257859) {\n", + " if (features[3] <= 1.76762133327) {\n", + " if (features[1] <= 3.45456106177) {\n", + " if (features[2] <= 3.65028546216) {\n", + " if (features[2] <= 3.18915864204) {\n", + " if (features[3] <= 0.951019813633) {\n", " classes[0] = 29; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", @@ -823,19 +809,19 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[0] <= 5.1755356442982308) {\n", + " if (features[0] <= 5.1755356443) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", " } else {\n", - " if (atts[3] <= 1.4533850152404659) {\n", - " if (atts[1] <= 2.6607527474224346) {\n", - " if (atts[3] <= 1.1197721207462903) {\n", + " if (features[3] <= 1.45338501524) {\n", + " if (features[1] <= 2.66075274742) {\n", + " if (features[3] <= 1.11977212075) {\n", " classes[0] = 0; \n", " classes[1] = 4; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[2] <= 5.0340535096243002) {\n", + " if (features[2] <= 5.03405350962) {\n", " classes[0] = 0; \n", " classes[1] = 5; \n", " classes[2] = 0; \n", @@ -851,9 +837,9 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[1] <= 3.2000655596791701) {\n", - " if (atts[1] <= 2.648931143548773) {\n", - " if (atts[0] <= 6.0403355971033852) {\n", + " if (features[1] <= 3.20006555968) {\n", + " if (features[1] <= 2.64893114355) {\n", + " if (features[0] <= 6.0403355971) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -863,20 +849,20 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[0] <= 6.5033636618817878) {\n", - " if (atts[0] <= 5.6045121474787978) {\n", + " if (features[0] <= 6.50336366188) {\n", + " if (features[0] <= 5.60451214748) {\n", " classes[0] = 0; \n", " classes[1] = 2; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 1.5280856302044936) {\n", - " if (atts[0] <= 5.9853353459190419) {\n", + " if (features[3] <= 1.5280856302) {\n", + " if (features[0] <= 5.98533534592) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[1] <= 2.8357509785984978) {\n", - " if (atts[2] <= 4.663137085998029) {\n", + " if (features[1] <= 2.8357509786) {\n", + " if (features[2] <= 4.663137086) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", @@ -898,8 +884,8 @@ " }\n", " }\n", " } else {\n", - " if (atts[1] <= 3.0563716510809584) {\n", - " if (atts[3] <= 1.6545352601337104) {\n", + " if (features[1] <= 3.05637165108) {\n", + " if (features[3] <= 1.65453526013) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -929,10 +915,10 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[3] <= 1.8121969431622151) {\n", - " if (atts[2] <= 5.5628289586225659) {\n", - " if (atts[2] <= 4.8118490651150063) {\n", - " if (atts[1] <= 3.1684422458572552) {\n", + " if (features[3] <= 1.81219694316) {\n", + " if (features[2] <= 5.56282895862) {\n", + " if (features[2] <= 4.81184906512) {\n", + " if (features[1] <= 3.16844224586) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 2; \n", @@ -957,37 +943,32 @@ " classes[2] = 34; \n", " }\n", " }\n", - " var i = 0, class_idx = 0, class_val = classes[0];\n", - " for (i = 1; i < 3; i++) {\n", - " if (classes[i] > class_val) {\n", - " class_idx = i;\n", - " class_val = classes[i];\n", - " }\n", - " }\n", - " return class_idx;\n", - " };\n", - " var predict_06 = function(atts) {\n", + " \n", + " return findMax(classes);\n", + " });\n", + " \n", + " trees.push(function(features) {\n", " var classes = new Array(3);\n", " \n", - " if (atts[3] <= 1.8380720877184731) {\n", - " if (atts[3] <= 0.94908629972759717) {\n", + " if (features[3] <= 1.83807208772) {\n", + " if (features[3] <= 0.949086299728) {\n", " classes[0] = 50; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[2] <= 3.9259614145650565) {\n", + " if (features[2] <= 3.92596141457) {\n", " classes[0] = 0; \n", " classes[1] = 11; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[0] <= 5.7524397380621881) {\n", - " if (atts[0] <= 5.5675846099678861) {\n", - " if (atts[3] <= 1.3096158726185634) {\n", + " if (features[0] <= 5.75243973806) {\n", + " if (features[0] <= 5.56758460997) {\n", + " if (features[3] <= 1.30961587262) {\n", " classes[0] = 0; \n", " classes[1] = 3; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[0] <= 4.9965273461288433) {\n", + " if (features[0] <= 4.99652734613) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -1003,16 +984,16 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[3] <= 1.3226210794537889) {\n", + " if (features[3] <= 1.32262107945) {\n", " classes[0] = 0; \n", " classes[1] = 9; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 1.5331659525110946) {\n", - " if (atts[1] <= 2.9363041020633207) {\n", - " if (atts[1] <= 2.6271371465694107) {\n", - " if (atts[2] <= 5.2132714648801279) {\n", - " if (atts[0] <= 6.1450817798403774) {\n", + " if (features[3] <= 1.53316595251) {\n", + " if (features[1] <= 2.93630410206) {\n", + " if (features[1] <= 2.62713714657) {\n", + " if (features[2] <= 5.21327146488) {\n", + " if (features[0] <= 6.14508177984) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -1027,17 +1008,17 @@ " classes[2] = 1; \n", " }\n", " } else {\n", - " if (atts[0] <= 6.1049023089068344) {\n", + " if (features[0] <= 6.10490230891) {\n", " classes[0] = 0; \n", " classes[1] = 2; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 1.4576934027935942) {\n", + " if (features[3] <= 1.45769340279) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[2] <= 5.0702880434553546) {\n", + " if (features[2] <= 5.07028804346) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", @@ -1055,16 +1036,16 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[2] <= 5.1260403107316117) {\n", - " if (atts[1] <= 3.1470031032977439) {\n", - " if (atts[2] <= 4.8079453149979541) {\n", + " if (features[2] <= 5.12604031073) {\n", + " if (features[1] <= 3.1470031033) {\n", + " if (features[2] <= 4.807945315) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 2; \n", " } else {\n", - " if (atts[0] <= 6.1519032927184263) {\n", - " if (atts[0] <= 6.0430469727063381) {\n", - " if (atts[1] <= 2.9580633267166325) {\n", + " if (features[0] <= 6.15190329272) {\n", + " if (features[0] <= 6.04304697271) {\n", + " if (features[1] <= 2.95806332672) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", @@ -1079,7 +1060,7 @@ " classes[2] = 1; \n", " }\n", " } else {\n", - " if (atts[2] <= 4.9211956019572716) {\n", + " if (features[2] <= 4.92119560196) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -1110,26 +1091,21 @@ " classes[1] = 0; \n", " classes[2] = 34; \n", " }\n", - " var i = 0, class_idx = 0, class_val = classes[0];\n", - " for (i = 1; i < 3; i++) {\n", - " if (classes[i] > class_val) {\n", - " class_idx = i;\n", - " class_val = classes[i];\n", - " }\n", - " }\n", - " return class_idx;\n", - " };\n", - " var predict_07 = function(atts) {\n", + " \n", + " return findMax(classes);\n", + " });\n", + " \n", + " trees.push(function(features) {\n", " var classes = new Array(3);\n", " \n", - " if (atts[0] <= 4.8350779809500537) {\n", + " if (features[0] <= 4.83507798095) {\n", " classes[0] = 16; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[2] <= 5.0521211317065813) {\n", - " if (atts[3] <= 1.187984123199987) {\n", - " if (atts[2] <= 2.4198502829763324) {\n", + " if (features[2] <= 5.05212113171) {\n", + " if (features[3] <= 1.1879841232) {\n", + " if (features[2] <= 2.41985028298) {\n", " classes[0] = 34; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", @@ -1139,20 +1115,20 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[3] <= 1.9700979161064909) {\n", - " if (atts[3] <= 1.2103523333737463) {\n", + " if (features[3] <= 1.97009791611) {\n", + " if (features[3] <= 1.21035233337) {\n", " classes[0] = 0; \n", " classes[1] = 5; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 1.7922824027484205) {\n", - " if (atts[3] <= 1.3556012588196729) {\n", + " if (features[3] <= 1.79228240275) {\n", + " if (features[3] <= 1.35560125882) {\n", " classes[0] = 0; \n", " classes[1] = 13; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[1] <= 2.3506367699266271) {\n", - " if (atts[2] <= 4.781047702897828) {\n", + " if (features[1] <= 2.35063676993) {\n", + " if (features[2] <= 4.7810477029) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", @@ -1162,8 +1138,8 @@ " classes[2] = 1; \n", " }\n", " } else {\n", - " if (atts[0] <= 5.2396924972122507) {\n", - " if (atts[3] <= 1.540099250418558) {\n", + " if (features[0] <= 5.23969249721) {\n", + " if (features[3] <= 1.54009925042) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", @@ -1180,9 +1156,9 @@ " }\n", " }\n", " } else {\n", - " if (atts[2] <= 4.8114599180137345) {\n", - " if (atts[0] <= 6.1531159293238975) {\n", - " if (atts[0] <= 5.9178402810269528) {\n", + " if (features[2] <= 4.81145991801) {\n", + " if (features[0] <= 6.15311592932) {\n", + " if (features[0] <= 5.91784028103) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", @@ -1210,15 +1186,15 @@ " }\n", " }\n", " } else {\n", - " if (atts[3] <= 1.9630314654063374) {\n", - " if (atts[0] <= 6.3214984808907442) {\n", - " if (atts[0] <= 5.9718572619240069) {\n", + " if (features[3] <= 1.96303146541) {\n", + " if (features[0] <= 6.32149848089) {\n", + " if (features[0] <= 5.97185726192) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 3; \n", " } else {\n", - " if (atts[1] <= 2.7957859024446297) {\n", - " if (atts[3] <= 1.5647405810243964) {\n", + " if (features[1] <= 2.79578590244) {\n", + " if (features[3] <= 1.56474058102) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -1245,44 +1221,39 @@ " }\n", " }\n", " }\n", - " var i = 0, class_idx = 0, class_val = classes[0];\n", - " for (i = 1; i < 3; i++) {\n", - " if (classes[i] > class_val) {\n", - " class_idx = i;\n", - " class_val = classes[i];\n", - " }\n", - " }\n", - " return class_idx;\n", - " };\n", - " var predict_08 = function(atts) {\n", + " \n", + " return findMax(classes);\n", + " });\n", + " \n", + " trees.push(function(features) {\n", " var classes = new Array(3);\n", " \n", - " if (atts[2] <= 2.0691016397809667) {\n", + " if (features[2] <= 2.06910163978) {\n", " classes[0] = 50; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 2.2998321977443212) {\n", - " if (atts[2] <= 5.7568501553340212) {\n", - " if (atts[3] <= 1.3336353223788777) {\n", + " if (features[3] <= 2.29983219774) {\n", + " if (features[2] <= 5.75685015533) {\n", + " if (features[3] <= 1.33363532238) {\n", " classes[0] = 0; \n", " classes[1] = 28; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[1] <= 2.7958206613730536) {\n", - " if (atts[3] <= 1.6143543486045333) {\n", - " if (atts[2] <= 4.272187437395063) {\n", + " if (features[1] <= 2.79582066137) {\n", + " if (features[3] <= 1.6143543486) {\n", + " if (features[2] <= 4.2721874374) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 1.4076716455330911) {\n", + " if (features[3] <= 1.40767164553) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", " } else {\n", - " if (atts[1] <= 2.4055632554020563) {\n", - " if (atts[0] <= 6.1686928410480846) {\n", + " if (features[1] <= 2.4055632554) {\n", + " if (features[0] <= 6.16869284105) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -1304,14 +1275,14 @@ " classes[2] = 7; \n", " }\n", " } else {\n", - " if (atts[3] <= 2.0267573938538184) {\n", - " if (atts[3] <= 1.6093679712505249) {\n", - " if (atts[2] <= 4.6346452780257099) {\n", + " if (features[3] <= 2.02675739385) {\n", + " if (features[3] <= 1.60936797125) {\n", + " if (features[2] <= 4.63464527803) {\n", " classes[0] = 0; \n", " classes[1] = 10; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[2] <= 4.9520732824288594) {\n", + " if (features[2] <= 4.95207328243) {\n", " classes[0] = 0; \n", " classes[1] = 6; \n", " classes[2] = 0; \n", @@ -1322,18 +1293,18 @@ " }\n", " }\n", " } else {\n", - " if (atts[1] <= 2.8720874155782443) {\n", + " if (features[1] <= 2.87208741558) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 2; \n", " } else {\n", - " if (atts[0] <= 6.5173975475393169) {\n", - " if (atts[1] <= 3.1451728271597688) {\n", + " if (features[0] <= 6.51739754754) {\n", + " if (features[1] <= 3.14517282716) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 7; \n", " } else {\n", - " if (atts[0] <= 6.1052098155768029) {\n", + " if (features[0] <= 6.10520981558) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", @@ -1368,57 +1339,52 @@ " classes[2] = 14; \n", " }\n", " }\n", - " var i = 0, class_idx = 0, class_val = classes[0];\n", - " for (i = 1; i < 3; i++) {\n", - " if (classes[i] > class_val) {\n", - " class_idx = i;\n", - " class_val = classes[i];\n", - " }\n", - " }\n", - " return class_idx;\n", - " };\n", - " var predict_09 = function(atts) {\n", + " \n", + " return findMax(classes);\n", + " });\n", + " \n", + " trees.push(function(features) {\n", " var classes = new Array(3);\n", " \n", - " if (atts[2] <= 1.7984751272085284) {\n", + " if (features[2] <= 1.79847512721) {\n", " classes[0] = 48; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[2] <= 2.743614525085631) {\n", + " if (features[2] <= 2.74361452509) {\n", " classes[0] = 2; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 1.5726279935672078) {\n", - " if (atts[1] <= 2.8580128675676453) {\n", - " if (atts[3] <= 1.0071725081685803) {\n", + " if (features[3] <= 1.57262799357) {\n", + " if (features[1] <= 2.85801286757) {\n", + " if (features[3] <= 1.00717250817) {\n", " classes[0] = 0; \n", " classes[1] = 7; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 1.4508596346903717) {\n", - " if (atts[0] <= 5.4788470711552755) {\n", + " if (features[3] <= 1.45085963469) {\n", + " if (features[0] <= 5.47884707116) {\n", " classes[0] = 0; \n", " classes[1] = 2; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[2] <= 4.3105326892188245) {\n", + " if (features[2] <= 4.31053268922) {\n", " classes[0] = 0; \n", " classes[1] = 9; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 1.2510118285538396) {\n", + " if (features[3] <= 1.25101182855) {\n", " classes[0] = 0; \n", " classes[1] = 2; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 1.3008071595868549) {\n", + " if (features[3] <= 1.30080715959) {\n", " classes[0] = 0; \n", " classes[1] = 2; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[1] <= 2.6182927931585578) {\n", + " if (features[1] <= 2.61829279316) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -1432,8 +1398,8 @@ " }\n", " }\n", " } else {\n", - " if (atts[2] <= 5.0329756375451549) {\n", - " if (atts[2] <= 4.9472819976263134) {\n", + " if (features[2] <= 5.03297563755) {\n", + " if (features[2] <= 4.94728199763) {\n", " classes[0] = 0; \n", " classes[1] = 3; \n", " classes[2] = 0; \n", @@ -1455,10 +1421,10 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[2] <= 6.1966904607047031) {\n", - " if (atts[2] <= 5.7145493422412432) {\n", - " if (atts[3] <= 1.7483953438728816) {\n", - " if (atts[0] <= 5.1244250369354614) {\n", + " if (features[2] <= 6.1966904607) {\n", + " if (features[2] <= 5.71454934224) {\n", + " if (features[3] <= 1.74839534387) {\n", + " if (features[0] <= 5.12442503694) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -1468,13 +1434,13 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[0] <= 5.9635245683145435) {\n", - " if (atts[1] <= 2.9403134736939704) {\n", + " if (features[0] <= 5.96352456831) {\n", + " if (features[1] <= 2.94031347369) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 5; \n", " } else {\n", - " if (atts[1] <= 3.1597595674715184) {\n", + " if (features[1] <= 3.15975956747) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -1503,21 +1469,16 @@ " }\n", " }\n", " }\n", - " var i = 0, class_idx = 0, class_val = classes[0];\n", - " for (i = 1; i < 3; i++) {\n", - " if (classes[i] > class_val) {\n", - " class_idx = i;\n", - " class_val = classes[i];\n", - " }\n", - " }\n", - " return class_idx;\n", - " };\n", - " var predict_10 = function(atts) {\n", + " \n", + " return findMax(classes);\n", + " });\n", + " \n", + " trees.push(function(features) {\n", " var classes = new Array(3);\n", " \n", - " if (atts[0] <= 4.9792803198265787) {\n", - " if (atts[1] <= 2.4799516418973426) {\n", - " if (atts[2] <= 2.1081189544909256) {\n", + " if (features[0] <= 4.97928031983) {\n", + " if (features[1] <= 2.4799516419) {\n", + " if (features[2] <= 2.10811895449) {\n", " classes[0] = 1; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", @@ -1527,7 +1488,7 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[2] <= 2.3031890773229251) {\n", + " if (features[2] <= 2.30318907732) {\n", " classes[0] = 19; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", @@ -1538,16 +1499,16 @@ " }\n", " }\n", " } else {\n", - " if (atts[2] <= 4.0566901295803843) {\n", - " if (atts[2] <= 3.864595594637342) {\n", - " if (atts[1] <= 2.4493558386314596) {\n", + " if (features[2] <= 4.05669012958) {\n", + " if (features[2] <= 3.86459559464) {\n", + " if (features[1] <= 2.44935583863) {\n", " classes[0] = 0; \n", " classes[1] = 4; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[1] <= 3.1013702094660816) {\n", - " if (atts[0] <= 5.4182674136813782) {\n", - " if (atts[1] <= 2.7351736650965983) {\n", + " if (features[1] <= 3.10137020947) {\n", + " if (features[0] <= 5.41826741368) {\n", + " if (features[1] <= 2.7351736651) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", @@ -1573,31 +1534,31 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[3] <= 2.4500323361484484) {\n", - " if (atts[0] <= 7.2381296350332578) {\n", - " if (atts[2] <= 4.9855890272850028) {\n", - " if (atts[3] <= 1.4175185078836599) {\n", + " if (features[3] <= 2.45003233615) {\n", + " if (features[0] <= 7.23812963503) {\n", + " if (features[2] <= 4.98558902729) {\n", + " if (features[3] <= 1.41751850788) {\n", " classes[0] = 0; \n", " classes[1] = 19; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 1.9085482831152847) {\n", - " if (atts[3] <= 1.5077226825858063) {\n", + " if (features[3] <= 1.90854828312) {\n", + " if (features[3] <= 1.50772268259) {\n", " classes[0] = 0; \n", " classes[1] = 10; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[1] <= 2.8843708444178024) {\n", + " if (features[1] <= 2.88437084442) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 2; \n", " } else {\n", - " if (atts[3] <= 1.7559926563657857) {\n", + " if (features[3] <= 1.75599265637) {\n", " classes[0] = 0; \n", " classes[1] = 2; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[1] <= 3.0877764696391852) {\n", + " if (features[1] <= 3.08777646964) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 2; \n", @@ -1616,10 +1577,10 @@ " }\n", " }\n", " } else {\n", - " if (atts[3] <= 1.9978145065895823) {\n", - " if (atts[3] <= 1.7865760346812085) {\n", - " if (atts[2] <= 5.0311129249450408) {\n", - " if (atts[0] <= 6.33527863763707) {\n", + " if (features[3] <= 1.99781450659) {\n", + " if (features[3] <= 1.78657603468) {\n", + " if (features[2] <= 5.03111292495) {\n", + " if (features[0] <= 6.33527863764) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -1629,12 +1590,12 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[3] <= 1.5068336243767517) {\n", + " if (features[3] <= 1.50683362438) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 2; \n", " } else {\n", - " if (atts[2] <= 5.7874281257170956) {\n", + " if (features[2] <= 5.78742812572) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", @@ -1668,28 +1629,23 @@ " }\n", " }\n", " }\n", - " var i = 0, class_idx = 0, class_val = classes[0];\n", - " for (i = 1; i < 3; i++) {\n", - " if (classes[i] > class_val) {\n", - " class_idx = i;\n", - " class_val = classes[i];\n", - " }\n", - " }\n", - " return class_idx;\n", - " };\n", - " var predict_11 = function(atts) {\n", + " \n", + " return findMax(classes);\n", + " });\n", + " \n", + " trees.push(function(features) {\n", " var classes = new Array(3);\n", " \n", - " if (atts[1] <= 3.1769571393777749) {\n", - " if (atts[3] <= 0.65371013474073503) {\n", + " if (features[1] <= 3.17695713938) {\n", + " if (features[3] <= 0.653710134741) {\n", " classes[0] = 13; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 1.8469214377642704) {\n", - " if (atts[2] <= 4.7208371488439003) {\n", - " if (atts[0] <= 5.7627729686766793) {\n", - " if (atts[3] <= 1.6360344565374696) {\n", + " if (features[3] <= 1.84692143776) {\n", + " if (features[2] <= 4.72083714884) {\n", + " if (features[0] <= 5.76277296868) {\n", + " if (features[3] <= 1.63603445654) {\n", " classes[0] = 0; \n", " classes[1] = 21; \n", " classes[2] = 0; \n", @@ -1704,17 +1660,17 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[3] <= 1.7208344558505428) {\n", - " if (atts[2] <= 4.9971796261133532) {\n", + " if (features[3] <= 1.72083445585) {\n", + " if (features[2] <= 4.99717962611) {\n", " classes[0] = 0; \n", " classes[1] = 3; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[0] <= 7.1981562798039125) {\n", - " if (atts[2] <= 5.257435716402731) {\n", - " if (atts[0] <= 6.4367447385930507) {\n", - " if (atts[0] <= 6.2970719060390206) {\n", - " if (atts[3] <= 1.5681733276103198) {\n", + " if (features[0] <= 7.1981562798) {\n", + " if (features[2] <= 5.2574357164) {\n", + " if (features[0] <= 6.43674473859) {\n", + " if (features[0] <= 6.29707190604) {\n", + " if (features[3] <= 1.56817332761) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -1757,14 +1713,14 @@ " }\n", " }\n", " } else {\n", - " if (atts[2] <= 3.8047750384801153) {\n", + " if (features[2] <= 3.80477503848) {\n", " classes[0] = 37; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 2.1793218211052987) {\n", - " if (atts[2] <= 5.2771791684203357) {\n", - " if (atts[3] <= 1.8542162920413572) {\n", + " if (features[3] <= 2.17932182111) {\n", + " if (features[2] <= 5.27717916842) {\n", + " if (features[3] <= 1.85421629204) {\n", " classes[0] = 0; \n", " classes[1] = 5; \n", " classes[2] = 0; \n", @@ -1785,23 +1741,18 @@ " }\n", " }\n", " }\n", - " var i = 0, class_idx = 0, class_val = classes[0];\n", - " for (i = 1; i < 3; i++) {\n", - " if (classes[i] > class_val) {\n", - " class_idx = i;\n", - " class_val = classes[i];\n", - " }\n", - " }\n", - " return class_idx;\n", - " };\n", - " var predict_12 = function(atts) {\n", + " \n", + " return findMax(classes);\n", + " });\n", + " \n", + " trees.push(function(features) {\n", " var classes = new Array(3);\n", " \n", - " if (atts[3] <= 1.7021128663169045) {\n", - " if (atts[1] <= 2.9849618599913255) {\n", - " if (atts[3] <= 1.4970948166731561) {\n", - " if (atts[0] <= 5.6231451775870234) {\n", - " if (atts[0] <= 4.7790295218134444) {\n", + " if (features[3] <= 1.70211286632) {\n", + " if (features[1] <= 2.98496185999) {\n", + " if (features[3] <= 1.49709481667) {\n", + " if (features[0] <= 5.62314517759) {\n", + " if (features[0] <= 4.77902952181) {\n", " classes[0] = 2; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", @@ -1811,12 +1762,12 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[3] <= 1.3284118253233357) {\n", + " if (features[3] <= 1.32841182532) {\n", " classes[0] = 0; \n", " classes[1] = 14; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[1] <= 2.7629100232801327) {\n", + " if (features[1] <= 2.76291002328) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -1828,15 +1779,15 @@ " }\n", " }\n", " } else {\n", - " if (atts[3] <= 1.6376492357878205) {\n", - " if (atts[2] <= 4.602142482827186) {\n", + " if (features[3] <= 1.63764923579) {\n", + " if (features[2] <= 4.60214248283) {\n", " classes[0] = 0; \n", " classes[1] = 3; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[1] <= 2.7219560941499337) {\n", - " if (atts[0] <= 6.0880929809363096) {\n", - " if (atts[3] <= 1.5437156698788206) {\n", + " if (features[1] <= 2.72195609415) {\n", + " if (features[0] <= 6.08809298094) {\n", + " if (features[3] <= 1.54371566988) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -1863,12 +1814,12 @@ " }\n", " }\n", " } else {\n", - " if (atts[3] <= 0.65664750033467612) {\n", + " if (features[3] <= 0.656647500335) {\n", " classes[0] = 48; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[2] <= 5.3797694299386132) {\n", + " if (features[2] <= 5.37976942994) {\n", " classes[0] = 0; \n", " classes[1] = 15; \n", " classes[2] = 0; \n", @@ -1880,10 +1831,10 @@ " }\n", " }\n", " } else {\n", - " if (atts[2] <= 5.2961110038745645) {\n", - " if (atts[2] <= 4.8192148664349324) {\n", - " if (atts[0] <= 6.0796086876843409) {\n", - " if (atts[1] <= 3.1799571540756144) {\n", + " if (features[2] <= 5.29611100387) {\n", + " if (features[2] <= 4.81921486643) {\n", + " if (features[0] <= 6.07960868768) {\n", + " if (features[1] <= 3.17995715408) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -1908,27 +1859,22 @@ " classes[2] = 30; \n", " }\n", " }\n", - " var i = 0, class_idx = 0, class_val = classes[0];\n", - " for (i = 1; i < 3; i++) {\n", - " if (classes[i] > class_val) {\n", - " class_idx = i;\n", - " class_val = classes[i];\n", - " }\n", - " }\n", - " return class_idx;\n", - " };\n", - " var predict_13 = function(atts) {\n", + " \n", + " return findMax(classes);\n", + " });\n", + " \n", + " trees.push(function(features) {\n", " var classes = new Array(3);\n", " \n", - " if (atts[0] <= 7.5141141980731554) {\n", - " if (atts[1] <= 2.7153722801061679) {\n", - " if (atts[0] <= 5.0467466254497211) {\n", - " if (atts[0] <= 4.7200417810678674) {\n", + " if (features[0] <= 7.51411419807) {\n", + " if (features[1] <= 2.71537228011) {\n", + " if (features[0] <= 5.04674662545) {\n", + " if (features[0] <= 4.72004178107) {\n", " classes[0] = 1; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[2] <= 3.8068991717706888) {\n", + " if (features[2] <= 3.80689917177) {\n", " classes[0] = 0; \n", " classes[1] = 3; \n", " classes[2] = 0; \n", @@ -1939,13 +1885,13 @@ " }\n", " }\n", " } else {\n", - " if (atts[2] <= 4.9043062884719539) {\n", - " if (atts[2] <= 4.6235755435786769) {\n", + " if (features[2] <= 4.90430628847) {\n", + " if (features[2] <= 4.62357554358) {\n", " classes[0] = 0; \n", " classes[1] = 16; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 1.7869205517467841) {\n", + " if (features[3] <= 1.78692055175) {\n", " classes[0] = 0; \n", " classes[1] = 1; \n", " classes[2] = 0; \n", @@ -1956,13 +1902,13 @@ " }\n", " }\n", " } else {\n", - " if (atts[0] <= 5.9034846845099169) {\n", + " if (features[0] <= 5.90348468451) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 3; \n", " } else {\n", - " if (atts[0] <= 6.0069964788449255) {\n", - " if (atts[2] <= 5.092356647339539) {\n", + " if (features[0] <= 6.00699647884) {\n", + " if (features[2] <= 5.09235664734) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -1980,8 +1926,8 @@ " }\n", " }\n", " } else {\n", - " if (atts[2] <= 3.9028751327869577) {\n", - " if (atts[3] <= 0.92942217227084012) {\n", + " if (features[2] <= 3.90287513279) {\n", + " if (features[3] <= 0.929422172271) {\n", " classes[0] = 49; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", @@ -1991,18 +1937,18 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[3] <= 1.95141861134337) {\n", - " if (atts[2] <= 5.9202819502434796) {\n", - " if (atts[1] <= 3.0337596705736161) {\n", - " if (atts[1] <= 2.9515778790039278) {\n", - " if (atts[3] <= 1.565131126685861) {\n", - " if (atts[3] <= 1.4106022275635259) {\n", + " if (features[3] <= 1.95141861134) {\n", + " if (features[2] <= 5.92028195024) {\n", + " if (features[1] <= 3.03375967057) {\n", + " if (features[1] <= 2.951577879) {\n", + " if (features[3] <= 1.56513112669) {\n", + " if (features[3] <= 1.41060222756) {\n", " classes[0] = 0; \n", " classes[1] = 10; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[1] <= 2.8239691897096861) {\n", - " if (atts[0] <= 6.3740416107871889) {\n", + " if (features[1] <= 2.82396918971) {\n", + " if (features[0] <= 6.37404161079) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -2023,8 +1969,8 @@ " classes[2] = 2; \n", " }\n", " } else {\n", - " if (atts[3] <= 1.7349329909051114) {\n", - " if (atts[2] <= 5.5073889949611754) {\n", + " if (features[3] <= 1.73493299091) {\n", + " if (features[2] <= 5.50738899496) {\n", " classes[0] = 0; \n", " classes[1] = 8; \n", " classes[2] = 0; \n", @@ -2040,9 +1986,9 @@ " }\n", " }\n", " } else {\n", - " if (atts[1] <= 3.2667907396292932) {\n", - " if (atts[0] <= 6.8510331900259231) {\n", - " if (atts[2] <= 5.3080841532160195) {\n", + " if (features[1] <= 3.26679073963) {\n", + " if (features[0] <= 6.85103319003) {\n", + " if (features[2] <= 5.30808415322) {\n", " classes[0] = 0; \n", " classes[1] = 4; \n", " classes[2] = 0; \n", @@ -2079,25 +2025,20 @@ " classes[1] = 0; \n", " classes[2] = 6; \n", " }\n", - " var i = 0, class_idx = 0, class_val = classes[0];\n", - " for (i = 1; i < 3; i++) {\n", - " if (classes[i] > class_val) {\n", - " class_idx = i;\n", - " class_val = classes[i];\n", - " }\n", - " }\n", - " return class_idx;\n", - " };\n", - " var predict_14 = function(atts) {\n", + " \n", + " return findMax(classes);\n", + " });\n", + " \n", + " trees.push(function(features) {\n", " var classes = new Array(3);\n", " \n", - " if (atts[2] <= 1.5213681480470247) {\n", + " if (features[2] <= 1.52136814805) {\n", " classes[0] = 37; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[2] <= 3.3733067152396501) {\n", - " if (atts[2] <= 1.928483210061239) {\n", + " if (features[2] <= 3.37330671524) {\n", + " if (features[2] <= 1.92848321006) {\n", " classes[0] = 13; \n", " classes[1] = 0; \n", " classes[2] = 0; \n", @@ -2107,19 +2048,19 @@ " classes[2] = 0; \n", " }\n", " } else {\n", - " if (atts[3] <= 1.4203844512442054) {\n", - " if (atts[0] <= 5.2718002974390057) {\n", + " if (features[3] <= 1.42038445124) {\n", + " if (features[0] <= 5.27180029744) {\n", " classes[0] = 0; \n", " classes[1] = 2; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[3] <= 1.3984442803066099) {\n", + " if (features[3] <= 1.39844428031) {\n", " classes[0] = 0; \n", " classes[1] = 24; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[1] <= 2.815819690150918) {\n", - " if (atts[0] <= 6.5752606285820789) {\n", + " if (features[1] <= 2.81581969015) {\n", + " if (features[0] <= 6.57526062858) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -2136,9 +2077,9 @@ " }\n", " }\n", " } else {\n", - " if (atts[2] <= 5.0733232229961533) {\n", - " if (atts[2] <= 4.7277283044181084) {\n", - " if (atts[3] <= 1.6830983078709849) {\n", + " if (features[2] <= 5.073323223) {\n", + " if (features[2] <= 4.72772830442) {\n", + " if (features[3] <= 1.68309830787) {\n", " classes[0] = 0; \n", " classes[1] = 10; \n", " classes[2] = 0; \n", @@ -2148,13 +2089,13 @@ " classes[2] = 1; \n", " }\n", " } else {\n", - " if (atts[3] <= 1.7955400567946675) {\n", - " if (atts[2] <= 4.9226321104784594) {\n", + " if (features[3] <= 1.79554005679) {\n", + " if (features[2] <= 4.92263211048) {\n", " classes[0] = 0; \n", " classes[1] = 2; \n", " classes[2] = 0; \n", " } else {\n", - " if (atts[1] <= 2.5475161659836072) {\n", + " if (features[1] <= 2.54751616598) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -2165,7 +2106,7 @@ " }\n", " }\n", " } else {\n", - " if (atts[1] <= 3.0302843343947554) {\n", + " if (features[1] <= 3.03028433439) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 7; \n", @@ -2177,10 +2118,10 @@ " }\n", " }\n", " } else {\n", - " if (atts[1] <= 3.110966564102843) {\n", - " if (atts[3] <= 1.8737745337319733) {\n", - " if (atts[0] <= 6.1758400547445209) {\n", - " if (atts[0] <= 5.9332085107091395) {\n", + " if (features[1] <= 3.1109665641) {\n", + " if (features[3] <= 1.87377453373) {\n", + " if (features[0] <= 6.17584005474) {\n", + " if (features[0] <= 5.93320851071) {\n", " classes[0] = 0; \n", " classes[1] = 0; \n", " classes[2] = 1; \n", @@ -2208,68 +2149,100 @@ " }\n", " }\n", " }\n", - " var i = 0, class_idx = 0, class_val = classes[0];\n", - " for (i = 1; i < 3; i++) {\n", - " if (classes[i] > class_val) {\n", - " class_idx = i;\n", - " class_val = classes[i];\n", - " }\n", - " }\n", - " return class_idx;\n", - " };\n", - " \n", - " this.predict = function(atts) {\n", - " var i = 0, n_classes = 3;\n", - " var classes = new Array(n_classes);\n", - " for (i = 0; i < n_classes; i++) {\n", - " classes[i] = 0;\n", - " }\n", " \n", - " classes[predict_00(atts)]++;\n", - " classes[predict_01(atts)]++;\n", - " classes[predict_02(atts)]++;\n", - " classes[predict_03(atts)]++;\n", - " classes[predict_04(atts)]++;\n", - " classes[predict_05(atts)]++;\n", - " classes[predict_06(atts)]++;\n", - " classes[predict_07(atts)]++;\n", - " classes[predict_08(atts)]++;\n", - " classes[predict_09(atts)]++;\n", - " classes[predict_10(atts)]++;\n", - " classes[predict_11(atts)]++;\n", - " classes[predict_12(atts)]++;\n", - " classes[predict_13(atts)]++;\n", - " classes[predict_14(atts)]++;\n", + " return findMax(classes);\n", + " });\n", " \n", - " var class_idx = 0, class_val = classes[0];\n", - " for (i = 1; i < n_classes; i++) {\n", - " if (classes[i] > class_val) {\n", - " class_idx = i;\n", - " class_val = classes[i];\n", - " }\n", + " this.predict = function(features) {\n", + " var classes = new Array(3).fill(0);\n", + " for (var i = 0; i < trees.length; i++) {\n", + " classes[trees[i](features)]++;\n", " }\n", - " return class_idx;\n", + " return findMax(classes);\n", " }\n", "\n", "};\n", "\n", "if (typeof process !== 'undefined' && typeof process.argv !== 'undefined') {\n", " if (process.argv.length - 2 == 4) {\n", - " var argv = process.argv.slice(2);\n", - " var prediction = new Brain().predict(argv);\n", + "\n", + " // Features:\n", + " var features = process.argv.slice(2);\n", + "\n", + " // Prediction:\n", + " var prediction = new ExtraTreesClassifier().predict(features);\n", " console.log(prediction);\n", + "\n", " }\n", - "}\n" + "}\n", + "CPU times: user 18.7 ms, sys: 2.74 ms, total: 21.5 ms\n", + "Wall time: 19.7 ms\n" ] } ], "source": [ + "%%time\n", + "\n", "from sklearn_porter import Porter\n", "\n", "output = Porter(clf, language='js').export()\n", "\n", "print output" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Run classification in JavaScript:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Save the transpiled estimator:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "with open('ExtraTreesClassifier.js', 'w') as f:\n", + " f.write(output)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Prediction:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "%%bash\n", + "\n", + "if hash node 2/dev/null; then\n", + " node ExtraTreesClassifier.js 1 2 3 4\n", + "fi" + ] } ], "metadata": { @@ -2288,7 +2261,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.12" + "version": "2.7.13" } }, "nbformat": 4, diff --git a/examples/estimator/classifier/GaussianNB/js/basics.ipynb b/examples/estimator/classifier/GaussianNB/js/basics.ipynb index 4b20504e..62a9986c 100644 --- a/examples/estimator/classifier/GaussianNB/js/basics.ipynb +++ b/examples/estimator/classifier/GaussianNB/js/basics.ipynb @@ -82,7 +82,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "metadata": { "scrolled": false }, @@ -130,9 +130,9 @@ " var features = process.argv.slice(2);\n", "\n", " // Parameters:\n", - " var priors = [0.33333333333333331, 0.33333333333333331, 0.33333333333333331];\n", - " var sigmas = [[0.12176400309242481, 0.14227600309242491, 0.029504003092424898, 0.011264003092424885], [0.26110400309242499, 0.096500003092424902, 0.21640000309242502, 0.038324003092424869], [0.39625600309242481, 0.10192400309242496, 0.29849600309242508, 0.073924003092424875]];\n", - " var thetas = [[5.0059999999999993, 3.4180000000000006, 1.464, 0.24399999999999991], [5.9359999999999999, 2.7700000000000005, 4.2599999999999998, 1.3259999999999998], [6.5879999999999983, 2.9739999999999998, 5.5519999999999996, 2.0259999999999998]];\n", + " var priors = [0.333333333333, 0.333333333333, 0.333333333333];\n", + " var sigmas = [[0.121764003092, 0.142276003092, 0.0295040030924, 0.0112640030924], [0.261104003092, 0.0965000030924, 0.216400003092, 0.0383240030924], [0.396256003092, 0.101924003092, 0.298496003092, 0.0739240030924]];\n", + " var thetas = [[5.006, 3.418, 1.464, 0.244], [5.936, 2.77, 4.26, 1.326], [6.588, 2.974, 5.552, 2.026]];\n", "\n", " // Estimator:\n", " var clf = new GaussianNB(priors, sigmas, thetas);\n", @@ -140,11 +140,15 @@ " console.log(prediction);\n", "\n", " }\n", - "}\n" + "}\n", + "CPU times: user 915 µs, sys: 831 µs, total: 1.75 ms\n", + "Wall time: 984 µs\n" ] } ], "source": [ + "%%time\n", + "\n", "from sklearn_porter import Porter\n", "\n", "porter = Porter(clf, language='js')\n", @@ -152,6 +156,60 @@ "\n", "print(output)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Run classification in JavaScript:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Save the transpiled estimator:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "with open('GaussianNB.js', 'w') as f:\n", + " f.write(output)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Prediction:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "%%bash\n", + "\n", + "if hash node 2/dev/null; then\n", + " node GaussianNB.js 1 2 3 4\n", + "fi" + ] } ], "metadata": { diff --git a/examples/estimator/classifier/KNeighborsClassifier/js/basics.ipynb b/examples/estimator/classifier/KNeighborsClassifier/js/basics.ipynb index d70d747e..64845a11 100644 --- a/examples/estimator/classifier/KNeighborsClassifier/js/basics.ipynb +++ b/examples/estimator/classifier/KNeighborsClassifier/js/basics.ipynb @@ -86,7 +86,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { "scrolled": false }, @@ -175,20 +175,26 @@ " var features = process.argv.slice(2);\n", "\n", " // Parameters:\n", - " var X = [[5.0999999999999996, 3.5, 1.3999999999999999, 0.20000000000000001], [4.9000000000000004, 3.0, 1.3999999999999999, 0.20000000000000001], [4.7000000000000002, 3.2000000000000002, 1.3, 0.20000000000000001], [4.5999999999999996, 3.1000000000000001, 1.5, 0.20000000000000001], [5.0, 3.6000000000000001, 1.3999999999999999, 0.20000000000000001], [5.4000000000000004, 3.8999999999999999, 1.7, 0.40000000000000002], [4.5999999999999996, 3.3999999999999999, 1.3999999999999999, 0.29999999999999999], [5.0, 3.3999999999999999, 1.5, 0.20000000000000001], [4.4000000000000004, 2.8999999999999999, 1.3999999999999999, 0.20000000000000001], [4.9000000000000004, 3.1000000000000001, 1.5, 0.10000000000000001], [5.4000000000000004, 3.7000000000000002, 1.5, 0.20000000000000001], [4.7999999999999998, 3.3999999999999999, 1.6000000000000001, 0.20000000000000001], [4.7999999999999998, 3.0, 1.3999999999999999, 0.10000000000000001], [4.2999999999999998, 3.0, 1.1000000000000001, 0.10000000000000001], [5.7999999999999998, 4.0, 1.2, 0.20000000000000001], [5.7000000000000002, 4.4000000000000004, 1.5, 0.40000000000000002], [5.4000000000000004, 3.8999999999999999, 1.3, 0.40000000000000002], [5.0999999999999996, 3.5, 1.3999999999999999, 0.29999999999999999], [5.7000000000000002, 3.7999999999999998, 1.7, 0.29999999999999999], [5.0999999999999996, 3.7999999999999998, 1.5, 0.29999999999999999], [5.4000000000000004, 3.3999999999999999, 1.7, 0.20000000000000001], [5.0999999999999996, 3.7000000000000002, 1.5, 0.40000000000000002], [4.5999999999999996, 3.6000000000000001, 1.0, 0.20000000000000001], [5.0999999999999996, 3.2999999999999998, 1.7, 0.5], [4.7999999999999998, 3.3999999999999999, 1.8999999999999999, 0.20000000000000001], [5.0, 3.0, 1.6000000000000001, 0.20000000000000001], [5.0, 3.3999999999999999, 1.6000000000000001, 0.40000000000000002], [5.2000000000000002, 3.5, 1.5, 0.20000000000000001], [5.2000000000000002, 3.3999999999999999, 1.3999999999999999, 0.20000000000000001], [4.7000000000000002, 3.2000000000000002, 1.6000000000000001, 0.20000000000000001], [4.7999999999999998, 3.1000000000000001, 1.6000000000000001, 0.20000000000000001], [5.4000000000000004, 3.3999999999999999, 1.5, 0.40000000000000002], [5.2000000000000002, 4.0999999999999996, 1.5, 0.10000000000000001], [5.5, 4.2000000000000002, 1.3999999999999999, 0.20000000000000001], [4.9000000000000004, 3.1000000000000001, 1.5, 0.10000000000000001], [5.0, 3.2000000000000002, 1.2, 0.20000000000000001], [5.5, 3.5, 1.3, 0.20000000000000001], [4.9000000000000004, 3.1000000000000001, 1.5, 0.10000000000000001], [4.4000000000000004, 3.0, 1.3, 0.20000000000000001], [5.0999999999999996, 3.3999999999999999, 1.5, 0.20000000000000001], [5.0, 3.5, 1.3, 0.29999999999999999], [4.5, 2.2999999999999998, 1.3, 0.29999999999999999], [4.4000000000000004, 3.2000000000000002, 1.3, 0.20000000000000001], [5.0, 3.5, 1.6000000000000001, 0.59999999999999998], [5.0999999999999996, 3.7999999999999998, 1.8999999999999999, 0.40000000000000002], [4.7999999999999998, 3.0, 1.3999999999999999, 0.29999999999999999], [5.0999999999999996, 3.7999999999999998, 1.6000000000000001, 0.20000000000000001], [4.5999999999999996, 3.2000000000000002, 1.3999999999999999, 0.20000000000000001], [5.2999999999999998, 3.7000000000000002, 1.5, 0.20000000000000001], [5.0, 3.2999999999999998, 1.3999999999999999, 0.20000000000000001], [7.0, 3.2000000000000002, 4.7000000000000002, 1.3999999999999999], [6.4000000000000004, 3.2000000000000002, 4.5, 1.5], [6.9000000000000004, 3.1000000000000001, 4.9000000000000004, 1.5], [5.5, 2.2999999999999998, 4.0, 1.3], [6.5, 2.7999999999999998, 4.5999999999999996, 1.5], [5.7000000000000002, 2.7999999999999998, 4.5, 1.3], [6.2999999999999998, 3.2999999999999998, 4.7000000000000002, 1.6000000000000001], [4.9000000000000004, 2.3999999999999999, 3.2999999999999998, 1.0], [6.5999999999999996, 2.8999999999999999, 4.5999999999999996, 1.3], [5.2000000000000002, 2.7000000000000002, 3.8999999999999999, 1.3999999999999999], [5.0, 2.0, 3.5, 1.0], [5.9000000000000004, 3.0, 4.2000000000000002, 1.5], [6.0, 2.2000000000000002, 4.0, 1.0], [6.0999999999999996, 2.8999999999999999, 4.7000000000000002, 1.3999999999999999], [5.5999999999999996, 2.8999999999999999, 3.6000000000000001, 1.3], [6.7000000000000002, 3.1000000000000001, 4.4000000000000004, 1.3999999999999999], [5.5999999999999996, 3.0, 4.5, 1.5], [5.7999999999999998, 2.7000000000000002, 4.0999999999999996, 1.0], [6.2000000000000002, 2.2000000000000002, 4.5, 1.5], [5.5999999999999996, 2.5, 3.8999999999999999, 1.1000000000000001], [5.9000000000000004, 3.2000000000000002, 4.7999999999999998, 1.8], [6.0999999999999996, 2.7999999999999998, 4.0, 1.3], [6.2999999999999998, 2.5, 4.9000000000000004, 1.5], [6.0999999999999996, 2.7999999999999998, 4.7000000000000002, 1.2], [6.4000000000000004, 2.8999999999999999, 4.2999999999999998, 1.3], [6.5999999999999996, 3.0, 4.4000000000000004, 1.3999999999999999], [6.7999999999999998, 2.7999999999999998, 4.7999999999999998, 1.3999999999999999], [6.7000000000000002, 3.0, 5.0, 1.7], [6.0, 2.8999999999999999, 4.5, 1.5], [5.7000000000000002, 2.6000000000000001, 3.5, 1.0], [5.5, 2.3999999999999999, 3.7999999999999998, 1.1000000000000001], [5.5, 2.3999999999999999, 3.7000000000000002, 1.0], [5.7999999999999998, 2.7000000000000002, 3.8999999999999999, 1.2], [6.0, 2.7000000000000002, 5.0999999999999996, 1.6000000000000001], [5.4000000000000004, 3.0, 4.5, 1.5], [6.0, 3.3999999999999999, 4.5, 1.6000000000000001], [6.7000000000000002, 3.1000000000000001, 4.7000000000000002, 1.5], [6.2999999999999998, 2.2999999999999998, 4.4000000000000004, 1.3], [5.5999999999999996, 3.0, 4.0999999999999996, 1.3], [5.5, 2.5, 4.0, 1.3], [5.5, 2.6000000000000001, 4.4000000000000004, 1.2], [6.0999999999999996, 3.0, 4.5999999999999996, 1.3999999999999999], [5.7999999999999998, 2.6000000000000001, 4.0, 1.2], [5.0, 2.2999999999999998, 3.2999999999999998, 1.0], [5.5999999999999996, 2.7000000000000002, 4.2000000000000002, 1.3], [5.7000000000000002, 3.0, 4.2000000000000002, 1.2], [5.7000000000000002, 2.8999999999999999, 4.2000000000000002, 1.3], [6.2000000000000002, 2.8999999999999999, 4.2999999999999998, 1.3], [5.0999999999999996, 2.5, 3.0, 1.1000000000000001], [5.7000000000000002, 2.7999999999999998, 4.0999999999999996, 1.3], [6.2999999999999998, 3.2999999999999998, 6.0, 2.5], [5.7999999999999998, 2.7000000000000002, 5.0999999999999996, 1.8999999999999999], [7.0999999999999996, 3.0, 5.9000000000000004, 2.1000000000000001], [6.2999999999999998, 2.8999999999999999, 5.5999999999999996, 1.8], [6.5, 3.0, 5.7999999999999998, 2.2000000000000002], [7.5999999999999996, 3.0, 6.5999999999999996, 2.1000000000000001], [4.9000000000000004, 2.5, 4.5, 1.7], [7.2999999999999998, 2.8999999999999999, 6.2999999999999998, 1.8], [6.7000000000000002, 2.5, 5.7999999999999998, 1.8], [7.2000000000000002, 3.6000000000000001, 6.0999999999999996, 2.5], [6.5, 3.2000000000000002, 5.0999999999999996, 2.0], [6.4000000000000004, 2.7000000000000002, 5.2999999999999998, 1.8999999999999999], [6.7999999999999998, 3.0, 5.5, 2.1000000000000001], [5.7000000000000002, 2.5, 5.0, 2.0], [5.7999999999999998, 2.7999999999999998, 5.0999999999999996, 2.3999999999999999], [6.4000000000000004, 3.2000000000000002, 5.2999999999999998, 2.2999999999999998], [6.5, 3.0, 5.5, 1.8], [7.7000000000000002, 3.7999999999999998, 6.7000000000000002, 2.2000000000000002], [7.7000000000000002, 2.6000000000000001, 6.9000000000000004, 2.2999999999999998], [6.0, 2.2000000000000002, 5.0, 1.5], [6.9000000000000004, 3.2000000000000002, 5.7000000000000002, 2.2999999999999998], [5.5999999999999996, 2.7999999999999998, 4.9000000000000004, 2.0], [7.7000000000000002, 2.7999999999999998, 6.7000000000000002, 2.0], [6.2999999999999998, 2.7000000000000002, 4.9000000000000004, 1.8], [6.7000000000000002, 3.2999999999999998, 5.7000000000000002, 2.1000000000000001], [7.2000000000000002, 3.2000000000000002, 6.0, 1.8], [6.2000000000000002, 2.7999999999999998, 4.7999999999999998, 1.8], [6.0999999999999996, 3.0, 4.9000000000000004, 1.8], [6.4000000000000004, 2.7999999999999998, 5.5999999999999996, 2.1000000000000001], [7.2000000000000002, 3.0, 5.7999999999999998, 1.6000000000000001], [7.4000000000000004, 2.7999999999999998, 6.0999999999999996, 1.8999999999999999], [7.9000000000000004, 3.7999999999999998, 6.4000000000000004, 2.0], [6.4000000000000004, 2.7999999999999998, 5.5999999999999996, 2.2000000000000002], [6.2999999999999998, 2.7999999999999998, 5.0999999999999996, 1.5], [6.0999999999999996, 2.6000000000000001, 5.5999999999999996, 1.3999999999999999], [7.7000000000000002, 3.0, 6.0999999999999996, 2.2999999999999998], [6.2999999999999998, 3.3999999999999999, 5.5999999999999996, 2.3999999999999999], [6.4000000000000004, 3.1000000000000001, 5.5, 1.8], [6.0, 3.0, 4.7999999999999998, 1.8], [6.9000000000000004, 3.1000000000000001, 5.4000000000000004, 2.1000000000000001], [6.7000000000000002, 3.1000000000000001, 5.5999999999999996, 2.3999999999999999], [6.9000000000000004, 3.1000000000000001, 5.0999999999999996, 2.2999999999999998], [5.7999999999999998, 2.7000000000000002, 5.0999999999999996, 1.8999999999999999], [6.7999999999999998, 3.2000000000000002, 5.9000000000000004, 2.2999999999999998], [6.7000000000000002, 3.2999999999999998, 5.7000000000000002, 2.5], [6.7000000000000002, 3.0, 5.2000000000000002, 2.2999999999999998], [6.2999999999999998, 2.5, 5.0, 1.8999999999999999], [6.5, 3.0, 5.2000000000000002, 2.0], [6.2000000000000002, 3.3999999999999999, 5.4000000000000004, 2.2999999999999998], [5.9000000000000004, 3.0, 5.0999999999999996, 1.8]];\n", + " var X = [[5.1, 3.5, 1.4, 0.2], [4.9, 3.0, 1.4, 0.2], [4.7, 3.2, 1.3, 0.2], [4.6, 3.1, 1.5, 0.2], [5.0, 3.6, 1.4, 0.2], [5.4, 3.9, 1.7, 0.4], [4.6, 3.4, 1.4, 0.3], [5.0, 3.4, 1.5, 0.2],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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2];\n", "\n", " // Estimator:\n", " var clf = new KNeighborsClassifier(3, 3, 2, X, y);\n", + "\n", + " // Prediction:\n", " var prediction = clf.predict(features);\n", " console.log(prediction);\n", "\n", " }\n", - "}\n" + "}\n", + "CPU times: user 2.55 ms, sys: 1.37 ms, total: 3.93 ms\n", + "Wall time: 2.92 ms\n" ] } ], "source": [ + "%%time\n", + "\n", "from sklearn_porter import Porter\n", "\n", "porter = Porter(clf, language='js')\n", @@ -196,6 +202,60 @@ "\n", "print(output)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Run classification in JavaScript:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Save the transpiled estimator:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "with open('KNeighborsClassifier.js', 'w') as f:\n", + " f.write(output)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Prediction:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "%%bash\n", + "\n", + "if hash node 2/dev/null; then\n", + " node KNeighborsClassifier.js 1 2 3 4\n", + "fi" + ] } ], "metadata": { diff --git a/examples/estimator/classifier/KNeighborsClassifier/js/basics_imported.ipynb b/examples/estimator/classifier/KNeighborsClassifier/js/basics_imported.ipynb index e5e9ba8f..601ddcbc 100644 --- a/examples/estimator/classifier/KNeighborsClassifier/js/basics_imported.ipynb +++ b/examples/estimator/classifier/KNeighborsClassifier/js/basics_imported.ipynb @@ -90,39 +90,6 @@ "metadata": { "scrolled": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "CPU times: user 3.64 ms, sys: 2.36 ms, total: 6.01 ms\n", - "Wall time: 4.14 ms\n" - ] - } - ], - "source": [ - "%%time\n", - "\n", - "from sklearn_porter import Porter\n", - "\n", - "porter = Porter(clf, language='js')\n", - "output = porter.export(export_data=True)\n", - "\n", - "with open('KNeighborsClassifier.js', 'w') as f:\n", - " f.write(output)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Classifier:" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, "outputs": [ { "name": "stdout", @@ -242,66 +209,66 @@ " });\n", "\n", " }\n", - "}" + "}\n", + "CPU times: user 4.07 ms, sys: 2.88 ms, total: 6.95 ms\n", + "Wall time: 4.86 ms\n" ] } ], "source": [ - "%%bash\n", + "%%time\n", "\n", - "cat KNeighborsClassifier.java" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Parameters:" + "from sklearn_porter import Porter\n", + "\n", + "porter = Porter(clf, language='js')\n", + "output = porter.export(export_data=True)\n", + "\n", + "print(output)" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": {}, "outputs": [ { - "name": "stdout", + "name": "stderr", "output_type": "stream", "text": [ - "{\"y\": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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0, 5.2, 2.3], [6.3, 2.5, 5.0, 1.9], [6.5, 3.0, 5.2, 2.0], [6.2, 3.4, 5.4, 2.3], [5.9, 3.0, 5.1, 1.8]], \"power\": 2, \"nClasses\": 3, \"kNeighbors\": 3}" + "cat: KNeighborsClassifier.java: No such file or directory\n" ] } ], "source": [ "%%bash\n", "\n", - "cat data.json" + "cat KNeighborsClassifier.java" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "File size:" + "Parameters:" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "4.0K\tdata.json\n" + "{\"X\": [[5.1, 3.5, 1.4, 0.2], [4.9, 3.0, 1.4, 0.2], [4.7, 3.2, 1.3, 0.2], [4.6, 3.1, 1.5, 0.2], [5.0, 3.6, 1.4, 0.2], [5.4, 3.9, 1.7, 0.4], [4.6, 3.4, 1.4, 0.3], [5.0, 3.4, 1.5, 0.2], [4.4, 2.9, 1.4, 0.2], [4.9, 3.1, 1.5, 0.1], [5.4, 3.7, 1.5, 0.2], [4.8, 3.4, 1.6, 0.2], [4.8, 3.0, 1.4, 0.1], [4.3, 3.0, 1.1, 0.1], [5.8, 4.0, 1.2, 0.2], [5.7, 4.4, 1.5, 0.4], [5.4, 3.9, 1.3, 0.4], [5.1, 3.5, 1.4, 0.3], [5.7, 3.8, 1.7, 0.3], [5.1, 3.8, 1.5, 0.3], [5.4, 3.4, 1.7, 0.2], [5.1, 3.7, 1.5, 0.4], [4.6, 3.6, 1.0, 0.2], [5.1, 3.3, 1.7, 0.5], [4.8, 3.4, 1.9, 0.2], [5.0, 3.0, 1.6, 0.2], [5.0, 3.4, 1.6, 0.4], [5.2, 3.5, 1.5, 0.2], [5.2, 3.4, 1.4, 0.2], [4.7, 3.2, 1.6, 0.2], [4.8, 3.1, 1.6, 0.2], [5.4, 3.4, 1.5, 0.4], [5.2, 4.1, 1.5, 0.1], [5.5, 4.2, 1.4, 0.2], [4.9,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6, 2.4], [6.4, 3.1, 5.5, 1.8], [6.0, 3.0, 4.8, 1.8], [6.9, 3.1, 5.4, 2.1], [6.7, 3.1, 5.6, 2.4], [6.9, 3.1, 5.1, 2.3], [5.8, 2.7, 5.1, 1.9], [6.8, 3.2, 5.9, 2.3], [6.7, 3.3, 5.7, 2.5], [6.7, 3.0, 5.2, 2.3], [6.3, 2.5, 5.0, 1.9], [6.5, 3.0, 5.2, 2.0], [6.2, 3.4, 5.4, 2.3], [5.9, 3.0, 5.1, 1.8]], \"kNeighbors\": 3, \"nClasses\": 3, \"power\": 2, \"y\": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]}" ] } ], "source": [ "%%bash\n", "\n", - "du -h data.json" + "cat data.json" ] }, { @@ -313,6 +280,32 @@ "### Run classification in JavaScript:" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Save the transpiled estimator:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "with open('KNeighborsClassifier.js', 'w') as f:\n", + " f.write(output)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Prediction:" + ] + }, { "cell_type": "code", "execution_count": 9, @@ -329,7 +322,7 @@ "name": "stderr", "output_type": "stream", "text": [ - "127.0.0.1 - - [26/Nov/2017 01:19:45] \"GET /data.json HTTP/1.1\" 200 -\n" + "127.0.0.1 - - [04/Dec/2017 21:12:27] \"GET /data.json HTTP/1.1\" 200 -\n" ] } ], diff --git a/examples/estimator/classifier/LinearSVC/js/basics.ipynb b/examples/estimator/classifier/LinearSVC/js/basics.ipynb index 5a9042c9..d7c77ad9 100644 --- a/examples/estimator/classifier/LinearSVC/js/basics.ipynb +++ b/examples/estimator/classifier/LinearSVC/js/basics.ipynb @@ -125,8 +125,8 @@ " var features = process.argv.slice(2);\n", "\n", " // Parameters:\n", - " var coefficients = [[0.18424209458473811, 0.45123000025163923, -0.80794587716737576, -0.45071660033253858], [0.052877455748516447, -0.89214995228605254, 0.40398084459610972, -0.9376821661447452], [-0.85070784319293802, -0.98670214922204336, 1.381010448739191, 1.8654095662423917]];\n", - " var intercepts = [0.10956266406702335, 1.6636707776739579, -1.7096109416521363];\n", + " var 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]];\n", + " var intercepts = [0.109562664067, 1.66367077767, -1.70961094165];\n", "\n", " // Prediction:\n", " var clf = new LinearSVC(coefficients, intercepts);\n", @@ -134,11 +134,15 @@ " console.log(prediction);\n", "\n", " }\n", - "}\n" + "}\n", + "CPU times: user 754 µs, sys: 642 µs, total: 1.4 ms\n", + "Wall time: 852 µs\n" ] } ], "source": [ + "%%time\n", + "\n", "from sklearn_porter import Porter\n", "\n", "porter = Porter(clf, language='js')\n", @@ -146,6 +150,60 @@ "\n", "print(output)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Run classification in JavaScript:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Save the transpiled estimator:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "with open('LinearSVC.js', 'w') as f:\n", + " f.write(output)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Prediction:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "%%bash\n", + "\n", + "if hash node 2/dev/null; then\n", + " node LinearSVC.js 1 2 3 4\n", + "fi" + ] } ], "metadata": { diff --git a/examples/estimator/classifier/MLPClassifier/js/basics.ipynb b/examples/estimator/classifier/MLPClassifier/js/basics.ipynb index 775877f8..c15ffe68 100644 --- a/examples/estimator/classifier/MLPClassifier/js/basics.ipynb +++ b/examples/estimator/classifier/MLPClassifier/js/basics.ipynb @@ -100,7 +100,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 6, "metadata": { "scrolled": false }, @@ -210,8 +210,8 @@ "\n", " }\n", "}\n", - "CPU times: user 1.53 ms, sys: 696 µs, total: 2.22 ms\n", - "Wall time: 1.67 ms\n" + "CPU times: user 1.6 ms, sys: 1.38 ms, total: 2.98 ms\n", + "Wall time: 1.9 ms\n" ] } ], @@ -242,7 +242,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 7, "metadata": { "collapsed": true }, @@ -261,7 +261,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 8, "metadata": {}, "outputs": [ { diff --git a/examples/estimator/classifier/NuSVC/js/basics.ipynb b/examples/estimator/classifier/NuSVC/js/basics.ipynb index f16ef65b..9bb4f2f1 100644 --- a/examples/estimator/classifier/NuSVC/js/basics.ipynb +++ b/examples/estimator/classifier/NuSVC/js/basics.ipynb @@ -52,7 +52,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -64,7 +64,7 @@ " tol=0.001, verbose=False)" ] }, - "execution_count": 2, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -85,7 +85,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 6, "metadata": { "scrolled": false }, @@ -246,9 +246,9 @@ " var features = process.argv.slice(2);\n", "\n", " // Parameters:\n", - " var vectors = [[4.9000000000000004, 3.0, 1.3999999999999999, 0.20000000000000001], [4.5999999999999996, 3.1000000000000001, 1.5, 0.20000000000000001], [5.4000000000000004, 3.8999999999999999, 1.7, 0.40000000000000002], [5.0, 3.3999999999999999, 1.5, 0.20000000000000001], [4.9000000000000004, 3.1000000000000001, 1.5, 0.10000000000000001], [5.4000000000000004, 3.7000000000000002, 1.5, 0.20000000000000001], [4.7999999999999998, 3.3999999999999999, 1.6000000000000001, 0.20000000000000001], [5.7000000000000002, 4.4000000000000004, 1.5, 0.40000000000000002], [5.7000000000000002, 3.7999999999999998, 1.7, 0.29999999999999999], [5.0999999999999996, 3.7999999999999998, 1.5, 0.29999999999999999], [5.4000000000000004, 3.3999999999999999, 1.7, 0.20000000000000001], [5.0999999999999996, 3.7000000000000002, 1.5, 0.40000000000000002], [5.0999999999999996, 3.2999999999999998, 1.7, 0.5], [4.7999999999999998, 3.3999999999999999, 1.8999999999999999, 0.20000000000000001], [5.0, 3.0, 1.6000000000000001, 0.20000000000000001], [5.0, 3.3999999999999999, 1.6000000000000001, 0.40000000000000002], [5.2000000000000002, 3.5, 1.5, 0.20000000000000001], [4.7000000000000002, 3.2000000000000002, 1.6000000000000001, 0.20000000000000001], [4.7999999999999998, 3.1000000000000001, 1.6000000000000001, 0.20000000000000001], [5.4000000000000004, 3.3999999999999999, 1.5, 0.40000000000000002], [4.9000000000000004, 3.1000000000000001, 1.5, 0.10000000000000001], [4.9000000000000004, 3.1000000000000001, 1.5, 0.10000000000000001], [5.0999999999999996, 3.3999999999999999, 1.5, 0.20000000000000001], [4.5, 2.2999999999999998, 1.3, 0.29999999999999999], [5.0, 3.5, 1.6000000000000001, 0.59999999999999998], [5.0999999999999996, 3.7999999999999998, 1.8999999999999999, 0.40000000000000002], [4.7999999999999998, 3.0, 1.3999999999999999, 0.29999999999999999], [5.0999999999999996, 3.7999999999999998, 1.6000000000000001, 0.20000000000000001], [5.2999999999999998, 3.7000000000000002, 1.5, 0.20000000000000001], [7.0, 3.2000000000000002, 4.7000000000000002, 1.3999999999999999], [6.4000000000000004, 3.2000000000000002, 4.5, 1.5], [6.9000000000000004, 3.1000000000000001, 4.9000000000000004, 1.5], [5.5, 2.2999999999999998, 4.0, 1.3], [6.5, 2.7999999999999998, 4.5999999999999996, 1.5], [5.7000000000000002, 2.7999999999999998, 4.5, 1.3], [6.2999999999999998, 3.2999999999999998, 4.7000000000000002, 1.6000000000000001], [4.9000000000000004, 2.3999999999999999, 3.2999999999999998, 1.0], [6.5999999999999996, 2.8999999999999999, 4.5999999999999996, 1.3], [5.2000000000000002, 2.7000000000000002, 3.8999999999999999, 1.3999999999999999], [5.0, 2.0, 3.5, 1.0], [5.9000000000000004, 3.0, 4.2000000000000002, 1.5], [6.0, 2.2000000000000002, 4.0, 1.0], [6.0999999999999996, 2.8999999999999999, 4.7000000000000002, 1.3999999999999999], [5.5999999999999996, 2.8999999999999999, 3.6000000000000001, 1.3], [6.7000000000000002, 3.1000000000000001, 4.4000000000000004, 1.3999999999999999], [5.5999999999999996, 3.0, 4.5, 1.5], [5.7999999999999998, 2.7000000000000002, 4.0999999999999996, 1.0], [6.2000000000000002, 2.2000000000000002, 4.5, 1.5], [5.5999999999999996, 2.5, 3.8999999999999999, 1.1000000000000001], [5.9000000000000004, 3.2000000000000002, 4.7999999999999998, 1.8], [6.0999999999999996, 2.7999999999999998, 4.0, 1.3], [6.2999999999999998, 2.5, 4.9000000000000004, 1.5], [6.0999999999999996, 2.7999999999999998, 4.7000000000000002, 1.2], [6.5999999999999996, 3.0, 4.4000000000000004, 1.3999999999999999], [6.7999999999999998, 2.7999999999999998, 4.7999999999999998, 1.3999999999999999], [6.7000000000000002, 3.0, 5.0, 1.7], [6.0, 2.8999999999999999, 4.5, 1.5], [5.7000000000000002, 2.6000000000000001, 3.5, 1.0], [5.5, 2.3999999999999999, 3.7999999999999998, 1.1000000000000001], [5.5, 2.3999999999999999, 3.7000000000000002, 1.0], [5.7999999999999998, 2.7000000000000002, 3.8999999999999999, 1.2], [6.0, 2.7000000000000002, 5.0999999999999996, 1.6000000000000001], [5.4000000000000004, 3.0, 4.5, 1.5], [6.0, 3.3999999999999999, 4.5, 1.6000000000000001], [6.7000000000000002, 3.1000000000000001, 4.7000000000000002, 1.5], [6.2999999999999998, 2.2999999999999998, 4.4000000000000004, 1.3], [5.5999999999999996, 3.0, 4.0999999999999996, 1.3], [5.5, 2.5, 4.0, 1.3], [5.5, 2.6000000000000001, 4.4000000000000004, 1.2], [6.0999999999999996, 3.0, 4.5999999999999996, 1.3999999999999999], [5.7999999999999998, 2.6000000000000001, 4.0, 1.2], [5.0, 2.2999999999999998, 3.2999999999999998, 1.0], [5.5999999999999996, 2.7000000000000002, 4.2000000000000002, 1.3], [5.7000000000000002, 3.0, 4.2000000000000002, 1.2], [5.7000000000000002, 2.8999999999999999, 4.2000000000000002, 1.3], [6.2000000000000002, 2.8999999999999999, 4.2999999999999998, 1.3], [5.0999999999999996, 2.5, 3.0, 1.1000000000000001], [5.7000000000000002, 2.7999999999999998, 4.0999999999999996, 1.3], [5.7999999999999998, 2.7000000000000002, 5.0999999999999996, 1.8999999999999999], [6.2999999999999998, 2.8999999999999999, 5.5999999999999996, 1.8], [4.9000000000000004, 2.5, 4.5, 1.7], [6.5, 3.2000000000000002, 5.0999999999999996, 2.0], [6.4000000000000004, 2.7000000000000002, 5.2999999999999998, 1.8999999999999999], [5.7000000000000002, 2.5, 5.0, 2.0], [5.7999999999999998, 2.7999999999999998, 5.0999999999999996, 2.3999999999999999], [6.4000000000000004, 3.2000000000000002, 5.2999999999999998, 2.2999999999999998], [6.5, 3.0, 5.5, 1.8], [6.0, 2.2000000000000002, 5.0, 1.5], [5.5999999999999996, 2.7999999999999998, 4.9000000000000004, 2.0], [6.2999999999999998, 2.7000000000000002, 4.9000000000000004, 1.8], [6.2000000000000002, 2.7999999999999998, 4.7999999999999998, 1.8], [6.0999999999999996, 3.0, 4.9000000000000004, 1.8], [7.2000000000000002, 3.0, 5.7999999999999998, 1.6000000000000001], [6.2999999999999998, 2.7999999999999998, 5.0999999999999996, 1.5], [6.0999999999999996, 2.6000000000000001, 5.5999999999999996, 1.3999999999999999], [6.4000000000000004, 3.1000000000000001, 5.5, 1.8], [6.0, 3.0, 4.7999999999999998, 1.8], [6.9000000000000004, 3.1000000000000001, 5.4000000000000004, 2.1000000000000001], [6.9000000000000004, 3.1000000000000001, 5.0999999999999996, 2.2999999999999998], [5.7999999999999998, 2.7000000000000002, 5.0999999999999996, 1.8999999999999999], [6.7000000000000002, 3.0, 5.2000000000000002, 2.2999999999999998], [6.2999999999999998, 2.5, 5.0, 1.8999999999999999], [6.5, 3.0, 5.2000000000000002, 2.0], [6.2000000000000002, 3.3999999999999999, 5.4000000000000004, 2.2999999999999998], [5.9000000000000004, 3.0, 5.0999999999999996, 1.8]];\n", - " var coefficients = [[4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 0.0, 4.6863813658892557, 0.0, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 4.6863813658892557, 0.0, 0.0, -0.0, -0.0, -0.0, -4.6863813658892557, -0.0, -0.0, -0.0, -4.6863813658892557, -0.0, -4.6863813658892557, -4.6863813658892557, -4.6863813658892557, -4.6863813658892557, -0.0, -4.6863813658892557, -0.0, -0.0, -4.6863813658892557, -0.0, -4.6863813658892557, -0.0, -4.6863813658892557, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -4.6863813658892557, -4.6863813658892557, -4.6863813658892557, -4.6863813658892557, -0.0, -0.0, -0.0, -0.0, -0.0, -4.6863813658892557, -4.6863813658892557, -4.6863813658892557, -0.0, -4.6863813658892557, -4.6863813658892557, -4.6863813658892557, -4.6863813658892557, -4.6863813658892557, -4.6863813658892557, -4.6863813658892557, -4.6863813658892557, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948, -0.0, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948, -0.0, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948, -2.1272220789292948], [0.0, 0.0, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 2.1272220789292948, 0.0, 2.1272220789292948, 2.1272220789292948, 0.0, 2.1272220789292948, 2.1272220789292948, 47.529341773693893, 47.529341773693893, 47.529341773693893, 0.0, 47.529341773693893, 47.529341773693893, 47.529341773693893, 0.0, 47.529341773693893, 0.0, 0.0, 0.0, 0.0, 47.529341773693893, 0.0, 47.529341773693893, 47.529341773693893, 0.0, 47.529341773693893, 0.0, 47.529341773693893, 0.0, 47.529341773693893, 47.529341773693893, 47.529341773693893, 47.529341773693893, 47.529341773693893, 47.529341773693893, 0.0, 0.0, 0.0, 0.0, 47.529341773693893, 47.529341773693893, 47.529341773693893, 47.529341773693893, 47.529341773693893, 0.0, 0.0, 47.529341773693893, 47.529341773693893, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -47.529341773693893, -47.529341773693893, -47.529341773693893, -47.529341773693893, -47.529341773693893, -47.529341773693893, -47.529341773693893, -0.0, -47.529341773693893, -47.529341773693893, -47.529341773693893, -47.529341773693893, -47.529341773693893, -47.529341773693893, -47.529341773693893, -47.529341773693893, -47.529341773693893, -47.529341773693893, -47.529341773693893, -47.529341773693893, -47.529341773693893, -47.529341773693893, -47.529341773693893, -47.529341773693893, -47.529341773693893, -0.0, -47.529341773693893]];\n", - " var intercepts = [0.10061840191760112, 0.051748160156319709, -0.084181689668018464];\n", + " var vectors = [[4.9, 3.0, 1.4, 0.2], [4.6, 3.1, 1.5, 0.2], [5.4, 3.9, 1.7, 0.4], [5.0, 3.4, 1.5, 0.2], [4.9, 3.1, 1.5, 0.1], [5.4, 3.7, 1.5, 0.2], [4.8, 3.4, 1.6, 0.2], [5.7, 4.4, 1.5, 0.4], [5.7, 3.8, 1.7, 0.3], [5.1, 3.8, 1.5, 0.3], [5.4, 3.4, 1.7, 0.2], [5.1, 3.7, 1.5, 0.4], [5.1, 3.3, 1.7, 0.5], [4.8, 3.4, 1.9, 0.2], [5.0, 3.0, 1.6, 0.2], [5.0, 3.4, 1.6, 0.4], [5.2, 3.5, 1.5, 0.2], [4.7, 3.2, 1.6, 0.2], [4.8, 3.1, 1.6, 0.2], [5.4, 3.4, 1.5, 0.4], [4.9, 3.1, 1.5, 0.1], [4.9, 3.1, 1.5, 0.1], [5.1, 3.4, 1.5, 0.2], [4.5, 2.3, 1.3, 0.3], [5.0, 3.5, 1.6, 0.6], [5.1, 3.8, 1.9, 0.4], [4.8, 3.0, 1.4, 0.3], [5.1, 3.8, 1.6, 0.2], [5.3, 3.7, 1.5, 0.2], [7.0, 3.2, 4.7, 1.4], [6.4, 3.2, 4.5, 1.5], [6.9, 3.1, 4.9, 1.5], [5.5, 2.3, 4.0, 1.3], [6.5, 2.8, 4.6, 1.5], [5.7, 2.8, 4.5, 1.3], [6.3, 3.3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5, 1.8], [6.0, 2.2, 5.0, 1.5], [5.6, 2.8, 4.9, 2.0], [6.3, 2.7, 4.9, 1.8], [6.2, 2.8, 4.8, 1.8], [6.1, 3.0, 4.9, 1.8], [7.2, 3.0, 5.8, 1.6], [6.3, 2.8, 5.1, 1.5], [6.1, 2.6, 5.6, 1.4], [6.4, 3.1, 5.5, 1.8], [6.0, 3.0, 4.8, 1.8], [6.9, 3.1, 5.4, 2.1], [6.9, 3.1, 5.1, 2.3], [5.8, 2.7, 5.1, 1.9], [6.7, 3.0, 5.2, 2.3], [6.3, 2.5, 5.0, 1.9], [6.5, 3.0, 5.2, 2.0], [6.2, 3.4, 5.4, 2.3], [5.9, 3.0, 5.1, 1.8]];\n", + " var coefficients = [[4.68638136589, 4.68638136589, 4.68638136589, 4.68638136589, 4.68638136589, 4.68638136589, 4.68638136589, 0.0, 4.68638136589, 0.0, 4.68638136589, 4.68638136589, 4.68638136589, 4.68638136589, 4.68638136589, 4.68638136589, 4.68638136589, 4.68638136589, 4.68638136589, 4.68638136589, 4.68638136589, 4.68638136589, 4.68638136589, 4.68638136589, 4.68638136589, 4.68638136589, 4.68638136589, 0.0, 0.0, -0.0, -0.0, -0.0, -4.68638136589, -0.0, -0.0, -0.0, -4.68638136589, -0.0, -4.68638136589, -4.68638136589, -4.68638136589, -4.68638136589, -0.0, -4.68638136589, -0.0, -0.0, -4.68638136589, -0.0, -4.68638136589, -0.0, -4.68638136589, -0.0, -0.0, -0.0, -0.0, -0.0, -0.0, -4.68638136589, -4.68638136589, -4.68638136589, -4.68638136589, -0.0, -0.0, -0.0, -0.0, -0.0, -4.68638136589, -4.68638136589, -4.68638136589, -0.0, -4.68638136589, -4.68638136589, -4.68638136589, -4.68638136589, -4.68638136589, -4.68638136589, -4.68638136589, -4.68638136589, -2.12722207893, -2.12722207893, -2.12722207893, -2.12722207893, -2.12722207893, -2.12722207893, -2.12722207893, -2.12722207893, -2.12722207893, -2.12722207893, -2.12722207893, -2.12722207893, -2.12722207893, -2.12722207893, -0.0, -2.12722207893, -2.12722207893, -2.12722207893, -2.12722207893, -0.0, -2.12722207893, -2.12722207893, -2.12722207893, -2.12722207893, -2.12722207893, -2.12722207893, -2.12722207893], [0.0, 0.0, 2.12722207893, 2.12722207893, 2.12722207893, 2.12722207893, 2.12722207893, 2.12722207893, 2.12722207893, 2.12722207893, 2.12722207893, 2.12722207893, 2.12722207893, 2.12722207893, 2.12722207893, 2.12722207893, 2.12722207893, 2.12722207893, 2.12722207893, 2.12722207893, 2.12722207893, 2.12722207893, 2.12722207893, 0.0, 2.12722207893, 2.12722207893, 0.0, 2.12722207893, 2.12722207893, 47.5293417737, 47.5293417737, 47.5293417737, 0.0, 47.5293417737, 47.5293417737, 47.5293417737, 0.0, 47.5293417737, 0.0, 0.0, 0.0, 0.0, 47.5293417737, 0.0, 47.5293417737, 47.5293417737, 0.0, 47.5293417737, 0.0, 47.5293417737, 0.0, 47.5293417737, 47.5293417737, 47.5293417737, 47.5293417737, 47.5293417737, 47.5293417737, 0.0, 0.0, 0.0, 0.0, 47.5293417737, 47.5293417737, 47.5293417737, 47.5293417737, 47.5293417737, 0.0, 0.0, 47.5293417737, 47.5293417737, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -47.5293417737, -47.5293417737, -47.5293417737, -47.5293417737, -47.5293417737, -47.5293417737, -47.5293417737, -0.0, -47.5293417737, -47.5293417737, -47.5293417737, -47.5293417737, -47.5293417737, -47.5293417737, -47.5293417737, -47.5293417737, -47.5293417737, -47.5293417737, -47.5293417737, -47.5293417737, -47.5293417737, -47.5293417737, -47.5293417737, -47.5293417737, -47.5293417737, -0.0, -47.5293417737]];\n", + " var intercepts = [0.100618401918, 0.0517481601563, -0.084181689668];\n", " var weights = [29, 49, 27];\n", "\n", " // Prediction:\n", @@ -257,11 +257,15 @@ " console.log(prediction);\n", "\n", " }\n", - "}\n" + "}\n", + "CPU times: user 2.85 ms, sys: 1.64 ms, total: 4.49 ms\n", + "Wall time: 3.06 ms\n" ] } ], "source": [ + "%%time\n", + "\n", "from sklearn_porter import Porter\n", "\n", "porter = Porter(clf, language='js')\n", @@ -269,6 +273,60 @@ "\n", "print(output)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Run classification in JavaScript:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Save the transpiled estimator:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "with open('NuSVC.js', 'w') as f:\n", + " f.write(output)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Prediction:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "%%bash\n", + "\n", + "if hash node 2/dev/null; then\n", + " node NuSVC.js 1 2 3 4\n", + "fi" + ] } ], "metadata": { diff --git a/examples/estimator/classifier/RandomForestClassifier/js/basics_embedded.ipynb b/examples/estimator/classifier/RandomForestClassifier/js/basics_embedded.ipynb index bbdda4ad..b5214c61 100644 --- a/examples/estimator/classifier/RandomForestClassifier/js/basics_embedded.ipynb +++ b/examples/estimator/classifier/RandomForestClassifier/js/basics_embedded.ipynb @@ -88,7 +88,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { "scrolled": false }, @@ -970,11 +970,15 @@ " console.log(prediction);\n", "\n", " }\n", - "}\n" + "}\n", + "CPU times: user 8.93 ms, sys: 1.83 ms, total: 10.8 ms\n", + "Wall time: 9.71 ms\n" ] } ], "source": [ + "%%time\n", + "\n", "from sklearn_porter import Porter\n", "\n", "porter = Porter(clf, language='js')\n", @@ -982,6 +986,60 @@ "\n", "print(output)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Run classification in JavaScript:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Save the transpiled estimator:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "with open('RandomForestClassifier.js', 'w') as f:\n", + " f.write(output)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Prediction:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n" + ] + } + ], + "source": [ + "%%bash\n", + "\n", + "if hash node 2/dev/null; then\n", + " node RandomForestClassifier.js 1 2 3 4\n", + "fi" + ] } ], "metadata": { diff --git a/examples/estimator/classifier/SVC/js/basics.ipynb b/examples/estimator/classifier/SVC/js/basics.ipynb index 1eb068b9..dea33f58 100644 --- a/examples/estimator/classifier/SVC/js/basics.ipynb +++ b/examples/estimator/classifier/SVC/js/basics.ipynb @@ -85,7 +85,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "metadata": { "scrolled": false }, @@ -246,9 +246,9 @@ " var features = process.argv.slice(2);\n", "\n", " // Parameters:\n", - " var vectors = [[5.0999999999999996, 3.5, 1.3999999999999999, 0.20000000000000001], [4.9000000000000004, 3.0, 1.3999999999999999, 0.20000000000000001], [4.7000000000000002, 3.2000000000000002, 1.3, 0.20000000000000001], [4.5999999999999996, 3.1000000000000001, 1.5, 0.20000000000000001], [5.0, 3.6000000000000001, 1.3999999999999999, 0.20000000000000001], [5.4000000000000004, 3.8999999999999999, 1.7, 0.40000000000000002], [4.5999999999999996, 3.3999999999999999, 1.3999999999999999, 0.29999999999999999], [5.0, 3.3999999999999999, 1.5, 0.20000000000000001], [4.4000000000000004, 2.8999999999999999, 1.3999999999999999, 0.20000000000000001], [4.9000000000000004, 3.1000000000000001, 1.5, 0.10000000000000001], [5.4000000000000004, 3.7000000000000002, 1.5, 0.20000000000000001], [4.7999999999999998, 3.3999999999999999, 1.6000000000000001, 0.20000000000000001], [4.7999999999999998, 3.0, 1.3999999999999999, 0.10000000000000001], [4.2999999999999998, 3.0, 1.1000000000000001, 0.10000000000000001], [5.7999999999999998, 4.0, 1.2, 0.20000000000000001], [5.7000000000000002, 4.4000000000000004, 1.5, 0.40000000000000002], [5.4000000000000004, 3.8999999999999999, 1.3, 0.40000000000000002], [5.0999999999999996, 3.5, 1.3999999999999999, 0.29999999999999999], [5.7000000000000002, 3.7999999999999998, 1.7, 0.29999999999999999], [5.0999999999999996, 3.7999999999999998, 1.5, 0.29999999999999999], [5.4000000000000004, 3.3999999999999999, 1.7, 0.20000000000000001], [5.0999999999999996, 3.7000000000000002, 1.5, 0.40000000000000002], [4.5999999999999996, 3.6000000000000001, 1.0, 0.20000000000000001], [5.0999999999999996, 3.2999999999999998, 1.7, 0.5], [4.7999999999999998, 3.3999999999999999, 1.8999999999999999, 0.20000000000000001], [5.0, 3.0, 1.6000000000000001, 0.20000000000000001], [5.0, 3.3999999999999999, 1.6000000000000001, 0.40000000000000002], [5.2000000000000002, 3.5, 1.5, 0.20000000000000001], [5.2000000000000002, 3.3999999999999999, 1.3999999999999999, 0.20000000000000001], [4.7000000000000002, 3.2000000000000002, 1.6000000000000001, 0.20000000000000001], [4.7999999999999998, 3.1000000000000001, 1.6000000000000001, 0.20000000000000001], [5.4000000000000004, 3.3999999999999999, 1.5, 0.40000000000000002], [5.2000000000000002, 4.0999999999999996, 1.5, 0.10000000000000001], [5.5, 4.2000000000000002, 1.3999999999999999, 0.20000000000000001], [4.9000000000000004, 3.1000000000000001, 1.5, 0.10000000000000001], [5.0, 3.2000000000000002, 1.2, 0.20000000000000001], [5.5, 3.5, 1.3, 0.20000000000000001], [4.9000000000000004, 3.1000000000000001, 1.5, 0.10000000000000001], [4.4000000000000004, 3.0, 1.3, 0.20000000000000001], [5.0999999999999996, 3.3999999999999999, 1.5, 0.20000000000000001], [5.0, 3.5, 1.3, 0.29999999999999999], [4.5, 2.2999999999999998, 1.3, 0.29999999999999999], [4.4000000000000004, 3.2000000000000002, 1.3, 0.20000000000000001], [5.0, 3.5, 1.6000000000000001, 0.59999999999999998], [5.0999999999999996, 3.7999999999999998, 1.8999999999999999, 0.40000000000000002], [4.7999999999999998, 3.0, 1.3999999999999999, 0.29999999999999999], [5.0999999999999996, 3.7999999999999998, 1.6000000000000001, 0.20000000000000001], [4.5999999999999996, 3.2000000000000002, 1.3999999999999999, 0.20000000000000001], [5.2999999999999998, 3.7000000000000002, 1.5, 0.20000000000000001], [5.0, 3.2999999999999998, 1.3999999999999999, 0.20000000000000001], [7.0, 3.2000000000000002, 4.7000000000000002, 1.3999999999999999], [6.4000000000000004, 3.2000000000000002, 4.5, 1.5], [6.9000000000000004, 3.1000000000000001, 4.9000000000000004, 1.5], [5.5, 2.2999999999999998, 4.0, 1.3], [6.5, 2.7999999999999998, 4.5999999999999996, 1.5], [5.7000000000000002, 2.7999999999999998, 4.5, 1.3], [6.2999999999999998, 3.2999999999999998, 4.7000000000000002, 1.6000000000000001], [4.9000000000000004, 2.3999999999999999, 3.2999999999999998, 1.0], [6.5999999999999996, 2.8999999999999999, 4.5999999999999996, 1.3], [5.2000000000000002, 2.7000000000000002, 3.8999999999999999, 1.3999999999999999], [5.0, 2.0, 3.5, 1.0], [5.9000000000000004, 3.0, 4.2000000000000002, 1.5], [6.0, 2.2000000000000002, 4.0, 1.0], [6.0999999999999996, 2.8999999999999999, 4.7000000000000002, 1.3999999999999999], [5.5999999999999996, 2.8999999999999999, 3.6000000000000001, 1.3], [6.7000000000000002, 3.1000000000000001, 4.4000000000000004, 1.3999999999999999], [5.5999999999999996, 3.0, 4.5, 1.5], [5.7999999999999998, 2.7000000000000002, 4.0999999999999996, 1.0], [6.2000000000000002, 2.2000000000000002, 4.5, 1.5], [5.5999999999999996, 2.5, 3.8999999999999999, 1.1000000000000001], [5.9000000000000004, 3.2000000000000002, 4.7999999999999998, 1.8], [6.0999999999999996, 2.7999999999999998, 4.0, 1.3], [6.2999999999999998, 2.5, 4.9000000000000004, 1.5], [6.0999999999999996, 2.7999999999999998, 4.7000000000000002, 1.2], [6.4000000000000004, 2.8999999999999999, 4.2999999999999998, 1.3], [6.5999999999999996, 3.0, 4.4000000000000004, 1.3999999999999999], [6.7999999999999998, 2.7999999999999998, 4.7999999999999998, 1.3999999999999999], [6.7000000000000002, 3.0, 5.0, 1.7], [6.0, 2.8999999999999999, 4.5, 1.5], [5.7000000000000002, 2.6000000000000001, 3.5, 1.0], [5.5, 2.3999999999999999, 3.7999999999999998, 1.1000000000000001], [5.5, 2.3999999999999999, 3.7000000000000002, 1.0], [5.7999999999999998, 2.7000000000000002, 3.8999999999999999, 1.2], [6.0, 2.7000000000000002, 5.0999999999999996, 1.6000000000000001], [5.4000000000000004, 3.0, 4.5, 1.5], [6.0, 3.3999999999999999, 4.5, 1.6000000000000001], [6.7000000000000002, 3.1000000000000001, 4.7000000000000002, 1.5], [6.2999999999999998, 2.2999999999999998, 4.4000000000000004, 1.3], [5.5999999999999996, 3.0, 4.0999999999999996, 1.3], [5.5, 2.5, 4.0, 1.3], [5.5, 2.6000000000000001, 4.4000000000000004, 1.2], [6.0999999999999996, 3.0, 4.5999999999999996, 1.3999999999999999], [5.7999999999999998, 2.6000000000000001, 4.0, 1.2], [5.0, 2.2999999999999998, 3.2999999999999998, 1.0], [5.5999999999999996, 2.7000000000000002, 4.2000000000000002, 1.3], [5.7000000000000002, 3.0, 4.2000000000000002, 1.2], [5.7000000000000002, 2.8999999999999999, 4.2000000000000002, 1.3], [6.2000000000000002, 2.8999999999999999, 4.2999999999999998, 1.3], [5.0999999999999996, 2.5, 3.0, 1.1000000000000001], [5.7000000000000002, 2.7999999999999998, 4.0999999999999996, 1.3], [6.2999999999999998, 3.2999999999999998, 6.0, 2.5], [5.7999999999999998, 2.7000000000000002, 5.0999999999999996, 1.8999999999999999], [7.0999999999999996, 3.0, 5.9000000000000004, 2.1000000000000001], [6.2999999999999998, 2.8999999999999999, 5.5999999999999996, 1.8], [6.5, 3.0, 5.7999999999999998, 2.2000000000000002], [7.5999999999999996, 3.0, 6.5999999999999996, 2.1000000000000001], [4.9000000000000004, 2.5, 4.5, 1.7], [7.2999999999999998, 2.8999999999999999, 6.2999999999999998, 1.8], [6.7000000000000002, 2.5, 5.7999999999999998, 1.8], [7.2000000000000002, 3.6000000000000001, 6.0999999999999996, 2.5], [6.5, 3.2000000000000002, 5.0999999999999996, 2.0], [6.4000000000000004, 2.7000000000000002, 5.2999999999999998, 1.8999999999999999], [6.7999999999999998, 3.0, 5.5, 2.1000000000000001], [5.7000000000000002, 2.5, 5.0, 2.0], [5.7999999999999998, 2.7999999999999998, 5.0999999999999996, 2.3999999999999999], [6.4000000000000004, 3.2000000000000002, 5.2999999999999998, 2.2999999999999998], [6.5, 3.0, 5.5, 1.8], [7.7000000000000002, 3.7999999999999998, 6.7000000000000002, 2.2000000000000002], [7.7000000000000002, 2.6000000000000001, 6.9000000000000004, 2.2999999999999998], [6.0, 2.2000000000000002, 5.0, 1.5], [6.9000000000000004, 3.2000000000000002, 5.7000000000000002, 2.2999999999999998], [5.5999999999999996, 2.7999999999999998, 4.9000000000000004, 2.0], [7.7000000000000002, 2.7999999999999998, 6.7000000000000002, 2.0], [6.2999999999999998, 2.7000000000000002, 4.9000000000000004, 1.8], [6.7000000000000002, 3.2999999999999998, 5.7000000000000002, 2.1000000000000001], [7.2000000000000002, 3.2000000000000002, 6.0, 1.8], [6.2000000000000002, 2.7999999999999998, 4.7999999999999998, 1.8], [6.0999999999999996, 3.0, 4.9000000000000004, 1.8], [6.4000000000000004, 2.7999999999999998, 5.5999999999999996, 2.1000000000000001], [7.2000000000000002, 3.0, 5.7999999999999998, 1.6000000000000001], [7.4000000000000004, 2.7999999999999998, 6.0999999999999996, 1.8999999999999999], [7.9000000000000004, 3.7999999999999998, 6.4000000000000004, 2.0], [6.4000000000000004, 2.7999999999999998, 5.5999999999999996, 2.2000000000000002], [6.2999999999999998, 2.7999999999999998, 5.0999999999999996, 1.5], [6.0999999999999996, 2.6000000000000001, 5.5999999999999996, 1.3999999999999999], [7.7000000000000002, 3.0, 6.0999999999999996, 2.2999999999999998], [6.2999999999999998, 3.3999999999999999, 5.5999999999999996, 2.3999999999999999], [6.4000000000000004, 3.1000000000000001, 5.5, 1.8], [6.0, 3.0, 4.7999999999999998, 1.8], [6.9000000000000004, 3.1000000000000001, 5.4000000000000004, 2.1000000000000001], [6.7000000000000002, 3.1000000000000001, 5.5999999999999996, 2.3999999999999999], [6.9000000000000004, 3.1000000000000001, 5.0999999999999996, 2.2999999999999998], [5.7999999999999998, 2.7000000000000002, 5.0999999999999996, 1.8999999999999999], [6.7999999999999998, 3.2000000000000002, 5.9000000000000004, 2.2999999999999998], [6.7000000000000002, 3.2999999999999998, 5.7000000000000002, 2.5], [6.7000000000000002, 3.0, 5.2000000000000002, 2.2999999999999998], [6.2999999999999998, 2.5, 5.0, 1.8999999999999999], [6.5, 3.0, 5.2000000000000002, 2.0], [6.2000000000000002, 3.3999999999999999, 5.4000000000000004, 2.2999999999999998], [5.9000000000000004, 3.0, 5.0999999999999996, 1.8]];\n", + " var vectors = [[5.1, 3.5, 1.4, 0.2], [4.9, 3.0, 1.4, 0.2], [4.7, 3.2, 1.3, 0.2], [4.6, 3.1, 1.5, 0.2], [5.0, 3.6, 1.4, 0.2], [5.4, 3.9, 1.7, 0.4], [4.6, 3.4, 1.4, 0.3], [5.0, 3.4, 1.5, 0.2], [4.4, 2.9, 1.4, 0.2], [4.9, 3.1, 1.5, 0.1], [5.4, 3.7, 1.5, 0.2], [4.8, 3.4, 1.6, 0.2], [4.8, 3.0, 1.4, 0.1], [4.3, 3.0, 1.1, 0.1], [5.8, 4.0, 1.2, 0.2], [5.7, 4.4, 1.5, 0.4], [5.4, 3.9, 1.3, 0.4], [5.1, 3.5, 1.4, 0.3], [5.7, 3.8, 1.7, 0.3], [5.1, 3.8, 1.5, 0.3], [5.4, 3.4, 1.7, 0.2], [5.1, 3.7, 1.5, 0.4], [4.6, 3.6, 1.0, 0.2], [5.1, 3.3, 1.7, 0.5], [4.8, 3.4, 1.9, 0.2], [5.0, 3.0, 1.6, 0.2], [5.0, 3.4, 1.6, 0.4], [5.2, 3.5, 1.5, 0.2], [5.2, 3.4, 1.4, 0.2], [4.7, 3.2, 1.6, 0.2], [4.8, 3.1, 1.6, 0.2], [5.4, 3.4, 1.5, 0.4], [5.2, 4.1, 1.5, 0.1], [5.5, 4.2, 1.4, 0.2], [4.9, 3.1, 1.5, 0.1], [5.0, 3.2, 1.2, 0.2], [5.5, 3.5, 1.3, 0.2], [4.9, 3.1, 1.5, 0.1], [4.4, 3.0, 1.3, 0.2], [5.1, 3.4, 1.5, 0.2], [5.0, 3.5, 1.3, 0.3], [4.5, 2.3, 1.3, 0.3], [4.4, 3.2, 1.3, 0.2], [5.0, 3.5, 1.6, 0.6], [5.1, 3.8, 1.9, 0.4], [4.8, 3.0, 1.4, 0.3], [5.1, 3.8, 1.6, 0.2], [4.6, 3.2, 1.4, 0.2], [5.3, 3.7, 1.5, 0.2], [5.0, 3.3, 1.4, 0.2], [7.0, 3.2, 4.7, 1.4], [6.4, 3.2, 4.5, 1.5], [6.9, 3.1, 4.9, 1.5], [5.5, 2.3, 4.0, 1.3], [6.5, 2.8, 4.6, 1.5], [5.7, 2.8, 4.5, 1.3], [6.3, 3.3, 4.7, 1.6], [4.9, 2.4, 3.3, 1.0], [6.6, 2.9, 4.6, 1.3], [5.2, 2.7, 3.9, 1.4], [5.0, 2.0, 3.5, 1.0], [5.9, 3.0, 4.2, 1.5], [6.0, 2.2, 4.0, 1.0], [6.1, 2.9, 4.7, 1.4], [5.6, 2.9, 3.6, 1.3], [6.7, 3.1, 4.4, 1.4], [5.6, 3.0, 4.5, 1.5], [5.8, 2.7, 4.1, 1.0], [6.2, 2.2, 4.5, 1.5], [5.6, 2.5, 3.9, 1.1], [5.9, 3.2, 4.8, 1.8], [6.1, 2.8, 4.0, 1.3], [6.3, 2.5, 4.9, 1.5], [6.1, 2.8, 4.7, 1.2], [6.4, 2.9, 4.3, 1.3], [6.6, 3.0, 4.4, 1.4], [6.8, 2.8, 4.8, 1.4], [6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2.8, 5.6, 2.1], [7.2, 3.0, 5.8, 1.6], [7.4, 2.8, 6.1, 1.9], [7.9, 3.8, 6.4, 2.0], [6.4, 2.8, 5.6, 2.2], [6.3, 2.8, 5.1, 1.5], [6.1, 2.6, 5.6, 1.4], [7.7, 3.0, 6.1, 2.3], [6.3, 3.4, 5.6, 2.4], [6.4, 3.1, 5.5, 1.8], [6.0, 3.0, 4.8, 1.8], [6.9, 3.1, 5.4, 2.1], [6.7, 3.1, 5.6, 2.4], [6.9, 3.1, 5.1, 2.3], [5.8, 2.7, 5.1, 1.9], [6.8, 3.2, 5.9, 2.3], [6.7, 3.3, 5.7, 2.5], [6.7, 3.0, 5.2, 2.3], [6.3, 2.5, 5.0, 1.9], [6.5, 3.0, 5.2, 2.0], [6.2, 3.4, 5.4, 2.3], [5.9, 3.0, 5.1, 1.8]];\n", " var coefficients = [[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -0.0, -1.0, -0.0, -1.0, -0.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -0.0, -0.0, -1.0, -1.0, -1.0, -0.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -0.0, -0.0, -1.0, -1.0, -1.0, -0.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0], [1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0]];\n", - " var intercepts = [0.043376922607421875, 0.11445245146751404, -0.0031709671020507812];\n", + " var intercepts = [0.0433769226074, 0.114452451468, -0.00317096710205];\n", " var weights = [50, 50, 50];\n", "\n", " // Prediction:\n", @@ -257,11 +257,15 @@ " console.log(prediction);\n", "\n", " }\n", - "}\n" + "}\n", + "CPU times: user 3.71 ms, sys: 1.83 ms, total: 5.54 ms\n", + "Wall time: 4 ms\n" ] } ], "source": [ + "%%time\n", + "\n", "from sklearn_porter import Porter\n", "\n", "porter = Porter(clf, language='js')\n", @@ -269,6 +273,60 @@ "\n", "print(output)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Run classification in JavaScript:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Save the transpiled estimator:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "with open('SVC.js', 'w') as f:\n", + " f.write(output)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Prediction:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "%%bash\n", + "\n", + "if hash node 2/dev/null; then\n", + " node SVC.js 1 2 3 4\n", + "fi" + ] } ], "metadata": { diff --git a/examples/estimator/regressor/MLPRegressor/js/basics.ipynb b/examples/estimator/regressor/MLPRegressor/js/basics.ipynb index 2be8cafb..cfee4a3e 100644 --- a/examples/estimator/regressor/MLPRegressor/js/basics.ipynb +++ b/examples/estimator/regressor/MLPRegressor/js/basics.ipynb @@ -90,7 +90,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": { "scrolled": false }, @@ -198,11 +198,15 @@ " console.log(prediction);\n", "\n", " }\n", - "}\n" + "}\n", + "CPU times: user 1.59 ms, sys: 1.08 ms, total: 2.67 ms\n", + "Wall time: 1.68 ms\n" ] } ], "source": [ + "%%time\n", + "\n", "from sklearn_porter import Porter\n", "\n", "porter = Porter(reg, language='js')\n", @@ -210,6 +214,60 @@ "\n", "print(output)" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Run regression in JavaScript:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Save the transpiled estimator:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "with open('MLPRegressor.js', 'w') as f:\n", + " f.write(output)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Prediction:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "151.51211299\n" + ] + } + ], + "source": [ + "%%bash\n", + "\n", + "if hash node 2/dev/null; then\n", + " node MLPRegressor.js 0.03 0.05 0.06 0.02 -0.04 -0.03 -0.04 -0.002 0.01 -0.01\n", + "fi" + ] } ], "metadata": {