Java机器学习,第1部分
生活随笔
收集整理的這篇文章主要介紹了
Java机器学习,第1部分
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
您搜索了某種導出機器學習模型的方法,以便可以將它們用于評估數據,并且看到可以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
總結
以上是生活随笔為你收集整理的Java机器学习,第1部分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux程序编译(linux 程序编译
- 下一篇: java 观察者模式示例_Java中的观