Java Code Examples for org.dmg.pmml.tree.Node#setPredicate()
The following examples show how to use
org.dmg.pmml.tree.Node#setPredicate() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TreeModelCompactor.java From jpmml-lightgbm with GNU Affero General Public License v3.0 | 4 votes |
@Override public void enterNode(Node node){ Object id = node.getId(); Object score = node.getScore(); Object defaultChild = node.getDefaultChild(); if(id == null){ throw new IllegalArgumentException(); } // End if if(node.hasNodes()){ List<Node> children = node.getNodes(); if(children.size() != 2 || score != null || defaultChild == null){ throw new IllegalArgumentException(); } Node firstChild = children.get(0); Node secondChild = children.get(1); if(equalsNode(defaultChild, firstChild)){ children = swapChildren(node); firstChild = children.get(0); secondChild = children.get(1); } else if(equalsNode(defaultChild, secondChild)){ // Ignored } else { throw new IllegalArgumentException(); } node.setDefaultChild(null); secondChild.setPredicate(True.INSTANCE); } else { if(score == null || defaultChild != null){ throw new IllegalArgumentException(); } } node.setId(null); }
Example 2
Source File: TreeModelCompactor.java From jpmml-sparkml with GNU Affero General Public License v3.0 | 4 votes |
@Override public void enterNode(Node node){ Object id = node.getId(); Object score = node.getScore(); if(id != null){ throw new IllegalArgumentException(); } // End if if(node.hasNodes()){ List<Node> children = node.getNodes(); if(children.size() != 2 || score != null){ throw new IllegalArgumentException(); } Node firstChild = children.get(0); Node secondChild = children.get(1); Predicate firstPredicate = firstChild.getPredicate(); Predicate secondPredicate = secondChild.getPredicate(); checkFieldReference(firstPredicate, secondPredicate); boolean update = true; if(hasOperator(firstPredicate, SimplePredicate.Operator.EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.EQUAL)){ update = isCategoricalField((SimplePredicate)firstPredicate); } else if(hasOperator(firstPredicate, SimplePredicate.Operator.NOT_EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.EQUAL)){ children = swapChildren(node); firstChild = children.get(0); secondChild = children.get(1); } else if(hasOperator(firstPredicate, SimplePredicate.Operator.EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.NOT_EQUAL)){ // Ignored } else if(hasOperator(firstPredicate, SimplePredicate.Operator.LESS_OR_EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.GREATER_THAN)){ // Ignored } else if(hasOperator(firstPredicate, SimplePredicate.Operator.EQUAL) && hasBooleanOperator(secondPredicate, SimpleSetPredicate.BooleanOperator.IS_IN)){ addCategoricalField(secondChild); } else if(hasBooleanOperator(firstPredicate, SimpleSetPredicate.BooleanOperator.IS_IN) && hasOperator(secondPredicate, SimplePredicate.Operator.EQUAL)){ children = swapChildren(node); firstChild = children.get(0); secondChild = children.get(1); addCategoricalField(secondChild); } else if(hasBooleanOperator(firstPredicate, SimpleSetPredicate.BooleanOperator.IS_IN) && hasBooleanOperator(secondPredicate, SimpleSetPredicate.BooleanOperator.IS_IN)){ addCategoricalField(secondChild); } else { throw new IllegalArgumentException(); } // End if if(update){ secondChild.setPredicate(True.INSTANCE); } } else { if(score == null){ throw new IllegalArgumentException(); } } }
Example 3
Source File: TreeModelCompactor.java From jpmml-xgboost with GNU Affero General Public License v3.0 | 4 votes |
@Override public void enterNode(Node node){ Object id = node.getId(); Object score = node.getScore(); Object defaultChild = node.getDefaultChild(); if(id == null){ throw new IllegalArgumentException(); } // End if if(node.hasNodes()){ List<Node> children = node.getNodes(); if(children.size() != 2 || score != null || defaultChild == null){ throw new IllegalArgumentException(); } Node firstChild = children.get(0); Node secondChild = children.get(1); if(equalsNode(defaultChild, firstChild)){ children = swapChildren(node); firstChild = children.get(0); secondChild = children.get(1); } else if(equalsNode(defaultChild, secondChild)){ // Ignored } else { throw new IllegalArgumentException(); } node.setDefaultChild(null); secondChild.setPredicate(True.INSTANCE); } else { if(score == null || defaultChild != null){ throw new IllegalArgumentException(); } } node.setId(null); }
Example 4
Source File: TreeModelCompactor.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 4 votes |
@Override public void enterNode(Node node){ Object id = node.getId(); Object score = node.getScore(); if(id == null){ throw new IllegalArgumentException(); } // End if if(node.hasNodes()){ List<Node> children = node.getNodes(); if(children.size() != 2){ throw new IllegalArgumentException(); } Node firstChild = children.get(0); Node secondChild = children.get(1); Predicate firstPredicate = firstChild.getPredicate(); Predicate secondPredicate = secondChild.getPredicate(); checkFieldReference(firstPredicate, secondPredicate); checkValue(firstPredicate, secondPredicate); if(hasOperator(firstPredicate, SimplePredicate.Operator.NOT_EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.EQUAL)){ children = swapChildren(node); firstChild = children.get(0); secondChild = children.get(1); } else if(hasOperator(firstPredicate, SimplePredicate.Operator.LESS_OR_EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.GREATER_THAN)){ // Ignored } else { throw new IllegalArgumentException(); } secondChild.setPredicate(True.INSTANCE); } else { if(score == null){ throw new IllegalArgumentException(); } } node.setId(null); }
Example 5
Source File: RandomForestCompactor.java From jpmml-r with GNU Affero General Public License v3.0 | 4 votes |
@Override public void enterNode(Node node){ Object id = node.getId(); Object score = node.getScore(); if(id == null){ throw new IllegalArgumentException(); } // End if if(node.hasNodes()){ List<Node> children = node.getNodes(); if(children.size() != 2 || score != null){ throw new IllegalArgumentException(); } Node firstChild = children.get(0); Node secondChild = children.get(1); Predicate firstPredicate = firstChild.getPredicate(); Predicate secondPredicate = secondChild.getPredicate(); checkFieldReference(firstPredicate, secondPredicate); boolean update = isDefinedField((HasFieldReference<?>)firstPredicate); if(hasOperator(firstPredicate, SimplePredicate.Operator.EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.EQUAL)){ // Ignored } else if(hasOperator(firstPredicate, SimplePredicate.Operator.LESS_OR_EQUAL) && hasOperator(secondPredicate, SimplePredicate.Operator.GREATER_THAN)){ update = true; } else if(hasOperator(firstPredicate, SimplePredicate.Operator.EQUAL) && hasBooleanOperator(secondPredicate, SimpleSetPredicate.BooleanOperator.IS_IN)){ // Ignored } else if(hasBooleanOperator(firstPredicate, SimpleSetPredicate.BooleanOperator.IS_IN) && hasOperator(secondPredicate, SimplePredicate.Operator.EQUAL)){ if(update){ children = swapChildren(node); firstChild = children.get(0); secondChild = children.get(1); } } else if(hasBooleanOperator(firstPredicate, SimpleSetPredicate.BooleanOperator.IS_IN) && hasBooleanOperator(secondPredicate, SimpleSetPredicate.BooleanOperator.IS_IN)){ // Ignored } else { throw new IllegalArgumentException(); } // End if if(update){ secondChild.setPredicate(True.INSTANCE); } } else { if(score == null){ throw new IllegalArgumentException(); } } node.setId(null); }
Example 6
Source File: RDFUpdate.java From oryx with Apache License 2.0 | 4 votes |
private TreeModel toTreeModel(DecisionTreeModel dtModel, CategoricalValueEncodings categoricalValueEncodings, IntLongMap nodeIDCounts) { boolean classificationTask = dtModel.algo().equals(Algo.Classification()); Preconditions.checkState(classificationTask == inputSchema.isClassification()); Node root = new ComplexNode(); root.setId("r"); Queue<Node> modelNodes = new ArrayDeque<>(); modelNodes.add(root); Queue<Pair<org.apache.spark.mllib.tree.model.Node,Split>> treeNodes = new ArrayDeque<>(); treeNodes.add(new Pair<>(dtModel.topNode(), null)); while (!treeNodes.isEmpty()) { Pair<org.apache.spark.mllib.tree.model.Node,Split> treeNodePredicate = treeNodes.remove(); Node modelNode = modelNodes.remove(); // This is the decision that got us here from the parent, if any; // not the predicate at this node Predicate predicate = buildPredicate(treeNodePredicate.getSecond(), categoricalValueEncodings); modelNode.setPredicate(predicate); org.apache.spark.mllib.tree.model.Node treeNode = treeNodePredicate.getFirst(); long nodeCount = nodeIDCounts.get(treeNode.id()); modelNode.setRecordCount((double) nodeCount); if (treeNode.isLeaf()) { Predict prediction = treeNode.predict(); int targetEncodedValue = (int) prediction.predict(); if (classificationTask) { Map<Integer,String> targetEncodingToValue = categoricalValueEncodings.getEncodingValueMap(inputSchema.getTargetFeatureIndex()); double predictedProbability = prediction.prob(); Preconditions.checkState(predictedProbability >= 0.0 && predictedProbability <= 1.0); // Not sure how nodeCount == 0 can happen but it does in the MLlib model long effectiveNodeCount = Math.max(1, nodeCount); // Problem: MLlib only gives a predicted class and its probability, and no distribution // over the rest. Infer that the rest of the probability is evenly distributed. double restProbability = (1.0 - predictedProbability) / (targetEncodingToValue.size() - 1); targetEncodingToValue.forEach((encodedValue, value) -> { double probability = encodedValue == targetEncodedValue ? predictedProbability : restProbability; // Yes, recordCount may be fractional; it's a relative indicator double recordCount = probability * effectiveNodeCount; if (recordCount > 0.0) { ScoreDistribution distribution = new ScoreDistribution(value, recordCount); // Not "confident" enough in the "probability" to call it one distribution.setConfidence(probability); modelNode.addScoreDistributions(distribution); } }); } else { modelNode.setScore(Double.toString(targetEncodedValue)); } } else { Split split = treeNode.split().get(); Node positiveModelNode = new ComplexNode().setId(modelNode.getId() + "+"); Node negativeModelNode = new ComplexNode().setId(modelNode.getId() + "-"); modelNode.addNodes(positiveModelNode, negativeModelNode); org.apache.spark.mllib.tree.model.Node rightTreeNode = treeNode.rightNode().get(); org.apache.spark.mllib.tree.model.Node leftTreeNode = treeNode.leftNode().get(); boolean defaultRight = nodeIDCounts.get(rightTreeNode.id()) > nodeIDCounts.get(leftTreeNode.id()); modelNode.setDefaultChild(defaultRight ? positiveModelNode.getId() : negativeModelNode.getId()); // Right node is "positive", so carries the predicate. It must evaluate first // and therefore come first in the tree modelNodes.add(positiveModelNode); modelNodes.add(negativeModelNode); treeNodes.add(new Pair<>(rightTreeNode, split)); treeNodes.add(new Pair<>(leftTreeNode, null)); } } return new TreeModel() .setNode(root) .setSplitCharacteristic(TreeModel.SplitCharacteristic.BINARY_SPLIT) .setMissingValueStrategy(TreeModel.MissingValueStrategy.DEFAULT_CHILD); }