def prepare(self): # Create files generated by exporter in the temp dir. code = export_to_java(self.model, class_name=self.class_name) code_file_name = self._resource_tmp_dir / f"{self.class_name}.java" utils.write_content_to_file(code, code_file_name) # Move Executor.java to the same temp dir. module_path = Path(__file__).absolute().parent executor_path = self._resource_tmp_dir / "Executor.java" copyfile(module_path / "Executor.java", executor_path) # Compile all files together. subprocess.call( [str(self._javac_bin), str(code_file_name), str(executor_path)])
def prepare(self): # Create files generated by exporter in the temp dir. code = export_to_java(self.model, class_name=self.class_name) code_file_name = os.path.join(self._resource_tmp_dir, f"{self.class_name}.java") with open(code_file_name, "w") as f: f.write(code) # Move Executor.java to the same temp dir. module_path = os.path.dirname(__file__) shutil.copy(os.path.join(module_path, "Executor.java"), self._resource_tmp_dir) # Compile all files together. subprocess.call([ self._javac_bin, code_file_name, os.path.join(self._resource_tmp_dir, "Executor.java") ])
import os import m2cgen as m2c from sklearn.datasets import load_diabetes from sklearn.tree import DecisionTreeRegressor # Load data X, y = load_diabetes(return_X_y=True) # Create + Train ML model tree = DecisionTreeRegressor() tree.fit(X, y) # Translate to .java code code = m2c.export_to_java(tree, package_name=None, class_name="DecisionTreeModel", indent=4, function_name="score") # Save .java code out_path = os.path.join('test/model_small', 'DecisionTreeModel.java') with open(out_path, 'w') as f: f.write(code)
# https://github.com/BayesWitnesses/m2cgen import m2cgen as m2c code = m2c.export_to_java(model)