pmml模型解析预测

1.读取csv参数进行批量预测

public static void main(String[] args) throws IOException {
PmmlInvoker invoker = new PmmlInvoker(“E:/jar/gaea/SASEM13_2_iris_DecTree.xml”);
List<Map<FieldName, String>> paramList = readInParams(“E:/jar/gaea/IrisScoreDecTree.csv”);
int lineNum = 0; //当前处理行数
File file = new File(“E:/jar/gaea/result.txt”);
for(Map<FieldName, String> param : paramList){
lineNum++;
Files.append(“当前行: " + lineNum + "=”,file, Charset.forName(utf8));
Map<FieldName, ?> result = invoker.invoke(param);
Set keySet = result.keySet(); //获取结果的keySet
for(FieldName fn : keySet){
if (fn.toString().equals(“species”)) {
System.out.println(result.get(fn).toString());
}
String tempString = result.get(fn).toString()+"\n";
Files.append(tempString,file,Charset.forName(utf8));
}
}
}

public static List<Map<FieldName,String>> readInParams(String filePath) throws IOException{
    InputStream is;
    is = PmmlCalc.class.getClassLoader().getResourceAsStream(filePath);
    if(is==null){
        is = new FileInputStream(filePath);
    }
    InputStreamReader isreader = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isreader);
    String[] nameArr = br.readLine().split(",");  //读取表头的名字
    ArrayList<Map<FieldName,String>> list = new ArrayList<>();
    String paramLine;  //一行参数
    //循环读取  每次读取一行数据
    while((paramLine = br.readLine()) != null){
        Map<FieldName,String> map = new HashMap<>();
        String[] paramLineArr = paramLine.split(",");
        for(int i=0; i<paramLineArr.length; i++){//一次循环处理一行数据
            map.put(new FieldName(nameArr[i]), paramLineArr[i]); //将表头和值组成map 加入list中
        }
        list.add(map);
    }
    is.close();
    return list;
}

}

public class PmmlInvoker {
private ModelEvaluator modelEvaluator;
// 通过文件读取模型
public PmmlInvoker(String pmmlFileName) {
PMML pmml = null;
InputStream is = null;
try {
if (pmmlFileName != null) {
is = PmmlInvoker.class.getClassLoader().getResourceAsStream(pmmlFileName);
if(is==null){
is = new FileInputStream(pmmlFileName);
}
pmml = PMMLUtil.unmarshal(is);
}
this.modelEvaluator = ModelEvaluatorFactory.newInstance().newModelEvaluator(pmml);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(is!=null)
is.close();
} catch (Exception localIOException3) {
localIOException3.printStackTrace();
}
}
this.modelEvaluator.verify();
System.out.println(“模型读取成功”);
}

// 通过输入流读取模型
public PmmlInvoker(InputStream is) {
    PMML pmml;
    try {
        pmml = PMMLUtil.unmarshal(is);
        try {
            is.close();
        } catch (IOException localIOException) {

        }
        this.modelEvaluator = ModelEvaluatorFactory.newInstance().newModelEvaluator(pmml);
    } catch (Exception e) {
        pmml = null;
    } finally {
        try {
            is.close();
        } catch (IOException localIOException3) {
        }
    }
    this.modelEvaluator.verify();
}

public Map<FieldName, String> invoke(Map<FieldName, String> paramsMap) {
    return this.modelEvaluator.evaluate(paramsMap);
}

}

2.公司项目引用

2.1 模型预测以及结果返回
public static String pmmlModel(Map<FieldName, String> map, String fileName, String key) throws IOException {
PmmlInvoker invoker = new PmmlInvoker(fileName);
Map<FieldName, ?> result = invoker.invoke(map);
Set keySet = result.keySet(); //获取结果的keySet
String value = null;
for(FieldName fn : keySet){
if (fn.toString().equals(key)) {
value = result.get(fn).toString();
}
}
return value;
}

2.2 获取前台传过来的数据模型参数和key值
Map<FieldName, String> map = new HashMap<>();
for (String str : preData.keySet()) {
FieldName fieldName = new FieldName(str);
map.put(fieldName, String.valueOf(preData.get(str)) /“1”/);
}

			try {
				String result = PmmlCalc.pmmlModel(map, jarName, resultKey);
				System.out.println(result.trim());
				return result.trim();
			} catch (IOException e) {
				e.printStackTrace();

}

2.3 获取路径和名称从本地获取pmml文件
String path = SpringBeanHolder.getBean(AiModelServiceImpl.class).path();
if (!path.substring(path.length()-1, path.length()).equals("//")
|| !path.substring(path.length()-1, path.length()).equals("/")) {
path = path + “//”;
}
System.out.println(path);
pkg.setExpression(
String.format(
A i M o d e l H a n d l e r . h a n d l e ( AiModelHandler.handle( AiModelHandler.handle(Fact_%s, “%s”, “%s”)”,
fact.getId(),
path + model.getJarName(),
value.getJSONObject(“resultKey”) != null ? value.getJSONObject(“resultKey”).getString(“data”) : “”
)
);

3.遇到的问题及解决办法

获取不到配置文件中的动态路径,把类添加到spring容器才可以获取
SpringBeanHolder.getBean(AiModelServiceImpl.class).path();通过这句获得配置文件中的动态路径

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值