use of org.jpmml.evaluator.Classification in project shifu by ShifuML.
the class PMMLVerifySuit method evalNNPmml.
@SuppressWarnings("unchecked")
private void evalNNPmml(String pmmlPath, String DataPath, String OutPath, String sep, String scoreName) throws Exception {
PMML pmml = PMMLUtils.loadPMML(pmmlPath);
NeuralNetworkEvaluator evaluator = new NeuralNetworkEvaluator(pmml);
PrintWriter writer = new PrintWriter(OutPath, "UTF-8");
List<TargetField> targetFields = evaluator.getTargetFields();
if (targetFields.size() == 1) {
writer.println(scoreName);
} else {
for (int i = 0; i < targetFields.size(); i++) {
if (i > 0) {
writer.print("|");
}
writer.print(scoreName + "_tag_" + i);
}
writer.println();
}
List<Map<FieldName, FieldValue>> input = CsvUtil.load(evaluator, DataPath, sep);
for (Map<FieldName, FieldValue> maps : input) {
switch(evaluator.getModel().getMiningFunction()) {
case REGRESSION:
if (targetFields.size() == 1) {
Map<FieldName, Double> regressionTerm = (Map<FieldName, Double>) evaluator.evaluate(maps);
writer.println(regressionTerm.get(new FieldName(AbstractSpecifCreator.FINAL_RESULT)).intValue());
} else {
Map<FieldName, Double> regressionTerm = (Map<FieldName, Double>) evaluator.evaluate(maps);
List<FieldName> outputFieldList = new ArrayList<FieldName>(regressionTerm.keySet());
Collections.sort(outputFieldList, new Comparator<FieldName>() {
@Override
public int compare(FieldName a, FieldName b) {
return a.getValue().compareTo(b.getValue());
}
});
int j = 0;
for (int i = 0; i < outputFieldList.size(); i++) {
FieldName fieldName = outputFieldList.get(i);
if (fieldName.getValue().startsWith(AbstractSpecifCreator.FINAL_RESULT)) {
if (j++ > 0) {
writer.print("|");
}
writer.print(regressionTerm.get(fieldName));
}
}
writer.println();
}
break;
case CLASSIFICATION:
Map<FieldName, Classification<Double>> classificationTerm = (Map<FieldName, Classification<Double>>) evaluator.evaluate(maps);
for (Classification<Double> cMap : classificationTerm.values()) for (Map.Entry<String, Value<Double>> entry : cMap.getValues().entrySet()) System.out.println(entry.getValue().getValue() * 1000);
break;
default:
break;
}
}
IOUtils.closeQuietly(writer);
}
use of org.jpmml.evaluator.Classification in project shifu by ShifuML.
the class PMMLScoreGenTest method score.
@SuppressWarnings("unchecked")
private double score(MiningModelEvaluator evaluator, Map<String, String> rawInput, String scoreName) {
List<TargetField> targetFields = evaluator.getTargetFields();
Map<FieldName, FieldValue> maps = convertRawIntoInput(evaluator, rawInput);
List<Double> scores = new ArrayList<Double>();
switch(evaluator.getModel().getMiningFunction()) {
case REGRESSION:
if (targetFields.size() == 1) {
Map<FieldName, Double> regressionTerm = (Map<FieldName, Double>) evaluator.evaluate(maps);
scores.add(regressionTerm.get(new FieldName(AbstractSpecifCreator.FINAL_RESULT)));
} else {
Map<FieldName, Double> regressionTerm = (Map<FieldName, Double>) evaluator.evaluate(maps);
List<FieldName> outputFieldList = new ArrayList<FieldName>(regressionTerm.keySet());
Collections.sort(outputFieldList, new Comparator<FieldName>() {
@Override
public int compare(FieldName a, FieldName b) {
return a.getValue().compareTo(b.getValue());
}
});
for (int i = 0; i < outputFieldList.size(); i++) {
FieldName fieldName = outputFieldList.get(i);
if (fieldName.getValue().startsWith(AbstractSpecifCreator.FINAL_RESULT)) {
scores.add(regressionTerm.get(fieldName));
}
}
}
break;
case CLASSIFICATION:
Map<FieldName, Classification<Double>> classificationTerm = (Map<FieldName, Classification<Double>>) evaluator.evaluate(maps);
for (Classification<Double> cMap : classificationTerm.values()) for (Map.Entry<String, Value<Double>> entry : cMap.getValues().entrySet()) System.out.println(entry.getValue().getValue() * 1000);
break;
default:
break;
}
return scores.get(0);
}
Aggregations