您搜索了某种导出机器学习模型的方法,以便可以将它们用于评估数据,并且看到可以PMML格式导出它们。 您实际上是在Java生态系统中工作的,但没有动机既不编写PMML库也不为其编写Rest API。 然后,我将向您推荐LightningScorer ,这是我的附带项目。
让我们带您参观部署和评分机器学习模型的过程。
首先获取本地副本
git clone https://github.com/sezinkarli/lightningscorer.git
并用Maven构建
mvn clean install
并通过转到目标文件夹开始
java -jar lightningscorer-uberjar-1.0.jar
现在,通过转到以下步骤来确保我们的服务器已启动并正在运行
http://localhost:8080/
。
服务器退货
{
"data": "I have come here to chew bubblegum and kick ass...",
"success": true
}
好吧,现在我们可以踢屁股了。
我将使用apache commons的http get / post方法。 首先,我们将部署我们的机器学习模型。 然后,我们将检查它是否安全可靠,然后使用我们的输入值对其进行评分。 我们将使用经过UCI机器学习存储库中虹膜数据集训练的决策树。 我们将发送4个参数(萼片长度和宽度以及花瓣长度和宽度),并且模型会将其分类为3个值之一。
final String url = "http://localhost:8080/model/";
final String modelId = "test1";//http://dmg.org/pmml/pmml_examples/KNIME_PMML_4.1_Examples/single_iris_dectree.xml
File pmmlFile = new File("/tmp/single_iris_dectree.xml");CloseableHttpClient client = HttpClients.createDefault();//first we will deploy our pmml file
HttpPost deployPost = new HttpPost(url + modelId);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("model", new File(pmmlFile.getAbsolutePath()), ContentType.APPLICATION_OCTET_STREAM, "model");
HttpEntity multipart = builder.build();
deployPost.setEntity(multipart);CloseableHttpResponse response = client.execute(deployPost);
String deployResponse = IOUtils.toString(response.getEntity().getContent(), Charset.forName("UTF-8"));
System.out.println(deployResponse);
// response is {"data":true,"success":true}
deployPost.releaseConnection();//now we check the model
HttpGet httpGet = new HttpGet(url + "ids");
response = client.execute(httpGet);
String getAllModelsResponse = IOUtils.toString(response.getEntity().getContent(), Charset.forName("UTF-8"));
System.out.println(getAllModelsResponse);
// response is {"data":["test1"],"success":true}
httpGet.releaseConnection();// lets score our deployed mode with parameters below
HttpPost scorePost = new HttpPost(url + modelId + "/score");
StringEntity params = new StringEntity("{" +"\"fields\":" +"{\"sepal_length\":4.5," +"\"sepal_width\":3.5," +"\"petal_length\":3.5," +"\"petal_width\":1" +"}" +"} ");
scorePost.addHeader("content-type", "application/json");
scorePost.setEntity(params);CloseableHttpResponse response2 = client.execute(scorePost);
String scoreResponse = IOUtils.toString(response2.getEntity().getContent(), Charset.forName("UTF-8"));
System.out.println(scoreResponse);
//response is{"data":{"result":{"class":"Iris-versicolor"}},"success":true}
scorePost.releaseConnection();client.close();
翻译自: https://www.javacodegeeks.com/2018/05/machine-learning-in-java-part-1.html