- 训练好模型后,保存模型
joblib.dump(xgb,"xgb_model.pkl")
- 新建一个python文件,useModel.py。
import sys
if __name__ == '__main__':
model = joblib.load("xgb_model.pkl")
value=[]
for i in range(len(sys.argv)):
value.append(float(sys.argv[i]))
predict = model.predict(value)
- java可以通过调用python解释器,即使用jpython。但是此种方式必须保证python代码内不包含第三方库。如果要包含第三方库,需要通过如下方式:
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("start python");
String[] arg = new String[]{"python", "test\\useModel.py","123","4"};
Process pr = Runtime.getRuntime().exec(arg);
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
System.out.println("end");
pr.waitFor();
}
|