Search in sources :

Example 1 with Classification

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);
}
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 Classification

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

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 FieldName (org.dmg.pmml.FieldName)2 Classification (org.jpmml.evaluator.Classification)2 FieldValue (org.jpmml.evaluator.FieldValue)2 TargetField (org.jpmml.evaluator.TargetField)2 PrintWriter (java.io.PrintWriter)1 PMML (org.dmg.pmml.PMML)1 NeuralNetworkEvaluator (org.jpmml.evaluator.neural_network.NeuralNetworkEvaluator)1