use of org.jpmml.evaluator.neural_network.NeuralNetworkEvaluator 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.neural_network.NeuralNetworkEvaluator in project shifu by ShifuML.
the class GolfPmmlTest method testNewPMMLModel.
@SuppressWarnings({ "unchecked", "incomplete-switch" })
@Test
public void testNewPMMLModel() throws Exception {
// model with segment expansion
PMML pmml = PMMLUtils.loadPMML("src/test/resources/dttest/model/golf0-new.pmml");
NeuralNetworkEvaluator evaluator = new NeuralNetworkEvaluator(pmml);
List<Map<FieldName, FieldValue>> input = CsvUtil.load(evaluator, "src/test/resources/dttest/data/golf0-new.csv", "\\|");
for (Map<FieldName, FieldValue> map : input) {
Map<String, Object> newMap = new HashMap<String, Object>();
Map<FieldName, Object> regressionTerm = (Map<FieldName, Object>) evaluator.evaluate(map);
Object pmmlScore = 0d;
for (Map.Entry<FieldName, Object> entry : regressionTerm.entrySet()) {
pmmlScore = entry.getValue();
}
for (Entry<FieldName, FieldValue> entry : map.entrySet()) {
FieldName key = entry.getKey();
FieldValue value = entry.getValue();
switch(value.getOpType()) {
case CONTINUOUS:
newMap.put(key.getValue(), Double.parseDouble(value.getValue().toString()));
break;
case CATEGORICAL:
newMap.put(key.getValue(), value.getValue().toString());
break;
default:
break;
}
}
System.out.println(pmmlScore);
}
}
use of org.jpmml.evaluator.neural_network.NeuralNetworkEvaluator in project shifu by ShifuML.
the class PmmlSpecValidationTest method doValidation.
@SuppressWarnings("unchecked")
private boolean doValidation(String pmmlPath, String DataPath, String delimiter, String scoreName) throws Exception {
PMML pmml = PMMLUtils.loadPMML(pmmlPath);
NeuralNetworkEvaluator evaluator = new NeuralNetworkEvaluator(pmml);
List<TargetField> targetFields = evaluator.getTargetFields();
CsvFile evalData = new CsvFile(DataPath, delimiter, true);
Iterator<Map<String, String>> iterator = evalData.iterator();
int mismatchCnt = 0;
while (iterator.hasNext()) {
Map<String, String> rawInput = iterator.next();
Map<FieldName, FieldValue> maps = convertRawIntoInput(evaluator, rawInput);
double pmmlScore = 0.0;
switch(evaluator.getModel().getMiningFunction()) {
case REGRESSION:
if (targetFields.size() == 1) {
Map<FieldName, Double> regressionTerm = (Map<FieldName, Double>) evaluator.evaluate(maps);
pmmlScore = 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)) {
pmmlScore = 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;
}
double expectScore = Double.parseDouble(rawInput.get(scoreName));
if (Math.abs(expectScore - pmmlScore) > EPS) {
System.out.println(rawInput.get("trans_id") + "|" + expectScore + "|" + pmmlScore);
mismatchCnt++;
}
}
return mismatchCnt == 0;
}
use of org.jpmml.evaluator.neural_network.NeuralNetworkEvaluator in project shifu by ShifuML.
the class GolfPmmlTest method testOldNNPMMlModel.
@SuppressWarnings({ "unchecked" })
@Test
public void testOldNNPMMlModel() throws Exception {
PMML pmml = PMMLUtils.loadPMML("src/test/resources/dttest/model/golf0.pmml");
NeuralNetworkEvaluator evaluator = new NeuralNetworkEvaluator(pmml);
List<Map<FieldName, FieldValue>> input = CsvUtil.load(evaluator, "src/test/resources/dttest/data/golf0.csv", "\\|");
for (Map<FieldName, FieldValue> map : input) {
Map<String, Object> newMap = new HashMap<String, Object>();
Map<FieldName, Object> regressionTerm = (Map<FieldName, Object>) evaluator.evaluate(map);
Object pmmlScore = 0d;
for (Map.Entry<FieldName, Object> entry : regressionTerm.entrySet()) {
pmmlScore = entry.getValue();
}
for (Entry<FieldName, FieldValue> entry : map.entrySet()) {
FieldName key = entry.getKey();
FieldValue value = entry.getValue();
switch(value.getOpType()) {
case CONTINUOUS:
newMap.put(key.getValue(), Double.parseDouble(value.getValue().toString()));
break;
case CATEGORICAL:
newMap.put(key.getValue(), value.getValue().toString());
break;
default:
break;
}
}
System.out.println(pmmlScore);
}
}
Aggregations