Skip to content

Commit

Permalink
Add new templates, examples and tests for the BernoulliNB classifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Darius Morawiec committed Dec 2, 2017
1 parent b68c5df commit 7eae57d
Show file tree
Hide file tree
Showing 12 changed files with 646 additions and 14 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ All notable changes to this project will be documented in this file.
- `ensemble.ExtraTreesClassifier` [notebook](examples/estimator/classifier/ExtraTreesClassifier/java/basics_imported.ipynb) ([#9979a94](https:/nok/sklearn-porter/commit/9979a948f51fef527b792babe06103c2c0dd5db4))
- `tree.DecisionTreeClassifier` [notebook](examples/estimator/classifier/DecisionTreeClassifier/java/basics_imported.ipynb) ([#9979a94](https:/nok/sklearn-porter/commit/9979a948f51fef527b792babe06103c2c0dd5db4))
- `neighbors.KNeighborsClassifier` [notebook](examples/estimator/classifier/KNeighborsClassifier/java/basics_imported.ipynb) ([#1b81d3f](https:/nok/sklearn-porter/commit/1b81d3f0e9cbe52203bbfde4cf73df17eacf4fdd#diff-86bc174b477eabd59f582e0e2882dcb3R54))
- `naive_bayes.GaussianNB` [notebook](examples/estimator/classifier/GaussianNB/java/basics_imported.ipynb) ([#b68c5df](https:/nok/sklearn-porter/commit/b68c5df241fec0b654d919066dca0663d909bde1))
- `neural_network.MLPClassifier` [notebook](examples/estimator/classifier/MLPClassifier/java/basics_imported.ipynb) ([#b988f57](https:/nok/sklearn-porter/commit/b988f575db10133d41b1c80773bfe814b7271900), [#d8ff774](https:/nok/sklearn-porter/commit/d8ff77400c20ce41e318403bb12d2d65eeb3d309))
- JavaScript:
- `ensemble.AdaBoostClassifier` [notebook](examples/estimator/classifier/AdaBoostClassifier/js/basics_imported.ipynb) ([#9fdcc49](https:/nok/sklearn-porter/commit/9fdcc49355617668c3bf451a8d5828880245a4ae), [#dd70ac7](https:/nok/sklearn-porter/commit/dd70ac7e63cecad9028e9dea32efb7e1d2e43237), [#2401fba](https:/nok/sklearn-porter/commit/2401fba9938880bf420de0253e489c22e7b040e4))
Expand Down
86 changes: 81 additions & 5 deletions examples/estimator/classifier/BernoulliNB/java/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 @@ -140,9 +140,9 @@
" }\n",
"\n",
" // Parameters:\n",
" final double[] priors = {-1.0986122886681096, -1.0986122886681096, -1.0986122886681096};\n",
" final double[][] 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",
" final double[][] 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",
" final double[] priors = {-1.09861228867, -1.09861228867, -1.09861228867};\n",
" final double[][] 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",
" final double[][] 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",
" // Prediction:\n",
" BernoulliNB clf = new BernoulliNB(priors, negProbs, delProbs);\n",
Expand All @@ -151,18 +151,94 @@
"\n",
" }\n",
" }\n",
"}\n"
"}\n",
"CPU times: user 1.04 ms, sys: 722 µs, total: 1.76 ms\n",
"Wall time: 1.25 ms\n"
]
}
],
"source": [
"%%time\n",
"\n",
"from sklearn_porter import Porter\n",
"\n",
"porter = Porter(clf)\n",
"output = porter.export()\n",
"\n",
"print(output)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Run classification in Java:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Save the transpiled estimator:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"with open('BernoulliNB.java', 'w') as f:\n",
" f.write(output)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Compiling:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%%bash\n",
"\n",
"javac -cp . BernoulliNB.java"
]
},
{
"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",
"java -cp . BernoulliNB 1 2 3 4"
]
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 7eae57d

Please sign in to comment.