h2o-genmodel.jar加载模型编译运行

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Katherine_hsr/article/details/82056347

h2o生成模型

import h2o
from h2o.automl import H2OAutoML
h2o.init()

path = '/Users/huoshirui/Desktop/xyworking/pythonData/dataClean/tengxun_final.csv'
tengxun_df = h2o.import_file(path)
df = tengxun_df[:, ["target", "tx_score", "tx_new_risk_code1", "tx_new_risk_code2", "tx_new_risk_code3", "tx_new_risk_code4",
                    "tx_new_risk_code5", "tx_new_risk_code6", "tx_new_risk_code7", "tx_new_risk_code503", "tx_new_risk_sumList",
                    "tx_new_is_riskList", "tx_score_0", "tx_score_1", "tx_score_2", "tx_score_3"]]

splits = df.split_frame(ratios = [0.7], seed = 666)
train = splits[0]
test = splits[1]

x = train.columns
y = 'target'
x.remove(y)

train[y] = train[y].asfactor()
test[y] = test[y].asfactor()

auto_model = H2OAutoML(max_runtime_secs = 100)
auto_model.train(x = x, y = y,
                 training_frame = train
                 )


#h2o.download_pojo(auto_model)

modelfile = auto_model.download_mojo(path="/Users/huoshirui/Desktop/xyworking/pythonData/dataClean/", get_genmodel_jar=True)
print("Model saved to " + modelfile)

执行代码后将会生成模型和jar包

Eclipse方式

新建eclipse java项目
这里写图片描述
配置环境变量,引入h2o-genmodel.jar,右键java项目->build path->Add External Archives选择jar,确认选中后便将该jar包引入项目中
新建源码java类main并创建main函数

import java.io.*;
import java.util.HashMap;
import java.util.Map;

import hex.genmodel.easy.RowData;
import hex.genmodel.easy.exception.PredictException;
import hex.genmodel.easy.EasyPredictModelWrapper;
import hex.genmodel.easy.prediction.*;
import hex.genmodel.MojoModel;

        public class main {
          public static void main(String[] args) throws Exception {
              Map<String, String> row = new HashMap<String, String>();
              row.put("tx_score", "23");
              row.put("tx_new_risk_code1", "0");
              row.put("tx_new_risk_code2", "0");
              row.put("tx_new_risk_code3", "0");
              row.put("tx_new_risk_code4", "0");
              row.put("tx_new_risk_code5", "0");
              row.put("tx_new_risk_code6", "0");
              row.put("tx_new_risk_code7", "0");
              row.put("tx_new_risk_code503", "0");
              row.put("tx_new_risk_sumList", "0");
              row.put("tx_new_is_riskList", "0");
              row.put("tx_score_0", "1");
              row.put("tx_score_1", "0");
              row.put("tx_score_2", "0");
              row.put("tx_score_3", "0");
              myh2o(row, "/Users/huoshirui/Desktop/xyworking/pythonData/dataClean/GBM_grid_0_AutoML_20180825_190112_model_6.zip");

          }

            public static void myh2o(Map<String, String> params, String path) throws PredictException, IOException{
                EasyPredictModelWrapper model = new EasyPredictModelWrapper(MojoModel.load(path));

                RowData row = new RowData();
                row.putAll(params);

                BinomialModelPrediction p = model.predictBinomial(row);
                System.out.println("Has penetrated the prostatic capsule (1=yes; 0=no): " + p.label);
                System.out.print("Class probabilities: ");
                for (int i = 0; i < p.classProbabilities.length; i++) {
                  if (i > 0) {
                    System.out.print(",");
                  }
                  System.out.print(p.classProbabilities[i]);
                }
                System.out.println("");

            }
        }

最终运行结果为:

Has penetrated the prostatic capsule (1=yes; 0=no): 0
Class probabilities: 0.9006471329627311,0.09935286703726891

命令行方式

根据上述代码创建java文件main.java
进入生成的模型文件路径中cd XXXpath
新建一个main.java touch main.java
编辑main.java vi main.java
将上述代码复制到main.java中
然后进行编译生成main.class

javac -cp h2o-genmodel.jar -J-Xms2g -J-XX:MaxPermSize=128m main.java

执行main.class

java -cp .:h2o-genmodel.jar main

执行结果

Has penetrated the prostatic capsule (1=yes; 0=no): 0
Class probabilities: 0.9006471329627311,0.09935286703726891

猜你喜欢

转载自blog.csdn.net/Katherine_hsr/article/details/82056347
今日推荐