nok / sklearn-porter

Transpile trained scikit-learn estimators to C, Java, JavaScript and others.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Invalid java generated for random forrest

markusheiden opened this issue · comments

There are several compile errors when transpiling random forrests to java:

  • At the start of each predict_x method:
    int[] classes = new int[[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]];should be
    int[] classes = new int[] { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };.
  • At the end of each predict_x method:
    for (int i = 1; i < [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]; i++)should be
    for (int i = 1; i < classes.length; i++).
  • At the start of each predict method:
    int n_classes = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]; int[] classes = new int[n_classes]; should be
    int[] classes = new int[] { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }; int n_classes = classes.length;

Maybe there are other errors too, because the transpiled random forrest does not produce the same result as python.

Thanks for your feedback, but I can't reproduce your critical issues.
Can you install the latest version of release/1.0.0 and test it again please?

pip install https://github.com/nok/sklearn-porter/zipball/release/1.0.0

Here is a basic example:

from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier

from sklearn_porter import Estimator


# 1. Load data and train a dummy classifier:
X, y = load_iris(return_X_y=True)
clf = RandomForestClassifier()
clf.fit(X, y)

# 2. Port or transpile an estimator:
est = Estimator(clf, language='java', template='exported')

# output = est.port()
# print(output)

# 3. Validate and compare the predictions:
score = est.test(X)
print(score)  # the score should be 1.0

https://github.com/nok/sklearn-porter/tree/release/1.0.0#basics