This is how you can train and save a Machine Learning model in R and then load it in Java to make predictions.
The demo is based on a PMML model of the classical Iris data set trained by the following R 3.6 script using a decision tree:
library("rpart")
library("rpart.plot")
library("XML")
library("pmml")
# Uncomment the following line specifying your custom local path following the conventions of your OS:
# setwd("/my/output/directory/path/")
data("iris")
tree <- rpart(Species ~ ., data = iris, method = "class")
rpart.plot(tree)
saveXML(pmml(tree), "rpart.pmml")The PMML file was saved in pmml-demo/src/main/resources/models/rpart.pmml
- Java 8
- Gradle 5.5
cd demo/src
./gradlew clean buildcd demo/src
./gradlew bootRunor:
cd demo/build/libs
java -jar demo-0.0.1-SNAPSHOT.jarhttp://localhost:8080/predict?petalLength=2.6&petalWidth=1.7&sepalLength=0.5&sepalWidth=1.9
Expected output:
{
"Predicted_Species": "versicolor",
"Probability_setosa": "0.0",
"Probability_versicolor": "0.9074074074074074",
"Probability_virginica": "0.09259259259259259"
}