org.dmg.pmml.tree.BranchNode Java Examples

The following examples show how to use org.dmg.pmml.tree.BranchNode. 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: NodeResolverTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void resolve(){
	Node leftChild = new LeafNode()
		.setId("1");

	Node rightChild = new LeafNode()
		.setId("2");

	Node root = new BranchNode(null, True.INSTANCE)
		.setId("0")
		.setDefaultChild(rightChild.getId())
		.addNodes(leftChild, rightChild);

	TreeModel treeModel = new TreeModel(MiningFunction.REGRESSION, new MiningSchema(), null)
		.setNode(root);

	PMML pmml = new PMML(Version.PMML_4_3.getVersion(), new Header(), new DataDictionary())
		.addModels(treeModel);

	NodeResolver resolver = new NodeResolver();
	resolver.applyTo(pmml);

	assertEquals(rightChild.getId(), root.getDefaultChild());

	treeModel.setMissingValueStrategy(TreeModel.MissingValueStrategy.DEFAULT_CHILD);

	resolver.applyTo(pmml);

	assertSame(rightChild, root.getDefaultChild());
}
 
Example #2
Source File: PredicateInternerTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private void checkTree(Node leftChild, Node rightChild){
	Node root = new BranchNode(null, True.INSTANCE)
		.addNodes(leftChild, rightChild);

	TreeModel treeModel = new TreeModel()
		.setNode(root);

	assertNotSame(leftChild.getPredicate(), rightChild.getPredicate());

	PredicateInterner interner = new PredicateInterner();
	interner.applyTo(treeModel);

	assertSame(leftChild.getPredicate(), rightChild.getPredicate());
}
 
Example #3
Source File: GolfingTreeModelExample.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@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 #4
Source File: HasNodeRegistryTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void getPath(){
	Node node1a = new BranchNode();

	Node node2a = new BranchNode();
	Node node2b = new BranchNode();

	node1a.addNodes(node2a, node2b);

	Node node3a = new BranchNode();
	Node node3b = new BranchNode();

	node2a.addNodes(node3a, node3b);

	Node node3c = new LeafNode();
	Node node3d = new LeafNode();

	node2b.addNodes(node3c, node3d);

	PMML pmml = new PMML(Version.PMML_4_3.getVersion(), new Header(), new DataDictionary())
		.addModels(new TreeModel(MiningFunction.REGRESSION, new MiningSchema(), node1a));

	HasNodeRegistry hasNodeRegistry = new TreeModelEvaluator(pmml);

	BiMap<Node, String> nodeRegistry = (hasNodeRegistry.getEntityRegistry()).inverse();

	String id1a = nodeRegistry.get(node1a);

	String id2a = nodeRegistry.get(node2a);
	String id2b = nodeRegistry.get(node2b);

	String id3a = nodeRegistry.get(node3a);
	String id3b = nodeRegistry.get(node3b);
	String id3c = nodeRegistry.get(node3c);
	String id3d = nodeRegistry.get(node3d);

	assertEquals(Arrays.asList(node1a), hasNodeRegistry.getPath(id1a));
	assertEquals(Arrays.asList(node1a, node2a), hasNodeRegistry.getPath(id2a));
	assertEquals(Arrays.asList(node1a, node2a, node3a), hasNodeRegistry.getPath(id3a));

	assertEquals(Arrays.asList(node1a), hasNodeRegistry.getPathBetween(id1a, id1a));
	assertEquals(Arrays.asList(node1a, node2a), hasNodeRegistry.getPathBetween(id1a, id2a));
	assertNull(hasNodeRegistry.getPathBetween(id2a, id1a));
	assertEquals(Arrays.asList(node2a, node3a), hasNodeRegistry.getPathBetween(id2a, id3a));
	assertEquals(Arrays.asList(node2a, node3b), hasNodeRegistry.getPathBetween(id2a, id3b));

	assertNull(hasNodeRegistry.getPathBetween(id2a, id2b));
	assertNull(hasNodeRegistry.getPathBetween(id2a, id3c));
	assertNull(hasNodeRegistry.getPathBetween(id2a, id3d));
}
 
Example #5
Source File: TreePathFinderTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
@Test
public void find(){
	Node node1a = new BranchNode();

	Node node2a = new LeafNode();
	Node node2b = new BranchNode();
	Node node2c = new BranchNode();

	node1a.addNodes(node2a, node2b, node2c);

	Node node3a = new BranchNode();
	Node node3b = new LeafNode();

	node2b.addNodes(node3a);
	node2c.addNodes(node3b);

	Node node4a = new LeafNode();

	node3a.addNodes(node4a);

	TreeModel treeModel = new TreeModel(MiningFunction.CLASSIFICATION, new MiningSchema(), node1a);

	TreePathFinder finder = new TreePathFinder();
	finder.applyTo(treeModel);

	Map<Node, List<Node>> paths = finder.getPaths();

	assertEquals(3, paths.size());

	assertEquals(Arrays.asList(node1a, node2a), paths.get(node2a));
	assertEquals(Arrays.asList(node1a, node2b, node3a, node4a), paths.get(node4a));
	assertEquals(Arrays.asList(node1a, node2c, node3b), paths.get(node3b));
}
 
Example #6
Source File: ArrayListTransformerTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
@Test
public void transform(){
	Node node1a = new BranchNode();

	Node node2a = new BranchNode();
	Node node2b = new LeafNode();

	node1a.addNodes(node2a, node2b);

	Array array = new ComplexArray()
		.setType(Array.Type.INT)
		.setValue(Arrays.asList(-1, 1));

	Predicate predicate = new SimpleSetPredicate(FieldName.create("x"), SimpleSetPredicate.BooleanOperator.IS_IN, array);

	Node node3a = new LeafNode(null, predicate);

	node2a.addNodes(node3a);

	assertTrue(node1a.getNodes() instanceof ArrayList);
	assertTrue(node2a.getNodes() instanceof ArrayList);

	Object value = array.getValue();

	assertTrue(value instanceof ArrayList);
	assertTrue(value instanceof ComplexValue);

	TreeModel treeModel = new TreeModel(MiningFunction.CLASSIFICATION, new MiningSchema(), node1a);

	ArrayListTransformer transformer = new ArrayListTransformer();
	transformer.applyTo(treeModel);

	assertTrue(node1a.getNodes() instanceof DoubletonList);
	assertTrue(node2a.getNodes() instanceof SingletonList);

	value = array.getValue();

	assertTrue(value instanceof ArrayList);
	assertTrue(value instanceof ComplexValue);
}
 
Example #7
Source File: ValueParserTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 2 votes vote down vote up
@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 #8
Source File: NodeScoreParserTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 2 votes vote down vote up
@Test
public void parseAndIntern(){
	Node node1a = new BranchNode("1", True.INSTANCE);

	Node node2a = new LeafNode("2", False.INSTANCE);
	Node node2b = new BranchNode("2.0", False.INSTANCE);
	Node node2c = new LeafNode(2.0f, True.INSTANCE);

	node1a.addNodes(node2a, node2b, node2c);

	Node node3a = new LeafNode("error", False.INSTANCE);

	node2b.addNodes(node3a);

	TreeModel treeModel = new TreeModel(MiningFunction.CLASSIFICATION, new MiningSchema(), node1a)
		.setMathContext(MathContext.FLOAT);

	VisitorBattery visitorBattery = new VisitorBattery();
	visitorBattery.add(NodeScoreParser.class);
	visitorBattery.add(FloatInterner.class);

	visitorBattery.applyTo(treeModel);

	assertEquals("1", node1a.getScore());

	assertEquals("2", node2a.getScore());
	assertEquals("2.0", node2b.getScore());
	assertEquals(2.0f, node2c.getScore());

	assertEquals("error", node3a.getScore());

	treeModel.setMiningFunction(MiningFunction.REGRESSION);

	visitorBattery.applyTo(treeModel);

	assertEquals(1.0f, node1a.getScore());

	assertEquals(2.0f, node2a.getScore());
	assertEquals(2.0f, node2b.getScore());
	assertEquals(2.0f, node2c.getScore());

	assertSame(node2a.getScore(), node2b.getScore());
	assertSame(node2a.getScore(), node2c.getScore());

	assertEquals("error", node3a.getScore());
}