Java机器学习,第2部分

欢迎使用本教程的第二部分,该教程使用LightningScorer评分PMML文件。

让我们找出其他参数是如何工作的。

初始步骤与教程的第一部分相似。

首先获取本地副本

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方法。 首先,我们将使用其他参数来部署我们的机器学习模型。 然后,我们将检查它是否正常工作,然后使用我们的输入值进行评分。 计分之后,我们将使用其他参数。

final String url = "http://localhost:8080/model/";
        final String modelId = "test2";

        //http://dmg.org/pmml/pmml_examples/knime_pmml_examples/ElNinoPolReg.xml
        File pmmlFile = new File("/tmp/ElNinoPolReg.xml");

        CloseableHttpClient client = HttpClients.createDefault();

        // deployment
        // notice that I give a variance value as an additional parameter that I will use later
        HttpPost deployPost = new HttpPost(url + modelId + "?variance=3.25");
        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);
        // {"data":true,"success":true}
        deployPost.releaseConnection();

        // check deployed 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);
        // {"data":["test1"],"success":true}
        httpGet.releaseConnection();

        //score deployed model
        HttpPost scorePost = new HttpPost(url + modelId + "/score");
        StringEntity params = new StringEntity("{" +
                "\"fields\":" +
                "{\"latitude\":2.5," +
                "\"longitude\":11.4," +
                "\"zon_winds\":3.5," +
                "\"mer_winds\":3," +
                "\"humidity\":31.2," +
                "\"s_s_temp\":25.21" +
                "}" +
                "} ");
        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);
        // {"data":{"result":{"airtemp":29.788226026392735}},"success":true}
        scorePost.releaseConnection();


        HttpGet additionalParamGet = new HttpGet(url + modelId + "/additional");
        CloseableHttpResponse response3 = client.execute(additionalParamGet);
        String additionalParamResponse = IOUtils.toString(response3.getEntity().getContent(), Charset.forName("UTF-8"));
        System.out.println(additionalParamResponse);
        // {"data":{"variance":"3.25"},"success":true}
        additionalParamGet.releaseConnection();


        // Then you can use the variance value with your result in airtemp to calculate an interval for your score


        client.close();

翻译自: https://www.javacodegeeks.com/2018/06/machine-learning-java-part-2.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值