-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JS variation of the BernoulliNB estimator
- Loading branch information
Darius Morawiec
committed
Oct 1, 2017
1 parent
98a40a8
commit 9784d6b
Showing
5 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
sklearn_porter/classifier/BernoulliNB/templates/js/class.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var {class_name} = function() {{ | ||
|
||
{method} | ||
|
||
}}; | ||
|
||
if (typeof process !== 'undefined' && typeof process.argv !== 'undefined') {{ | ||
if (process.argv.length - 2 == {n_features}) {{ | ||
var argv = process.argv.slice(2); | ||
var prediction = new {class_name}().{method_name}(argv); | ||
console.log(prediction); | ||
}} | ||
}} |
33 changes: 33 additions & 0 deletions
33
sklearn_porter/classifier/BernoulliNB/templates/js/method.predict.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
this.{method_name} = function(atts) {{ | ||
|
||
{priors} | ||
{neg_probs} | ||
{del_probs} | ||
|
||
var jll = new Array({n_classes}); | ||
for (var i = 0; i < {n_classes}; i++) {{ | ||
var sum = 0.; | ||
for (var j = 0; j < {n_features}; j++) {{ | ||
sum += atts[i] * delProbs[j][i]; | ||
}} | ||
jll[i] = sum; | ||
}} | ||
for (var i = 0; i < {n_classes}; i++) {{ | ||
var sum = 0.; | ||
for (var j = 0; j < {n_features}; j++) {{ | ||
sum += negProbs[i][j]; | ||
}} | ||
jll[i] += priors[i] + sum; | ||
}} | ||
|
||
var highestLikeli = -1; | ||
var classIndex = -1; | ||
for (var i = 0; i < {n_classes}; i++) {{ | ||
if (jll[i] > highestLikeli) {{ | ||
highestLikeli = jll[i]; | ||
classIndex = i; | ||
}} | ||
}} | ||
return classIndex; | ||
}}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import unittest | ||
|
||
from sklearn.naive_bayes import BernoulliNB | ||
|
||
from ..Classifier import Classifier | ||
from ...language.JavaScript import JavaScript | ||
|
||
|
||
class BernoulliNBJSTest(JavaScript, Classifier, unittest.TestCase): | ||
|
||
def setUp(self): | ||
super(BernoulliNBJSTest, self).setUp() | ||
self.mdl = BernoulliNB() | ||
|
||
def tearDown(self): | ||
super(BernoulliNBJSTest, self).tearDown() | ||
|
||
@unittest.skip('BernoulliNB is just suitable for discrete data.') | ||
def test_random_features_w_iris_data(self): | ||
pass | ||
|
||
@unittest.skip('BernoulliNB is just suitable for discrete data.') | ||
def test_existing_features_w_binary_data(self): | ||
pass | ||
|
||
@unittest.skip('BernoulliNB is just suitable for discrete data.') | ||
def test_random_features_w_binary_data(self): | ||
pass | ||
|
||
@unittest.skip('BernoulliNB is just suitable for discrete data.') | ||
def test_random_features_w_digits_data(self): | ||
pass | ||
|
||
@unittest.skip('BernoulliNB is just suitable for discrete data.') | ||
def test_existing_features_w_digits_data(self): | ||
pass |