注:加载失败时尝试修改pmml文件版本为4.3
依赖
<dependency> <groupId>org.jpmml</groupId> <artifactId>pmml-evaluator</artifactId> <version>1.4.1</version> </dependency> <dependency> <groupId>org.jpmml</groupId> <artifactId>pmml-evaluator-extension</artifactId> <version>1.4.1</version> </dependency> ------------resources添加-------------- <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>application.yml</include> <include>application-${activatedProperties}.yml</include> <include>**/**.pmml</include> </includes> </resource> </resources>
加载模型
@Bean @Qualifier("evaluator") public Evaluator load() { PMML pmml = null; try { ClassPathResource classPathResource = new ClassPathResource("META-INF/xxx.pmml"); InputStream is = classPathResource.getInputStream(); pmml = PMMLUtil.unmarshal(is); } catch (IOException e) { log.error("Get resource:xxx.pmml failed! error msg:{}", e.getMessage()); } catch (JAXBException | SAXException e) { log.error(e.getMessage()); } ModelEvaluatorFactory modelEvaluatorFactory = ModelEvaluatorFactory.newInstance(); modelEvaluator = (Evaluator) modelEvaluatorFactory.newModelEvaluator(pmml); modelEvaluator.verify(); log.info("load model successful!"); return modelEvaluator; }
调用模型
@Autowired @Qualifier("evaluator") private Evaluator evaluator; @Test public void moduleTest() { List<InputField> inputFields = evaluator.getInputFields(); Map<String, Object> map = new LinkedHashMap<>(); map.put("x1", 50); map.put("x2", 50); map.put("x3", 7); map.put("x4", 60); map.put("x5", 250); map.put("x6", 6); map.put("x7", 80); map.put("x8", 260); map.put("x9", 48); map.put("x10", 1); map.put("x11", 5); map.put("x12", 1); map.put("x13", 0); Map<FieldName, FieldValue> args = new HashMap<>(); for (InputField inputField : inputFields) { FieldName name = inputField.getName(); Object value = map.get(name.getValue()); FieldValue inputFieldValue = inputField.prepare(value); args.put(name, inputFieldValue); } Map<FieldName, ?> evaluate = evaluator.evaluate(args); Object value = evaluate.get(evaluator.getTargetFields().get(0).getName()); BigDecimal predictValue = JSON.parseObject(JSON.toJSONString(value)).getJSONObject("values").getJSONObject("1.0").getBigDecimal("value"); log.info("result:{}", predictValue); }
标签:map,JAVA,PMML,pmml,value,log,put,evaluator,加载 From: https://www.cnblogs.com/lockyluo/p/17160839.html