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