Skip to content

Commit

Permalink
Add prediction in the used programming language
Browse files Browse the repository at this point in the history
  • Loading branch information
Darius Morawiec committed Dec 4, 2017
1 parent 6da2f7c commit 65439b9
Show file tree
Hide file tree
Showing 15 changed files with 1,219 additions and 689 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)"
]
},
{
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)"
]
},
{
Expand All @@ -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]}]"
]
}
],
Expand All @@ -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": [
{
Expand All @@ -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"
]
}
],
Expand Down
68 changes: 63 additions & 5 deletions examples/estimator/classifier/BernoulliNB/js/basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {
"scrolled": false
},
Expand Down Expand Up @@ -133,28 +133,86 @@
" 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",
" var prediction = clf.predict(features);\n",
" 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",
"output = porter.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('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": {
Expand Down
Loading

0 comments on commit 65439b9

Please sign in to comment.