org.dmg.pmml.SimplePredicate Java Examples
The following examples show how to use
org.dmg.pmml.SimplePredicate.
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: PredicateUtilTest.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 6 votes |
@Test public void evaluateSurrogateCompoundPredicate(){ FieldName temperature = FieldName.create("temperature"); FieldName humidity = FieldName.create("humidity"); CompoundPredicate temperaturePredicate = new CompoundPredicate(CompoundPredicate.BooleanOperator.AND, null) .addPredicates( new SimplePredicate(temperature, SimplePredicate.Operator.LESS_THAN, "90"), new SimplePredicate(temperature, SimplePredicate.Operator.GREATER_THAN, "50") ); SimplePredicate humidityPredicate = new SimplePredicate(humidity, SimplePredicate.Operator.GREATER_OR_EQUAL, "80"); CompoundPredicate compoundPredicate = new CompoundPredicate(CompoundPredicate.BooleanOperator.SURROGATE, null) .addPredicates(temperaturePredicate, humidityPredicate); assertEquals(Boolean.TRUE, evaluate(compoundPredicate, temperature, 70, humidity, null)); assertEquals(Boolean.FALSE, evaluate(compoundPredicate, temperature, 40, humidity, null)); assertEquals(Boolean.FALSE, evaluate(compoundPredicate, temperature, 100, humidity, null)); assertEquals(Boolean.TRUE, evaluate(compoundPredicate, temperature, null, humidity, 90)); assertEquals(Boolean.FALSE, evaluate(compoundPredicate, temperature, null, humidity, 70)); assertEquals(null, evaluate(compoundPredicate, temperature, null, humidity, null)); }
Example #2
Source File: PredicateInterner.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 6 votes |
public Predicate intern(Predicate predicate){ if(predicate instanceof SimplePredicate){ return intern((SimplePredicate)predicate); } else if(predicate instanceof SimpleSetPredicate){ return intern((SimpleSetPredicate)predicate); } else if(predicate instanceof True){ return True.INSTANCE; } else if(predicate instanceof False){ return False.INSTANCE; } return predicate; }
Example #3
Source File: TreeModelFlattener.java From jpmml-sklearn with GNU Affero General Public License v3.0 | 5 votes |
static private Iterator<Node> getChildren(Node node){ Predicate predicate = node.getPredicate(); if(!(predicate instanceof SimplePredicate)){ return null; } SimplePredicate simplePredicate = (SimplePredicate)predicate; if(!hasOperator(simplePredicate, SimplePredicate.Operator.LESS_OR_EQUAL)){ return null; } // End if if(node.hasNodes()){ List<Node> children = node.getNodes(); int endPos = 0; for(Node child : children){ Predicate childPredicate = child.getPredicate(); if(!hasFieldReference(childPredicate, simplePredicate.getField()) || !hasOperator(childPredicate, simplePredicate.getOperator())){ break; } endPos++; } if(endPos > 0){ return (children.subList(0, endPos)).iterator(); } return null; } return null; }
Example #4
Source File: PredicateInternerTest.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 5 votes |
@Test public void internSimplePredicate(){ FieldName name = FieldName.create("x"); Predicate left = new SimplePredicate(name, SimplePredicate.Operator.EQUAL, "1"); Predicate right = new SimplePredicate(name, SimplePredicate.Operator.EQUAL, "1"); checkTree(left, right); }
Example #5
Source File: RegressionTree.java From pyramid with Apache License 2.0 | 5 votes |
static private Predicate encodePredicate(Feature feature, Node node, boolean left){ FieldName name = feature.getName(); SimplePredicate.Operator operator; String value; if(feature instanceof BinaryFeature){ BinaryFeature binaryFeature = (BinaryFeature)feature; operator = (left ? SimplePredicate.Operator.NOT_EQUAL : SimplePredicate.Operator.EQUAL); value = binaryFeature.getValue(); } else { ContinuousFeature continuousFeature = feature.toContinuousFeature(); Number splitValue = node.getThreshold(); DataType dataType = continuousFeature.getDataType(); switch(dataType){ case INTEGER: splitValue = (int)(splitValue.floatValue() + 1f); break; case FLOAT: break; default: throw new IllegalArgumentException(); } operator = (left ? SimplePredicate.Operator.LESS_OR_EQUAL : SimplePredicate.Operator.GREATER_THAN); value = ValueUtil.formatValue(splitValue); } SimplePredicate simplePredicate = new SimplePredicate(name, operator) .setValue(value); return simplePredicate; }
Example #6
Source File: PredicateUtil.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 5 votes |
static Boolean evaluatePredicate(Predicate predicate, EvaluationContext context){ if(predicate instanceof SimplePredicate){ return evaluateSimplePredicate((SimplePredicate)predicate, context); } else if(predicate instanceof SimpleSetPredicate){ return evaluateSimpleSetPredicate((SimpleSetPredicate)predicate, context); } else if(predicate instanceof CompoundPredicate){ return evaluateCompoundPredicate((CompoundPredicate)predicate, context); } else if(predicate instanceof True){ return evaluateTrue((True)predicate); } else if(predicate instanceof False){ return evaluateFalse((False)predicate); } // End if if(predicate instanceof JavaPredicate){ return evaluateJavaPredicate((JavaPredicate)predicate, context); } throw new UnsupportedElementException(predicate); }
Example #7
Source File: RDFUpdate.java From oryx with Apache License 2.0 | 5 votes |
private Predicate buildPredicate(Split split, CategoricalValueEncodings categoricalValueEncodings) { if (split == null) { // Left child always applies, but is evaluated second return new True(); } int featureIndex = inputSchema.predictorToFeatureIndex(split.feature()); FieldName fieldName = FieldName.create(inputSchema.getFeatureNames().get(featureIndex)); if (split.featureType().equals(FeatureType.Categorical())) { // Note that categories in MLlib model select the *left* child but the // convention here will be that the predicate selects the *right* child // So the predicate will evaluate "not in" this set // More ugly casting @SuppressWarnings("unchecked") Collection<Double> javaCategories = (Collection<Double>) (Collection<?>) JavaConversions.seqAsJavaList(split.categories()); Set<Integer> negativeEncodings = javaCategories.stream().map(Double::intValue).collect(Collectors.toSet()); Map<Integer,String> encodingToValue = categoricalValueEncodings.getEncodingValueMap(featureIndex); List<String> negativeValues = negativeEncodings.stream().map(encodingToValue::get).collect(Collectors.toList()); String joinedValues = TextUtils.joinPMMLDelimited(negativeValues); return new SimpleSetPredicate(fieldName, SimpleSetPredicate.BooleanOperator.IS_NOT_IN, new Array(Array.Type.STRING, joinedValues)); } else { // For MLlib, left means <= threshold, so right means > return new SimplePredicate(fieldName, SimplePredicate.Operator.GREATER_THAN, Double.toString(split.threshold())); } }
Example #8
Source File: ValueParser.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 5 votes |
@Override public VisitorAction visit(SimplePredicate simplePredicate){ FieldName name = simplePredicate.getField(); if(name == null){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_FIELD); } // End if if(simplePredicate.hasValue()){ parseValue(name, simplePredicate); } return super.visit(simplePredicate); }
Example #9
Source File: GolfingTreeModelExample.java From jpmml-model with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public PMML produce(){ FieldName temperature = FieldName.create("temperature"); FieldName humidity = FieldName.create("humidity"); FieldName windy = FieldName.create("windy"); FieldName outlook = FieldName.create("outlook"); FieldName whatIdo = FieldName.create("whatIDo"); Header header = new Header() .setCopyright("www.dmg.org") .setDescription("A very small binary tree model to show structure."); DataDictionary dataDictionary = new DataDictionary() .addDataFields( new DataField(temperature, OpType.CONTINUOUS, DataType.DOUBLE), new DataField(humidity, OpType.CONTINUOUS, DataType.DOUBLE), new DataField(windy, OpType.CATEGORICAL, DataType.STRING) .addValues(createValues("true", "false")), new DataField(outlook, OpType.CATEGORICAL, DataType.STRING) .addValues(createValues("sunny", "overcast", "rain")), new DataField(whatIdo, OpType.CATEGORICAL, DataType.STRING) .addValues(createValues("will play", "may play", "no play")) ); dataDictionary.setNumberOfFields((dataDictionary.getDataFields()).size()); MiningSchema miningSchema = new MiningSchema() .addMiningFields( new MiningField(temperature), new MiningField(humidity), new MiningField(windy), new MiningField(outlook), new MiningField(whatIdo) .setUsageType(MiningField.UsageType.TARGET) ); Node root = new BranchNode("will play", True.INSTANCE); // Upper half of the tree root.addNodes( new BranchNode("will play", new SimplePredicate(outlook, Operator.EQUAL, "sunny")) .addNodes( new BranchNode("will play", createCompoundPredicate(BooleanOperator.AND, new SimplePredicate(temperature, Operator.LESS_THAN, "90"), new SimplePredicate(temperature, Operator.GREATER_THAN, "50")) ) .addNodes( new LeafNode("will play", new SimplePredicate(humidity, Operator.LESS_THAN, "80")), new LeafNode("no play", new SimplePredicate(humidity, Operator.GREATER_OR_EQUAL, "80")) ), new LeafNode("no play", createCompoundPredicate(BooleanOperator.OR, new SimplePredicate(temperature, Operator.GREATER_OR_EQUAL, "90"), new SimplePredicate(temperature, Operator.LESS_OR_EQUAL, "50")) ) ) ); // Lower half of the tree root.addNodes( new BranchNode("may play", createCompoundPredicate(BooleanOperator.OR, new SimplePredicate(outlook, Operator.EQUAL, "overcast"), new SimplePredicate(outlook, Operator.EQUAL, "rain")) ) .addNodes( new LeafNode("may play", createCompoundPredicate(BooleanOperator.AND, new SimplePredicate(temperature, Operator.GREATER_THAN, "60"), new SimplePredicate(temperature, Operator.LESS_THAN, "100"), new SimplePredicate(outlook, Operator.EQUAL, "overcast"), new SimplePredicate(humidity, Operator.LESS_THAN, "70"), new SimplePredicate(windy, Operator.EQUAL, "false")) ), new LeafNode("no play", createCompoundPredicate(BooleanOperator.AND, new SimplePredicate(outlook, Operator.EQUAL, "rain"), new SimplePredicate(humidity, Operator.LESS_THAN, "70")) ) ) ); TreeModel treeModel = new TreeModel(MiningFunction.CLASSIFICATION, miningSchema, root) .setModelName("golfing"); PMML pmml = new PMML(Version.PMML_4_4.getVersion(), header, dataDictionary) .addModels(treeModel); return pmml; }
Example #10
Source File: PredicateUtilTest.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 4 votes |
@Test public void evaluateBooleanSimplePredicate(){ FieldName flag = FieldName.create("flag"); SimplePredicate simplePredicate = new SimplePredicate(flag, SimplePredicate.Operator.EQUAL, "true"); assertEquals(Boolean.TRUE, evaluate(simplePredicate, flag, true)); assertEquals(Boolean.FALSE, evaluate(simplePredicate, flag, false)); simplePredicate.setOperator(SimplePredicate.Operator.NOT_EQUAL); assertEquals(Boolean.FALSE, evaluate(simplePredicate, flag, true)); assertEquals(Boolean.TRUE, evaluate(simplePredicate, flag, false)); simplePredicate.setValue("0.5"); simplePredicate.setOperator(SimplePredicate.Operator.LESS_OR_EQUAL); assertEquals(Boolean.FALSE, evaluate(simplePredicate, flag, true)); assertEquals(Boolean.TRUE, evaluate(simplePredicate, flag, false)); simplePredicate.setOperator(SimplePredicate.Operator.GREATER_THAN); assertEquals(Boolean.TRUE, evaluate(simplePredicate, flag, true)); assertEquals(Boolean.FALSE, evaluate(simplePredicate, flag, false)); }
Example #11
Source File: PredicateUtil.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 4 votes |
static public Boolean evaluateSimplePredicate(SimplePredicate simplePredicate, EvaluationContext context){ FieldName name = simplePredicate.getField(); if(name == null){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_FIELD); } SimplePredicate.Operator operator = simplePredicate.getOperator(); if(operator == null){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_OPERATOR); } switch(operator){ case IS_MISSING: case IS_NOT_MISSING: // "If the operator is isMissing or isNotMissing, then the value attribute must not appear" if(simplePredicate.hasValue()){ throw new MisplacedAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_VALUE, simplePredicate.getValue()); } break; default: // "With all other operators, however, the value attribute is required" if(!simplePredicate.hasValue()){ throw new MissingAttributeException(simplePredicate, PMMLAttributes.SIMPLEPREDICATE_VALUE); } break; } FieldValue value = context.evaluate(name); switch(operator){ case IS_MISSING: return Boolean.valueOf(FieldValueUtil.isMissing(value)); case IS_NOT_MISSING: return Boolean.valueOf(!FieldValueUtil.isMissing(value)); default: break; } // "A SimplePredicate evaluates to unknwon if the input value is missing" if(FieldValueUtil.isMissing(value)){ return null; } switch(operator){ case EQUAL: return value.equals(simplePredicate); case NOT_EQUAL: return !value.equals(simplePredicate); default: break; } int order = value.compareTo(simplePredicate); switch(operator){ case LESS_THAN: return Boolean.valueOf(order < 0); case LESS_OR_EQUAL: return Boolean.valueOf(order <= 0); case GREATER_OR_EQUAL: return Boolean.valueOf(order >= 0); case GREATER_THAN: return Boolean.valueOf(order > 0); default: throw new UnsupportedAttributeException(simplePredicate, operator); } }
Example #12
Source File: PredicateInterner.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 4 votes |
private SimplePredicate intern(SimplePredicate simplePredicate){ return this.simplePredicateCache.intern(simplePredicate); }
Example #13
Source File: PredicateInterner.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 4 votes |
@Override public ElementKey createKey(SimplePredicate simplePredicate){ Object[] content = {simplePredicate.getField(), simplePredicate.getOperator(), simplePredicate.getValue()}; return new ElementKey(content); }
Example #14
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 #15
Source File: StringInternerTest.java From jpmml-model with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Test public void intern(){ SimplePredicate left = new CustomSimplePredicate(FieldName.create("x"), SimplePredicate.Operator.LESS_THAN, new String("0")); SimplePredicate right = new CustomSimplePredicate(FieldName.create("y"), SimplePredicate.Operator.LESS_THAN, new String("0")); assertNotSame(left.getValue(), right.getValue()); CompoundPredicate compoundPredicate = new CompoundPredicate(CompoundPredicate.BooleanOperator.OR, null) .addPredicates(left, right); StringInterner interner = new StringInterner(); interner.applyTo(compoundPredicate); assertSame(left.getValue(), right.getValue()); }
Example #16
Source File: FieldReferenceFinder.java From jpmml-model with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Override public VisitorAction visit(SimplePredicate simplePredicate){ process(simplePredicate.getField()); return super.visit(simplePredicate); }
Example #17
Source File: RDFPMMLUtilsTest.java From oryx with Apache License 2.0 | 4 votes |
public static PMML buildDummyRegressionModel() { PMML pmml = PMMLUtils.buildSkeletonPMML(); List<DataField> dataFields = new ArrayList<>(); dataFields.add(new DataField(FieldName.create("foo"), OpType.CONTINUOUS, DataType.DOUBLE)); dataFields.add(new DataField(FieldName.create("bar"), OpType.CONTINUOUS, DataType.DOUBLE)); DataDictionary dataDictionary = new DataDictionary(dataFields).setNumberOfFields(dataFields.size()); pmml.setDataDictionary(dataDictionary); List<MiningField> miningFields = new ArrayList<>(); MiningField predictorMF = new MiningField(FieldName.create("foo")) .setOpType(OpType.CONTINUOUS) .setUsageType(MiningField.UsageType.ACTIVE) .setImportance(0.5); miningFields.add(predictorMF); MiningField targetMF = new MiningField(FieldName.create("bar")) .setOpType(OpType.CONTINUOUS) .setUsageType(MiningField.UsageType.PREDICTED); miningFields.add(targetMF); MiningSchema miningSchema = new MiningSchema(miningFields); double dummyCount = 2.0; Node rootNode = new ComplexNode().setId("r").setRecordCount(dummyCount).setPredicate(new True()); double halfCount = dummyCount / 2; Node left = new ComplexNode() .setId("r-") .setRecordCount(halfCount) .setPredicate(new True()) .setScore("-2.0"); Node right = new ComplexNode().setId("r+").setRecordCount(halfCount) .setPredicate(new SimplePredicate(FieldName.create("foo"), SimplePredicate.Operator.GREATER_THAN, "3.14")) .setScore("2.0"); rootNode.addNodes(right, left); TreeModel treeModel = new TreeModel(MiningFunction.REGRESSION, miningSchema, rootNode) .setSplitCharacteristic(TreeModel.SplitCharacteristic.BINARY_SPLIT) .setMissingValueStrategy(TreeModel.MissingValueStrategy.DEFAULT_CHILD) .setMiningSchema(miningSchema); pmml.addModels(treeModel); return pmml; }
Example #18
Source File: RPartConverter.java From jpmml-r with GNU Affero General Public License v3.0 | 4 votes |
private List<Predicate> encodePredicates(Feature feature, int splitOffset, RNumberVector<?> splits, RIntegerVector csplit){ Predicate leftPredicate; Predicate rightPredicate; RIntegerVector splitsDim = splits.dim(); int splitRows = splitsDim.getValue(0); int splitColumns = splitsDim.getValue(1); List<? extends Number> ncat = FortranMatrixUtil.getColumn(splits.getValues(), splitRows, splitColumns, 1); List<? extends Number> index = FortranMatrixUtil.getColumn(splits.getValues(), splitRows, splitColumns, 3); int splitType = ValueUtil.asInt(ncat.get(splitOffset)); Number splitValue = index.get(splitOffset); if(Math.abs(splitType) == 1){ SimplePredicate.Operator leftOperator; SimplePredicate.Operator rightOperator; if(splitType == -1){ leftOperator = SimplePredicate.Operator.LESS_THAN; rightOperator = SimplePredicate.Operator.GREATER_OR_EQUAL; } else { leftOperator = SimplePredicate.Operator.GREATER_OR_EQUAL; rightOperator = SimplePredicate.Operator.LESS_THAN; } leftPredicate = createSimplePredicate(feature, leftOperator, splitValue); rightPredicate = createSimplePredicate(feature, rightOperator, splitValue); } else { CategoricalFeature categoricalFeature = (CategoricalFeature)feature; RIntegerVector csplitDim = csplit.dim(); int csplitRows = csplitDim.getValue(0); int csplitColumns = csplitDim.getValue(1); List<Integer> csplitRow = FortranMatrixUtil.getRow(csplit.getValues(), csplitRows, csplitColumns, ValueUtil.asInt(splitValue) - 1); List<?> values = categoricalFeature.getValues(); leftPredicate = createSimpleSetPredicate(categoricalFeature, selectValues(values, csplitRow, 1)); rightPredicate = createSimpleSetPredicate(categoricalFeature, selectValues(values, csplitRow, 3)); } return Arrays.asList(leftPredicate, rightPredicate); }
Example #19
Source File: TreeModelConverter.java From jpmml-r with GNU Affero General Public License v3.0 | 4 votes |
public Predicate createSimplePredicate(Feature feature, SimplePredicate.Operator operator, Object value){ PredicateManager predicateManager = getPredicateManager(); return predicateManager.createSimplePredicate(feature, operator, value); }
Example #20
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 #21
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 #22
Source File: ValueParserTest.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 2 votes |
@Test public void parseTreeModel(){ DataField dataField = new DataField(FieldName.create("x1"), OpType.CATEGORICAL, DataType.STRING); DataDictionary dataDictionary = new DataDictionary() .addDataFields(dataField); NormDiscrete normDiscrete = new NormDiscrete(dataField.getName(), "1"); DerivedField derivedField = new DerivedField(FieldName.create("global(" + dataField.getName() + ")"), OpType.CATEGORICAL, DataType.STRING, normDiscrete); TransformationDictionary transformationDictionary = new TransformationDictionary() .addDerivedFields(derivedField); SimplePredicate simplePredicate = new SimplePredicate(derivedField.getName(), SimplePredicate.Operator.EQUAL, "1"); Node child = new LeafNode("1", simplePredicate); SimpleSetPredicate simpleSetPredicate = new SimpleSetPredicate(dataField.getName(), SimpleSetPredicate.BooleanOperator.IS_IN, new Array(Array.Type.STRING, "0 1")); Node root = new BranchNode("0", simpleSetPredicate) .addNodes(child); MiningField miningField = new MiningField(dataField.getName()); MiningSchema miningSchema = new MiningSchema() .addMiningFields(miningField); TreeModel treeModel = new TreeModel(MiningFunction.REGRESSION, miningSchema, null) .setNode(root); PMML pmml = new PMML(Version.PMML_4_3.getVersion(), new Header(), dataDictionary) .setTransformationDictionary(transformationDictionary) .addModels(treeModel); List<DataField> dataFields = dataDictionary.getDataFields(); ValueParser parser = new ValueParser(ValueParser.Mode.STRICT); parser.applyTo(pmml); dataField = dataFields.get(0); assertEquals("1", normDiscrete.getValue()); assertEquals("1", simplePredicate.getValue()); Array array = simpleSetPredicate.getArray(); assertEquals(ImmutableSet.of("0", "1"), array.getValue()); dataField.setDataType(DataType.INTEGER); parser.applyTo(pmml); dataField = dataFields.get(0); assertEquals(1, normDiscrete.getValue()); assertEquals("1", simplePredicate.getValue()); array = simpleSetPredicate.getArray(); assertTrue(array instanceof RichComplexArray); assertEquals(ImmutableSet.of(0, 1), array.getValue()); dataField.setDataType(DataType.DOUBLE); derivedField.setDataType(DataType.INTEGER); parser.applyTo(pmml); dataField = dataFields.get(0); assertEquals(1.0d, normDiscrete.getValue()); assertEquals(1, simplePredicate.getValue()); array = simpleSetPredicate.getArray(); assertEquals(ImmutableSet.of(0.0d, 1.0d), array.getValue()); dataField.setDataType(DataType.BOOLEAN); derivedField.setDataType(DataType.DOUBLE); parser.applyTo(pmml); dataField = dataFields.get(0); assertEquals(true, normDiscrete.getValue()); assertEquals(1.0d, simplePredicate.getValue()); array = simpleSetPredicate.getArray(); assertEquals(ImmutableSet.of(false, true), array.getValue()); derivedField.setDataType(DataType.BOOLEAN); parser.applyTo(pmml); dataField = dataFields.get(0); assertEquals(true, normDiscrete.getValue()); assertEquals(true, simplePredicate.getValue()); }
Example #23
Source File: PredicateUtilTest.java From jpmml-evaluator with GNU Affero General Public License v3.0 | 2 votes |
@Test public void evaluateSimplePredicate(){ FieldName age = FieldName.create("age"); SimplePredicate simplePredicate = new SimplePredicate(age, SimplePredicate.Operator.IS_MISSING, null); assertEquals(Boolean.FALSE, evaluate(simplePredicate, age, 30)); assertEquals(Boolean.TRUE, evaluate(simplePredicate, age, null)); simplePredicate.setOperator(SimplePredicate.Operator.IS_NOT_MISSING); assertEquals(Boolean.TRUE, evaluate(simplePredicate, age, 30)); assertEquals(Boolean.FALSE, evaluate(simplePredicate, age, null)); simplePredicate.setValue("30"); simplePredicate.setOperator(SimplePredicate.Operator.EQUAL); assertEquals(Boolean.TRUE, evaluate(simplePredicate, age, 30)); assertEquals(null, evaluate(simplePredicate, age, null)); simplePredicate.setOperator(SimplePredicate.Operator.NOT_EQUAL); assertEquals(Boolean.FALSE, evaluate(simplePredicate, age, 30)); assertEquals(null, evaluate(simplePredicate, age, null)); simplePredicate.setOperator(SimplePredicate.Operator.LESS_THAN); assertEquals(Boolean.FALSE, evaluate(simplePredicate, age, 30)); simplePredicate.setOperator(SimplePredicate.Operator.LESS_OR_EQUAL); assertEquals(Boolean.TRUE, evaluate(simplePredicate, age, 30)); simplePredicate.setOperator(SimplePredicate.Operator.GREATER_OR_EQUAL); assertEquals(Boolean.TRUE, evaluate(simplePredicate, age, 30)); simplePredicate.setOperator(SimplePredicate.Operator.GREATER_THAN); assertEquals(Boolean.FALSE, evaluate(simplePredicate, age, 30)); }
Example #24
Source File: ObjectMapperTest.java From jpmml-model with BSD 3-Clause "New" or "Revised" License | 2 votes |
@Test public void jsonClone() throws Exception { DataField dataField = new DataField(FieldName.create("x"), OpType.CATEGORICAL, DataType.BOOLEAN); DataDictionary dataDictionary = new DataDictionary() .addDataFields(dataField); MiningField miningField = new MiningField(FieldName.create("x")); MiningSchema miningSchema = new MiningSchema() .addMiningFields(miningField); assertSame(dataField.getName(), miningField.getName()); SimplePredicate simplePredicate = new SimplePredicate(FieldName.create("x"), SimplePredicate.Operator.IS_NOT_MISSING, null); Node node = new ComplexNode(simplePredicate); TreeModel treeModel = new TreeModel() .setMiningSchema(miningSchema) .setNode(node); PMML pmml = new PMML() .setDataDictionary(dataDictionary) .addModels(treeModel); DirectByteArrayOutputStream buffer = new DirectByteArrayOutputStream(1024); JacksonUtil.writePMML(pmml, buffer); PMML jsonPmml; try(InputStream is = buffer.getInputStream()){ jsonPmml = JacksonUtil.readPMML(is); } DataDictionary jsonDataDictionary = jsonPmml.getDataDictionary(); List<DataField> jsonDataFields = jsonDataDictionary.getDataFields(); assertEquals(1, jsonDataFields.size()); DataField jsonDataField = jsonDataFields.get(0); assertEquals(dataField.getName(), jsonDataField.getName()); assertEquals(dataField.getOpType(), jsonDataField.getOpType()); assertEquals(dataField.getDataType(), jsonDataField.getDataType()); List<Model> jsonModels = jsonPmml.getModels(); assertEquals(1, jsonModels.size()); TreeModel jsonTreeModel = (TreeModel)jsonModels.get(0); MiningSchema jsonMiningSchema = jsonTreeModel.getMiningSchema(); List<MiningField> jsonMiningFields = jsonMiningSchema.getMiningFields(); assertEquals(1, jsonMiningFields.size()); MiningField jsonMiningField = jsonMiningFields.get(0); assertEquals(miningField.getName(), jsonMiningField.getName()); assertEquals(miningField.getUsageType(), jsonMiningField.getUsageType()); assertSame(jsonDataField.getName(), jsonMiningField.getName()); Node jsonNode = jsonTreeModel.getNode(); SimplePredicate jsonSimplePredicate = (SimplePredicate)jsonNode.getPredicate(); assertEquals(simplePredicate.getField(), jsonSimplePredicate.getField()); assertEquals(simplePredicate.getOperator(), jsonSimplePredicate.getOperator()); assertSame(jsonDataField.getName(), jsonSimplePredicate.getField()); assertSame(jsonMiningField.getName(), jsonSimplePredicate.getField()); }