python으로 random forest 모델을 만들고 pickle로 모델까지는 추출했습니다.
그런데 모델로 predict해야 할 시스템이 JAVA환경이라, JAVA에서 호출했을 때 Predict될 수 있도록 .py 화일을 만들어 달라고 하시는데, 혹시 어떻게 해야 할지 알 수 있을까요?
제가 이해한 것이 맞다면 Java에서 호출할 수 있도록 .py 파일만 전달드리면 될 것 같습니다.
만약 .py 파일 이름이 hello.py라고 한다면 자바에서는 간단하게는 아래와 같이 호출하면 되고
Process p = Runtime.getRuntime().exec("python hello.py");
좀 더 복잡하게는 아래와 같이 호출하면 됩니다.
@Test public void givenPythonScript_whenPythonProcessExecuted_thenSuccess() throws ExecuteException, IOException { String line = "python " + resolvePythonScriptPath("hello.py"); CommandLine cmdLine = CommandLine.parse(line); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); DefaultExecutor executor = new DefaultExecutor(); executor.setStreamHandler(streamHandler); int exitCode = executor.execute(cmdLine); assertEquals("No errors should be detected", 0, exitCode); assertEquals("Should contain script output: ", "Hello Baeldung Readers!!", outputStream.toString() .trim()); }
즉, python 파일을 커맨드 라인에서 실행할 수 있으면 이후 호출은 자바쪽에서 하면 될 것 같습니다.
제가 이해한 것이 맞다면 Java에서 호출할 수 있도록 .py 파일만 전달드리면 될 것 같습니다.
만약 .py 파일 이름이 hello.py라고 한다면 자바에서는 간단하게는 아래와 같이 호출하면 되고
좀 더 복잡하게는 아래와 같이 호출하면 됩니다.
즉, python 파일을 커맨드 라인에서 실행할 수 있으면 이후 호출은 자바쪽에서 하면 될 것 같습니다.