sklearn-porter

Repository: https://github.com/nok/sklearn-porter

BernoulliNB

Documentation: sklearn.naive_bayes.BernoulliNB


In [1]:
import sys
sys.path.append('../../../../..')

Load data


In [2]:
from sklearn.datasets import load_iris

iris_data = load_iris()

X = iris_data.data
y = iris_data.target

print(X.shape, y.shape)


((150, 4), (150,))

Train classifier


In [3]:
from sklearn.naive_bayes import BernoulliNB

clf = BernoulliNB()
clf.fit(X, y)


Out[3]:
BernoulliNB(alpha=1.0, binarize=0.0, class_prior=None, fit_prior=True)

Transpile classifier


In [4]:
from sklearn_porter import Porter

porter = Porter(clf, language='java')
output = porter.export(export_data=True)

print(output)


import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import com.google.gson.Gson;


class BernoulliNB {

    private class Classifier {
        private double[] priors;
        private double[][] negProbs;
        private double[][] delProbs;
    }

    private Classifier clf;

    public BernoulliNB(String file) throws FileNotFoundException {
        String jsonStr = new Scanner(new File(file)).useDelimiter("\\Z").next();
        this.clf = new Gson().fromJson(jsonStr, Classifier.class);
    }

    public int predict(double[] features) {
        int nClasses = this.clf.priors.length;
        int nFeatures = this.clf.delProbs.length;

        double[] jll = new double[nClasses];
        for (int i = 0; i < nClasses; i++) {
            double sum = 0.;
            for (int j = 0; j < nFeatures; j++) {
                sum += features[i] * this.clf.delProbs[j][i];
            }
            jll[i] = sum;
        }
        for (int i = 0; i < nClasses; i++) {
            double sum = 0.;
            for (int j = 0; j < nFeatures; j++) {
                sum += this.clf.negProbs[i][j];
            }
            jll[i] += this.clf.priors[i] + sum;
        }

        int classIndex = 0;
        for (int i = 0; i < nClasses; i++) {
            classIndex = jll[i] > jll[classIndex] ? i : classIndex;
        }
        return classIndex;
    }

    public static void main(String[] args) throws FileNotFoundException {
        if (args.length > 0 && args[0].endsWith(".json")) {

            // Features:
            double[] features = new double[args.length-1];
            for (int i = 1, l = args.length; i < l; i++) {
                features[i - 1] = Double.parseDouble(args[i]);
            }

            // Parameters:
            String modelData = args[0];

            // Estimators:
            BernoulliNB clf = new BernoulliNB(modelData);

            // Prediction:
            int prediction = clf.predict(features);
            System.out.println(prediction);

        }
    }
}

Run classification in Java


In [5]:
# Save classifier:
# with open('BernoulliNB.java', 'w') as f:
#     f.write(output)

# Check model data:
# $ cat data.json

# Download dependencies:
# $ wget -O gson.jar http://central.maven.org/maven2/com/google/code/gson/gson/2.8.5/gson-2.8.5.jar

# Compile model:
# $ javac -cp .:gson.jar BernoulliNB.java

# Run classification:
# $ java -cp .:gson.jar BernoulliNB data.json 1 2 3 4