modelNode.setScore(Double.toString(targetEncodedValue));
@Override public Node encode(Node node, Number splitValue, RNumberVector<?> terminalClassCount){ node.setScore(splitValue); return node; } };
static private Node encodeRegressionScore(Node node, RDoubleVector probabilities){ if(probabilities.size() != 1){ throw new IllegalArgumentException(); } Double probability = probabilities.asScalar(); node.setScore(probability); return node; }
@Override public Node encode(Node node, Number splitValue, RNumberVector<?> terminalClassCount){ int index = ValueUtil.asInt(splitValue); if(terminalClassCount != null){ throw new IllegalArgumentException(); } node.setScore(levels.getValue(index - 1)); return node; } };
@Override public void encode(Node node, LeafNode leafNode){ String score = ValueUtil.formatValue(leafNode.prediction()); node.setScore(score); } };
@Override public VisitorAction visit(Node node){ MathContext mathContext = this.mathContext; if(mathContext != null && node.hasScore()){ Object score = node.getScore(); if(score instanceof String){ String stringScore = (String)score; try { switch(mathContext){ case DOUBLE: node.setScore(Double.parseDouble(stringScore)); break; case FLOAT: node.setScore(Float.parseFloat(stringScore)); break; default: break; } } catch(NumberFormatException nfe){ // Ignored } } } return super.visit(node); } }
@Override public VisitorAction visit(Node node){ MathContext mathContext = this.mathContext; if(mathContext != null && node.hasScore()){ Object score = node.getScore(); if(score instanceof String){ String stringScore = (String)score; try { switch(mathContext){ case DOUBLE: node.setScore(Double.parseDouble(stringScore)); break; case FLOAT: node.setScore(Float.parseFloat(stringScore)); break; default: break; } } catch(NumberFormatException nfe){ // Ignored } } } return super.visit(node); } }
@Override public Node encode(Node node, int offset){ Number score = yval.getValue(offset); Number recordCount = n.getValue(offset); node .setScore(score) .setRecordCount(recordCount.doubleValue()); return node; } };
@Override public Node encode(Node node, int offset){ String score = categories.get(this.classes.get(offset) - 1); Integer recordCount = n.getValue(offset); node .setScore(score) .setRecordCount(recordCount.doubleValue()); if(hasScoreDistribution){ node = NodeUtil.toComplexNode(node); List<ScoreDistribution> scoreDistributions = node.getScoreDistributions(); for(int i = 0; i < categories.size(); i++){ List<? extends Number> recordCounts = this.recordCounts.get(i); ScoreDistribution scoreDistribution = new ScoreDistribution() .setValue(categories.get(i)) .setRecordCount(recordCounts.get(offset).doubleValue()); scoreDistributions.add(scoreDistribution); } } return node; } };
@Override public VisitorAction visit(Node node){ if(node.getScore() != null){ double nodeDepth = 0d; Deque<PMMLObject> parents = getParents(); for(PMMLObject parent : parents){ if(!(parent instanceof Node)){ break; } nodeDepth++; } double nodeSample = this.nodeSamples[Integer.parseInt(node.getId())]; double averagePathLength = (corrected ? correctedAveragePathLength(nodeSample) : averagePathLength(nodeSample)); node.setScore(nodeDepth + averagePathLength); } return super.visit(node); } };
@Override public Node encode(Node node, Number splitValue, RNumberVector<?> terminalClassCount){ if(splitValue.doubleValue() != 0d || (terminalClassCount == null || terminalClassCount.size() != levels.size())){ throw new IllegalArgumentException(); } node = NodeUtil.toComplexNode(node); List<ScoreDistribution> scoreDistributions = node.getScoreDistributions(); Double maxProbability = null; for(int i = 0; i < terminalClassCount.size(); i++){ String value = levels.getValue(i); Double probability = ValueUtil.asDouble(terminalClassCount.getValue(i)); if(maxProbability == null || (maxProbability).compareTo(probability) < 0){ node.setScore(value); maxProbability = probability; } ScoreDistribution scoreDisctibution = new ScoreDistribution(value, probability); scoreDistributions.add(scoreDisctibution); } return node; } };
node.setScore(value);
@Override public void encode(Node node, LeafNode leafNode){ int index = ValueUtil.asInt(leafNode.prediction()); node.setScore(this.categoricalLabel.getValue(index)); ImpurityCalculator impurityCalculator = leafNode.impurityStats(); node.setRecordCount((double)impurityCalculator.count()); double[] stats = impurityCalculator.stats(); for(int i = 0; i < stats.length; i++){ ScoreDistribution scoreDistribution = new ScoreDistribution(this.categoricalLabel.getValue(i), stats[i]); node.addScoreDistributions(scoreDistribution); } } };
private void encodeNode(org.dmg.pmml.tree.Node parent, int index, Schema schema){ parent.setId(String.valueOf(index + 1)); Node node = allNodes.get(index); if(!node.isLeaf()){ int splitIndex = node.getFeatureIndex(); Feature feature = schema.getFeature(splitIndex); org.dmg.pmml.tree.Node leftChild = new org.dmg.pmml.tree.Node() .setPredicate(encodePredicate(feature, node, true)); encodeNode(leftChild, node.getLeftChild().getId(), schema); org.dmg.pmml.tree.Node rightChild = new org.dmg.pmml.tree.Node() .setPredicate(encodePredicate(feature, node, false)); encodeNode(rightChild, node.getRightChild().getId(), schema); parent.addNodes(leftChild, rightChild); boolean defaultLeft = false; parent.setDefaultChild(defaultLeft ? leftChild.getId() : rightChild.getId()); } else { float value = (float)node.getValue(); parent.setScore(ValueUtil.formatValue(value)); } }
@Override public Node unmarshal(ComplexNode value){ if(value.getRecordCount() != null){ return value; } // End if if(value.hasExtensions() || (value.getPartition() != null) || value.hasScoreDistributions() || (value.getEmbeddedModel() != null)){ return value; } Node node; if(value.hasNodes()){ node = new BranchNode() .setId(value.getId()) .setDefaultChild(value.getDefaultChild()); (node.getNodes()).addAll(value.getNodes()); } else { node = new LeafNode() .setId(value.getId()); } node .setScore(value.getScore()) .setPredicate(value.getPredicate()); return node; }
@Override public Node unmarshal(ComplexNode value){ if(value.getRecordCount() != null){ return value; } // End if if(value.hasExtensions() || (value.getPartition() != null) || value.hasScoreDistributions() || (value.getEmbeddedModel() != null)){ return value; } Node node; if(value.hasNodes()){ node = new BranchNode() .setId(value.getId()) .setDefaultChild(value.getDefaultChild()); (node.getNodes()).addAll(value.getNodes()); } else { node = new LeafNode() .setId(value.getId()); } node .setScore(value.getScore()) .setPredicate(value.getPredicate()); return node; }