Tuesday, June 19, 2018

machine learning ass-kicking in java part 2

Welcome to the second part of the tutorial for scoring your PMML files using  LightningScorer, which is a side project of mine.

Let's find out how additional parameters work.
The initial steps are similar to the first part of the tutorial.

Get your local copy first
git clone https://github.com/sezinkarli/lightningscorer.git

and build it with maven
mvn clean install

and start it by going to your target folder
java -jar lightningscorer-uberjar-1.0.jar


Now lets make sure our server is up and running by going to
http://localhost:8080/
.
Server returns
{
"data": "I have come here to chew bubblegum and kick ass...",
"success": true
}

Ok then we are now ready to kick ass, again.

I'll use apache commons' http get/post methods. First, we'll deploy our machine learning model with an additional parameter. Then we will check if it's working and then use our input values and score it.  After the scoring we will use our additional parameter.


01final String url = "http://localhost:8080/model/";
02final String modelId = "test2";
03 
05File pmmlFile = new File("/tmp/ElNinoPolReg.xml");
06 
07CloseableHttpClient client = HttpClients.createDefault();
08 
09// deployment
10// notice that I give a variance value as an additional parameter that I will use later
11HttpPost deployPost = new HttpPost(url + modelId + "?variance=3.25");
12MultipartEntityBuilder builder = MultipartEntityBuilder.create();
13builder.addBinaryBody("model", new File(pmmlFile.getAbsolutePath()), ContentType.APPLICATION_OCTET_STREAM, "model");
14HttpEntity multipart = builder.build();
15deployPost.setEntity(multipart);
16 
17CloseableHttpResponse response = client.execute(deployPost);
18String deployResponse = IOUtils.toString(response.getEntity().getContent(), Charset.forName("UTF-8"));
19System.out.println(deployResponse);
20// {"data":true,"success":true}
21deployPost.releaseConnection();
22 
23// check deployed model
24HttpGet httpGet = new HttpGet(url + "ids");
25 
26response = client.execute(httpGet);
27String getAllModelsResponse = IOUtils.toString(response.getEntity().getContent(), Charset.forName("UTF-8"));
28System.out.println(getAllModelsResponse);
29// {"data":["test1"],"success":true}
30httpGet.releaseConnection();
31 
32//score deployed model
33HttpPost scorePost = new HttpPost(url + modelId + "/score");
34StringEntity params = new StringEntity("{" +
35        "\"fields\":" +
36        "{\"latitude\":2.5," +
37        "\"longitude\":11.4," +
38        "\"zon_winds\":3.5," +
39        "\"mer_winds\":3," +
40        "\"humidity\":31.2," +
41        "\"s_s_temp\":25.21" +
42        "}" +
43        "} ");
44scorePost.addHeader("content-type", "application/json");
45scorePost.setEntity(params);
46 
47CloseableHttpResponse response2 = client.execute(scorePost);
48String scoreResponse = IOUtils.toString(response2.getEntity().getContent(), Charset.forName("UTF-8"));
49System.out.println(scoreResponse);
50// {"data":{"result":{"airtemp":29.788226026392735}},"success":true}
51scorePost.releaseConnection();
52 
53 
54HttpGet additionalParamGet = new HttpGet(url + modelId + "/additional");
55CloseableHttpResponse response3 = client.execute(additionalParamGet);
56String additionalParamResponse = IOUtils.toString(response3.getEntity().getContent(), Charset.forName("UTF-8"));
57System.out.println(additionalParamResponse);
58// {"data":{"variance":"3.25"},"success":true}
59additionalParamGet.releaseConnection();
60 
61 
62// Then you can use the variance value with your result in airtemp to calculate an interval for your score
63 
64 
65client.close();

Monday, May 28, 2018

machine learning ass-kicking in java part 1

You searched for some way to export your machine learning models so you can use them for evaluating your data and you see that you can export them in PMML format. You actually work in Java ecosystem but not motivated to write neither your PMML library nor an rest api for it. Then I will recommend you LightningScorer, which is a side project of mine.

Let's take you a tour for deploying, and scoring your machine learning models.


Get your local copy first
git clone https://github.com/sezinkarli/lightningscorer.git

and build it with maven
mvn clean install

and start it by going to your target folder
java -jar lightningscorer-uberjar-1.0.jar


Now lets make sure our server is up and running by going to 
http://localhost:8080/
.
Server returns
{
"data": "I have come here to chew bubblegum and kick ass...",
"success": true
}

Ok then we are now ready to kick ass.

I'll use apache commons' http get/post methods. First, we'll deploy our machine learning model. Then we will check if it's safe and sound and then use our input values and score it. We will use a decision tree trained with iris data set from UCI machine learning repository. We will send 4 parameters ( sepal length and width and petal length and width) and the model will classify it for us into one of 3 values.


01final String url = "http://localhost:8080/model/";
02final String modelId = "test1";
03 
05File pmmlFile = new File("/tmp/single_iris_dectree.xml");
06 
07CloseableHttpClient client = HttpClients.createDefault();
08 
09//first we will deploy our pmml file
10HttpPost deployPost = new HttpPost(url + modelId);
11MultipartEntityBuilder builder = MultipartEntityBuilder.create();
12builder.addBinaryBody("model", new File(pmmlFile.getAbsolutePath()), ContentType.APPLICATION_OCTET_STREAM, "model");
13HttpEntity multipart = builder.build();
14deployPost.setEntity(multipart);
15 
16CloseableHttpResponse response = client.execute(deployPost);
17String deployResponse = IOUtils.toString(response.getEntity().getContent(), Charset.forName("UTF-8"));
18System.out.println(deployResponse);
19// response is {"data":true,"success":true}
20deployPost.releaseConnection();
21 
22 //now we check the model
23HttpGet httpGet = new HttpGet(url + "ids");
24response = client.execute(httpGet);
25String getAllModelsResponse = IOUtils.toString(response.getEntity().getContent(), Charset.forName("UTF-8"));
26System.out.println(getAllModelsResponse);
27// response is {"data":["test1"],"success":true} 
28httpGet.releaseConnection();
29 
30// lets score our deployed mode with parameters below
31HttpPost scorePost = new HttpPost(url + modelId + "/score");
32StringEntity params = new StringEntity("{" +
33        "\"fields\":" +
34            "{\"sepal_length\":4.5," +
35        "\"sepal_width\":3.5," +
36        "\"petal_length\":3.5," +
37        "\"petal_width\":1" +
38        "}" +
39        "} ");
40scorePost.addHeader("content-type", "application/json");
41scorePost.setEntity(params);
42 
43CloseableHttpResponse response2 = client.execute(scorePost);
44String scoreResponse = IOUtils.toString(response2.getEntity().getContent(), Charset.forName("UTF-8"));
45System.out.println(scoreResponse);
46//response is{"data":{"result":{"class":"Iris-versicolor"}},"success":true}
47scorePost.releaseConnection();
48 
49client.close();

Saturday, May 12, 2018

lightningscorer - blazing fast pmml scoring web service

I've been working on a side project lately dubbed "LightningScorer". You can basically deploy your PMML files (which is the xml language for machine learning models) and score them with your inputs. My main inspiration was Openscoring project and I'm quite happy to build something faster than that. LightningScorer is lightweight and easy to use.