{"id":19633405,"url":"https://github.com/dvilaverde/sklearn2java","last_synced_at":"2026-01-27T04:07:36.836Z","repository":{"id":64696848,"uuid":"577028573","full_name":"dvilaverde/SkLearn2Java","owner":"dvilaverde","description":"A parser for scikit-learn exported text models to execute in the Java runtime.","archived":false,"fork":false,"pushed_at":"2024-04-28T18:13:58.000Z","size":101,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-28T07:40:30.704Z","etag":null,"topics":["decision-tree","decisiontreeclassifier","java","machine-learning","ml","random-forest","randomforestclassifier","sci-kit","scikit","scikit-learn","scikitlearn-machine-learning","sklearn"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dvilaverde.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-12-11T18:52:02.000Z","updated_at":"2025-04-19T22:49:15.000Z","dependencies_parsed_at":"2024-04-28T19:25:37.703Z","dependency_job_id":"f06ea2ef-ade8-4fd1-afa5-cf6c0a82df0c","html_url":"https://github.com/dvilaverde/SkLearn2Java","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/dvilaverde/SkLearn2Java","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dvilaverde%2FSkLearn2Java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dvilaverde%2FSkLearn2Java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dvilaverde%2FSkLearn2Java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dvilaverde%2FSkLearn2Java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dvilaverde","download_url":"https://codeload.github.com/dvilaverde/SkLearn2Java/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dvilaverde%2FSkLearn2Java/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28801134,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-27T03:44:14.111Z","status":"ssl_error","status_checked_at":"2026-01-27T03:43:33.507Z","response_time":168,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["decision-tree","decisiontreeclassifier","java","machine-learning","ml","random-forest","randomforestclassifier","sci-kit","scikit","scikit-learn","scikitlearn-machine-learning","sklearn"],"created_at":"2024-11-11T12:17:16.712Z","updated_at":"2026-01-27T04:07:36.809Z","avatar_url":"https://github.com/dvilaverde.png","language":"Java","readme":"# SkLearn2Java\n\nThis project aims to used text exported ML models generated by sci-kit learn and make them usable in Java.\n\n[![javadoc](https://javadoc.io/badge2/rocks.vilaverde/scikit-learn-2-java/javadoc.svg)](https://javadoc.io/doc/rocks.vilaverde/scikit-learn-2-java)\n\n## Support\n* The tree.DecisionTreeClassifier is supported\n  * Supports `predict()`,\n  * Supports `predict_proba()` when `export_text()` configured with `show_weights=True`\n* The tree.RandomForestClassifier is supported\n  * Supports `predict()`,\n  * Supports `predict_proba()` when `export_text()` configured with `show_weights=True`\n\n## Installing\n\n### Importing Maven Dependency\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003erocks.vilaverde\u003c/groupId\u003e\n  \u003cartifactId\u003escikit-learn-2-java\u003c/artifactId\u003e\n  \u003cversion\u003e1.1.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## DecisionTreeClassifier\n\nAs an example, a DecisionTreeClassifier model trained on the Iris dataset and exported using `sklearn.tree`\n`export_text()` as shown below:\n\n```\n\u003e\u003e\u003e from sklearn.datasets import load_iris\n\u003e\u003e\u003e from sklearn.tree import DecisionTreeClassifier\n\u003e\u003e\u003e from sklearn.tree import export_text\n\u003e\u003e\u003e iris = load_iris()\n\u003e\u003e\u003e X = iris['data']\n\u003e\u003e\u003e y = iris['target']\n\u003e\u003e\u003e decision_tree = DecisionTreeClassifier(random_state=0, max_depth=2)\n\u003e\u003e\u003e decision_tree = decision_tree.fit(X, y)\n\u003e\u003e\u003e r = export_text(decision_tree, feature_names=iris['feature_names'], show_weights=True, max_depth=sys.maxsize)\n\u003e\u003e\u003e print(r)\n\n|--- petal width (cm) \u003c= 0.80\n|   |--- class: 0\n|--- petal width (cm) \u003e  0.80\n|   |--- petal width (cm) \u003c= 1.75\n|   |   |--- class: 1\n|   |--- petal width (cm) \u003e  1.75\n|   |   |--- class: 2\n```\n\nThe exported text can then be executed in Java. Note that when calling `export_text` it is \nrecommended that `max_depth` be set to `sys.maxsize` so that the tree isn't truncated.\n\n### Java Example\nIn this example the iris model exported using `export_text` is parsed, features are created as a Java Map\nand the decision tree is asked to predict the class.\n\n```\n    Reader tree = getTrainedModel(\"iris.model\");\n    final Classifier\u003cInteger\u003e decisionTree = DecisionTreeClassifier.parse(tree,\n                PredictionFactory.INTEGER);\n\n    Features features = Features.of(\"sepal length (cm)\",\n                \"sepal width (cm)\",\n                \"petal length (cm)\",\n                \"petal width (cm)\");\n    FeatureVector fv = features.newSample();\n    fv.add(0, 3.0).add(1, 5.0).add(2, 4.0).add(3, 2.0);\n    \n    Integer prediction = decisionTree.predict(fv);\n    System.out.println(prediction.toString());\n```\n\n## RandomForestClassifier\n\nTo use a RandomForestClassifier that has been trained on the Iris dataset, each of the `estimators`  \nin the classifiers need to be and exported using `from sklearn.tree export export_text` as shown below:\n\n```\n\u003e\u003e\u003e from sklearn import datasets\n\u003e\u003e\u003e from sklearn import tree\n\u003e\u003e\u003e from sklearn.ensemble import RandomForestClassifier\n\u003e\u003e\u003e \n\u003e\u003e\u003e import os\n\u003e\u003e\u003e \n\u003e\u003e\u003e iris = datasets.load_iris()\n\u003e\u003e\u003e X = iris.data\n\u003e\u003e\u003e y = iris.target\n\u003e\u003e\u003e \n\u003e\u003e\u003e clf = RandomForestClassifier(n_estimators = 50, n_jobs=8)\n\u003e\u003e\u003e model = clf.fit(X, y)\n\u003e\u003e\u003e \n\u003e\u003e\u003e for i, t in enumerate(clf.estimators_):\n\u003e\u003e\u003e     with open(os.path.join('/tmp/estimators', \"iris-\" + str(i) + \".txt\"), \"w\") as file1:\n\u003e\u003e\u003e         text_representation = tree.export_text(t, feature_names=iris.feature_names, show_weights=True, decimals=4, max_depth=sys.maxsize)\n\u003e\u003e\u003e         file1.write(text_representation)\n```\n\nOnce all the estimators are exported into `/tmp/estimators`, you can create a TAR archive, for example:\n```bash\ncd /tmp/estimators\ntar -czvf /tmp/iris.tgz .\n```\n\nThen you can use the RandomForestClassifier class to parse the TAR archive.\n\n```\n    import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;\n    ...\n    \n    TarArchiveInputStream tree = getArchive(\"iris.tgz\");\n    final Classifier\u003cDouble\u003e decisionTree = RandomForestClassifier.parse(tree,\n                PredictionFactory.DOUBLE);\n```\n\n## Testing\nTesting was done using models exported using sci-kit learn version 1.1.3, but should \nwork with newer versions of sci-kit learn.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdvilaverde%2Fsklearn2java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdvilaverde%2Fsklearn2java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdvilaverde%2Fsklearn2java/lists"}