use of org.knime.base.node.mine.treeensemble2.data.TreeNominalColumnData in project knime-core by knime.
the class TreeNodeNominalBinaryConditionTest method testToPMMLPredicate.
/**
* This method tests the
* {@link TreeNodeNominalBinaryCondition#toPMMLPredicate()} method.
*
* @throws Exception
*/
@Test
public void testToPMMLPredicate() throws Exception {
final TreeEnsembleLearnerConfiguration config = new TreeEnsembleLearnerConfiguration(false);
final TestDataGenerator dataGen = new TestDataGenerator(config);
final TreeNominalColumnData col = dataGen.createNominalAttributeColumn("A,A,B,C,C,D", "testcol", 0);
TreeNodeNominalBinaryCondition cond = new TreeNodeNominalBinaryCondition(col.getMetaData(), BigInteger.valueOf(1), true, false);
PMMLPredicate predicate = cond.toPMMLPredicate();
assertThat(predicate, instanceOf(PMMLSimpleSetPredicate.class));
PMMLSimpleSetPredicate setPredicate = (PMMLSimpleSetPredicate) predicate;
assertEquals("Wrong attribute", col.getMetaData().getAttributeName(), setPredicate.getSplitAttribute());
assertEquals("Wrong set predicate", PMMLSetOperator.IS_IN, setPredicate.getSetOperator());
assertArrayEquals("Wrong values", new String[] { "A" }, setPredicate.getValues().toArray(new String[1]));
cond = new TreeNodeNominalBinaryCondition(col.getMetaData(), BigInteger.valueOf(2), false, true);
predicate = cond.toPMMLPredicate();
assertEquals("Wrong attribute", col.getMetaData().getAttributeName(), predicate.getSplitAttribute());
assertThat(predicate, instanceOf(PMMLCompoundPredicate.class));
PMMLCompoundPredicate compoundPredicate = (PMMLCompoundPredicate) predicate;
assertEquals("Wrong boolean operator", PMMLBooleanOperator.OR, compoundPredicate.getBooleanOperator());
LinkedList<PMMLPredicate> preds = compoundPredicate.getPredicates();
assertEquals("Number of predicates did not match.", 2, preds.size());
assertThat(preds.get(0), instanceOf(PMMLSimpleSetPredicate.class));
setPredicate = (PMMLSimpleSetPredicate) preds.get(0);
assertEquals("Wrong attribute", col.getMetaData().getAttributeName(), setPredicate.getSplitAttribute());
assertEquals("Wrong set predicate", PMMLSetOperator.IS_NOT_IN, setPredicate.getSetOperator());
assertArrayEquals("Wrong values", new String[] { "B" }, setPredicate.getValues().toArray(new String[1]));
assertThat(preds.get(1), instanceOf(PMMLSimplePredicate.class));
PMMLSimplePredicate simplePredicate = (PMMLSimplePredicate) preds.get(1);
assertEquals("Should be isMissing", PMMLOperator.IS_MISSING, simplePredicate.getOperator());
}
use of org.knime.base.node.mine.treeensemble2.data.TreeNominalColumnData in project knime-core by knime.
the class TreeNodeNominalBinaryConditionTest method testTestCondition.
/**
* This method tests the
* {@link TreeNodeNominalBinaryCondition#testCondition(org.knime.base.node.mine.treeensemble2.data.PredictorRecord)}
* method.
*
* @throws Exception
*/
@Test
public void testTestCondition() throws Exception {
final TreeEnsembleLearnerConfiguration config = new TreeEnsembleLearnerConfiguration(false);
final TestDataGenerator dataGen = new TestDataGenerator(config);
final TreeNominalColumnData col = dataGen.createNominalAttributeColumn("A,A,B,C,C,D", "testcol", 0);
TreeNodeNominalBinaryCondition cond = new TreeNodeNominalBinaryCondition(col.getMetaData(), BigInteger.valueOf(1), true, false);
final Map<String, Object> map = Maps.newHashMap();
final String colName = col.getMetaData().getAttributeName();
map.put(colName, 0);
PredictorRecord record = new PredictorRecord(map);
assertTrue("The value A was not accepted but should have been.", cond.testCondition(record));
map.clear();
map.put(colName, 1);
assertFalse("The value B was falsely accepted", cond.testCondition(record));
map.clear();
map.put(colName, 2);
assertFalse("The value C was falsely accepted", cond.testCondition(record));
map.clear();
map.put(colName, 3);
assertFalse("The value D was falsely accepted", cond.testCondition(record));
map.clear();
map.put(colName, PredictorRecord.NULL);
assertFalse("The condition falsely accepted missing values", cond.testCondition(record));
cond = new TreeNodeNominalBinaryCondition(col.getMetaData(), BigInteger.valueOf(5), true, true);
map.clear();
map.put(colName, 0);
assertTrue("The value A was falsely rejected.", cond.testCondition(record));
map.clear();
map.put(colName, 2);
assertTrue("The value C was falsely rejected.", cond.testCondition(record));
map.clear();
map.put(colName, PredictorRecord.NULL);
assertTrue("Missing values were falsely rejected.", cond.testCondition(record));
map.clear();
map.put(colName, 1);
assertFalse("The value B was falsely accepted.", cond.testCondition(record));
map.clear();
map.put(colName, 3);
assertFalse("The value B was falsely accepted.", cond.testCondition(record));
cond = new TreeNodeNominalBinaryCondition(col.getMetaData(), BigInteger.valueOf(5), false, true);
map.clear();
map.put(colName, 0);
assertFalse("The value A was falsely accepted.", cond.testCondition(record));
map.clear();
map.put(colName, 2);
assertFalse("The value C was falsely accepted.", cond.testCondition(record));
map.clear();
map.put(colName, PredictorRecord.NULL);
assertTrue("Missing values were falsely rejected.", cond.testCondition(record));
map.clear();
map.put(colName, 1);
assertTrue("The value B was falsely rejected.", cond.testCondition(record));
map.clear();
map.put(colName, 3);
assertTrue("The value D was falsely rejected.", cond.testCondition(record));
cond = new TreeNodeNominalBinaryCondition(col.getMetaData(), BigInteger.valueOf(5), false, false);
map.clear();
map.put(colName, 0);
assertFalse("The value A was falsely accepted.", cond.testCondition(record));
map.clear();
map.put(colName, 2);
assertFalse("The value C was falsely accepted.", cond.testCondition(record));
map.clear();
map.put(colName, PredictorRecord.NULL);
assertFalse("Missing values were falsely accepted.", cond.testCondition(record));
map.clear();
map.put(colName, 1);
assertTrue("The value B was falsely rejected.", cond.testCondition(record));
map.clear();
map.put(colName, 3);
assertTrue("The value D was falsely rejected.", cond.testCondition(record));
}
use of org.knime.base.node.mine.treeensemble2.data.TreeNominalColumnData in project knime-core by knime.
the class TreeTargetNumericColumnDataTest method testGetPriors.
/**
* Tests the {@link TreeTargetNumericColumnData#getPriors(DataMemberships, TreeEnsembleLearnerConfiguration)} and
* {@link TreeTargetNumericColumnData#getPriors(double[], TreeEnsembleLearnerConfiguration)} methods.
*/
@Test
public void testGetPriors() {
String targetCSV = "1,4,3,5,6,7,8,12,22,1";
// irrelevant but necessary to build TreeDataObject
String someAttributeCSV = "A,B,A,B,A,A,B,A,A,B";
TreeEnsembleLearnerConfiguration config = new TreeEnsembleLearnerConfiguration(true);
TestDataGenerator dataGen = new TestDataGenerator(config);
TreeTargetNumericColumnData target = TestDataGenerator.createNumericTargetColumn(targetCSV);
TreeNominalColumnData attribute = dataGen.createNominalAttributeColumn(someAttributeCSV, "test-col", 0);
TreeData data = new TreeData(new TreeAttributeColumnData[] { attribute }, target, TreeType.Ordinary);
double[] weights = new double[10];
Arrays.fill(weights, 1.0);
DataMemberships rootMem = new RootDataMemberships(weights, data, new DefaultDataIndexManager(data));
RegressionPriors datMemPriors = target.getPriors(rootMem, config);
assertEquals(6.9, datMemPriors.getMean(), DELTA);
assertEquals(69, datMemPriors.getYSum(), DELTA);
assertEquals(352.9, datMemPriors.getSumSquaredDeviation(), DELTA);
}
use of org.knime.base.node.mine.treeensemble2.data.TreeNominalColumnData in project knime-core by knime.
the class TreeNodeNominalConditionTest method testToPMMLPredicate.
/**
* This method tests the {@link TreeNodeNominalCondition#toPMMLPredicate()} method.
*
* @throws Exception
*/
@Test
public void testToPMMLPredicate() throws Exception {
final TreeEnsembleLearnerConfiguration config = new TreeEnsembleLearnerConfiguration(false);
final TestDataGenerator dataGen = new TestDataGenerator(config);
final TreeNominalColumnData col = dataGen.createNominalAttributeColumn("A,A,B,C,C,D", "testcol", 0);
TreeNodeNominalCondition cond = new TreeNodeNominalCondition(col.getMetaData(), 3, false);
PMMLPredicate predicate = cond.toPMMLPredicate();
assertThat(predicate, instanceOf(PMMLSimplePredicate.class));
PMMLSimplePredicate simplePredicate = (PMMLSimplePredicate) predicate;
assertEquals("Wrong operator", PMMLOperator.EQUAL, simplePredicate.getOperator());
assertEquals("Wrong split value", "D", simplePredicate.getThreshold());
cond = new TreeNodeNominalCondition(col.getMetaData(), 0, true);
predicate = cond.toPMMLPredicate();
assertThat(predicate, instanceOf(PMMLCompoundPredicate.class));
PMMLCompoundPredicate compound = (PMMLCompoundPredicate) predicate;
assertEquals("Wrong boolean operator.", PMMLBooleanOperator.OR, compound.getBooleanOperator());
List<PMMLPredicate> preds;
preds = compound.getPredicates();
assertEquals("Wrong number of predicates in compound predicate.", 2, preds.size());
assertThat(preds.get(0), instanceOf(PMMLSimplePredicate.class));
simplePredicate = (PMMLSimplePredicate) preds.get(0);
assertEquals("Wrong operator", PMMLOperator.EQUAL, simplePredicate.getOperator());
assertEquals("Wrong split value", "A", simplePredicate.getThreshold());
assertEquals("Wrong attribute.", col.getMetaData().getAttributeName(), simplePredicate.getSplitAttribute());
assertThat(preds.get(1), instanceOf(PMMLSimplePredicate.class));
simplePredicate = (PMMLSimplePredicate) preds.get(1);
assertEquals("Should be isMissing", PMMLOperator.IS_MISSING, simplePredicate.getOperator());
}
use of org.knime.base.node.mine.treeensemble2.data.TreeNominalColumnData in project knime-core by knime.
the class TreeNodeNominalConditionTest method testTestCondition.
/**
* This method tests the
* {@link TreeNodeNominalCondition#testCondition(org.knime.base.node.mine.treeensemble2.data.PredictorRecord)}
* method
*
* @throws Exception
*/
@Test
public void testTestCondition() throws Exception {
final TreeEnsembleLearnerConfiguration config = new TreeEnsembleLearnerConfiguration(false);
final TestDataGenerator dataGen = new TestDataGenerator(config);
final TreeNominalColumnData col = dataGen.createNominalAttributeColumn("A,A,B,C,C,D", "testcol", 0);
TreeNodeNominalCondition cond = new TreeNodeNominalCondition(col.getMetaData(), 3, false);
final Map<String, Object> map = Maps.newHashMap();
final String colName = col.getMetaData().getAttributeName();
map.put(colName, 0);
final PredictorRecord record = new PredictorRecord(map);
assertFalse("The value A was falsely accepted", cond.testCondition(record));
map.clear();
map.put(colName, 1);
assertFalse("The value B was falsely accepted", cond.testCondition(record));
map.clear();
map.put(colName, 2);
assertFalse("The value C was falsely accepted", cond.testCondition(record));
map.clear();
map.put(colName, 3);
assertTrue("The value D was falsely rejected", cond.testCondition(record));
map.clear();
map.put(colName, PredictorRecord.NULL);
assertFalse("Missing values were falsely accepted", cond.testCondition(record));
cond = new TreeNodeNominalCondition(col.getMetaData(), 0, true);
map.clear();
map.put(colName, 0);
assertTrue("The value A was falsely rejected", cond.testCondition(record));
map.clear();
map.put(colName, 1);
assertFalse("The value B was falsely accepted", cond.testCondition(record));
map.clear();
map.put(colName, 2);
assertFalse("The value C was falsely accepted", cond.testCondition(record));
map.clear();
map.put(colName, 3);
assertFalse("The value D was falsely accepted", cond.testCondition(record));
map.clear();
map.put(colName, PredictorRecord.NULL);
assertTrue("Missing values were falsely rejected", cond.testCondition(record));
}
Aggregations