sklearn-porter

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

LinearSVC

Documentation: sklearn.svm.LinearSVC


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 import svm

clf = svm.LinearSVC(C=1., random_state=0)
clf.fit(X, y)


/opt/miniconda/envs/sklearn-porter/lib/python2.7/site-packages/sklearn/svm/base.py:922: ConvergenceWarning: Liblinear failed to converge, increase the number of iterations.
  "the number of iterations.", ConvergenceWarning)
Out[3]:
LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
     intercept_scaling=1, loss='squared_hinge', max_iter=1000,
     multi_class='ovr', penalty='l2', random_state=0, tol=0.0001,
     verbose=0)

Transpile classifier


In [4]:
from sklearn_porter import Porter

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

print(output)


class LinearSVC {

    private double[][] coefficients;
    private double[] intercepts;
    
    public LinearSVC(double[][] coefficients, double[] intercepts) {
        this.coefficients = coefficients;
        this.intercepts = intercepts;
    }

    public int predict(double[] features) {
        int classIdx = 0;
        double classVal = Double.NEGATIVE_INFINITY;
        for (int i = 0, il = this.intercepts.length; i < il; i++) {
            double prob = 0.;
            for (int j = 0, jl = this.coefficients[0].length; j < jl; j++) {
                prob += this.coefficients[i][j] * features[j];
            }
            if (prob + this.intercepts[i] > classVal) {
                classVal = prob + this.intercepts[i];
                classIdx = i;
            }
        }
        return classIdx;
    }

    public static void main(String[] args) {
        if (args.length == 4) {

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

            // Parameters:
            double[][] coefficients = {{0.1842387105153816, 0.45123226735416666, -0.8079442418237148, -0.4507120994138801}, {0.05538075463135362, -0.9011091317807134, 0.40964725305179495, -0.9616708418438406}, {-0.8507797840387784, -0.9866797559823655, 1.380978948733964, 1.865367212912733}};
            double[] intercepts = {0.10956037385996253, 1.6860176665898299, -1.7096500573837932};

            // Prediction:
            LinearSVC clf = new LinearSVC(coefficients, intercepts);
            int estimation = clf.predict(features);
            System.out.println(estimation);

        }
    }
}

Run classification in Java


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

# Compile model:
# $ javac -cp . LinearSVC.java

# Run classification:
# $ java LinearSVC 1 2 3 4