生产环境很多时候是需要实时对数据进行预测的,即离线训练好模型后将模型保存为模型文件,然后在线服务将模型加载到内存
引入pom.xml
1 2 3 4 5 6 7 8 9 10 11
| <dependency> <groupId>ai.catboost</groupId> <artifactId>catboost-common</artifactId> <version>0.26</version> </dependency>
<dependency> <groupId>ai.catboost</groupId> <artifactId>catboost-prediction</artifactId> <version>0.26</version> </dependency>
|
样例代码
需要注意的地方
- catboost模型需要同时传入float特征和category特征值
- 需要将模型的预测值通过sigmod激活函数来映射为概率值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import ai.catboost.CatBoostModel;
List<String> floatFeatures; List<String> categoryFeatures; Map<String, String> feature = new HashMap();
try { CatBoostModel model = CatBoostModel.loadModel("model.cbm"); CatBoostModel model = CatBoostModel.loadModel(InputStream is);
float[] floatVector = new float[floatFeatures.size()]; String[] categoryVector = new String[categoryFeatures.size()]; for (int i = 0; i < floatFeatures.size(); i++) { String f = feature.getOrDefault(floatFeatures.get(i), "-1.0"); floatVector[i] = Float.parseFloat(f); } for (int i = 0; i < categoryFeatures.size(); i++) { String f = feature.getOrDefault(categoryFeatures.get(i), "NA"); categoryVector[i] = f; } CatBoostPredictions prediction = model.predict(floatVector, categoryVector); return 1.0 / (1.0 + Math.exp(-prediction.get(0, 0))); } catch (CatBoostError e) { e.printStackTrace(); return -1.0; }
|
评论系统未开启,无法评论!