Search in sources :

Example 1 with NeuralNetworkEvaluator

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);
}
Also used : ArrayList(java.util.ArrayList) TargetField(org.jpmml.evaluator.TargetField) Classification(org.jpmml.evaluator.Classification) PMML(org.dmg.pmml.PMML) FieldValue(org.jpmml.evaluator.FieldValue) NeuralNetworkEvaluator(org.jpmml.evaluator.neural_network.NeuralNetworkEvaluator) HashMap(java.util.HashMap) Map(java.util.Map) FieldName(org.dmg.pmml.FieldName) PrintWriter(java.io.PrintWriter)

Example 2 with NeuralNetworkEvaluator

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);
    }
}
Also used : HashMap(java.util.HashMap) PMML(org.dmg.pmml.PMML) FieldValue(org.jpmml.evaluator.FieldValue) NeuralNetworkEvaluator(org.jpmml.evaluator.neural_network.NeuralNetworkEvaluator) Map(java.util.Map) HashMap(java.util.HashMap) FieldName(org.dmg.pmml.FieldName) Test(org.junit.Test)

Example 3 with NeuralNetworkEvaluator

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;
}
Also used : PMML(org.dmg.pmml.PMML) NeuralNetworkEvaluator(org.jpmml.evaluator.neural_network.NeuralNetworkEvaluator) CsvFile(ml.shifu.shifu.combo.CsvFile) FieldName(org.dmg.pmml.FieldName)

Example 4 with NeuralNetworkEvaluator

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);
    }
}
Also used : HashMap(java.util.HashMap) PMML(org.dmg.pmml.PMML) FieldValue(org.jpmml.evaluator.FieldValue) NeuralNetworkEvaluator(org.jpmml.evaluator.neural_network.NeuralNetworkEvaluator) Map(java.util.Map) HashMap(java.util.HashMap) FieldName(org.dmg.pmml.FieldName) Test(org.junit.Test)

Aggregations

FieldName (org.dmg.pmml.FieldName)4 PMML (org.dmg.pmml.PMML)4 NeuralNetworkEvaluator (org.jpmml.evaluator.neural_network.NeuralNetworkEvaluator)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 FieldValue (org.jpmml.evaluator.FieldValue)3 Test (org.junit.Test)2 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 CsvFile (ml.shifu.shifu.combo.CsvFile)1 Classification (org.jpmml.evaluator.Classification)1 TargetField (org.jpmml.evaluator.TargetField)1