Search in sources :

Example 6 with TreeNominalColumnData

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());
}
Also used : TreeEnsembleLearnerConfiguration(org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration) PMMLSimpleSetPredicate(org.knime.base.node.mine.decisiontree2.PMMLSimpleSetPredicate) PMMLSimplePredicate(org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) TreeNominalColumnData(org.knime.base.node.mine.treeensemble2.data.TreeNominalColumnData) TestDataGenerator(org.knime.base.node.mine.treeensemble2.data.TestDataGenerator) PMMLCompoundPredicate(org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate) Test(org.junit.Test)

Example 7 with TreeNominalColumnData

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));
}
Also used : TreeEnsembleLearnerConfiguration(org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration) PredictorRecord(org.knime.base.node.mine.treeensemble2.data.PredictorRecord) TreeNominalColumnData(org.knime.base.node.mine.treeensemble2.data.TreeNominalColumnData) TestDataGenerator(org.knime.base.node.mine.treeensemble2.data.TestDataGenerator) Test(org.junit.Test)

Example 8 with TreeNominalColumnData

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);
}
Also used : TreeEnsembleLearnerConfiguration(org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration) RootDataMemberships(org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships) RootDataMemberships(org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships) DataMemberships(org.knime.base.node.mine.treeensemble2.data.memberships.DataMemberships) DefaultDataIndexManager(org.knime.base.node.mine.treeensemble2.data.memberships.DefaultDataIndexManager) Test(org.junit.Test)

Example 9 with TreeNominalColumnData

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());
}
Also used : TreeEnsembleLearnerConfiguration(org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration) PMMLSimplePredicate(org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) TreeNominalColumnData(org.knime.base.node.mine.treeensemble2.data.TreeNominalColumnData) TestDataGenerator(org.knime.base.node.mine.treeensemble2.data.TestDataGenerator) PMMLCompoundPredicate(org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate) Test(org.junit.Test)

Example 10 with TreeNominalColumnData

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));
}
Also used : TreeEnsembleLearnerConfiguration(org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration) PredictorRecord(org.knime.base.node.mine.treeensemble2.data.PredictorRecord) TreeNominalColumnData(org.knime.base.node.mine.treeensemble2.data.TreeNominalColumnData) TestDataGenerator(org.knime.base.node.mine.treeensemble2.data.TestDataGenerator) Test(org.junit.Test)

Aggregations

TreeEnsembleLearnerConfiguration (org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration)19 Test (org.junit.Test)18 DataMemberships (org.knime.base.node.mine.treeensemble2.data.memberships.DataMemberships)15 RootDataMemberships (org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships)15 NominalBinarySplitCandidate (org.knime.base.node.mine.treeensemble2.learner.NominalBinarySplitCandidate)13 NominalMultiwaySplitCandidate (org.knime.base.node.mine.treeensemble2.learner.NominalMultiwaySplitCandidate)13 SplitCandidate (org.knime.base.node.mine.treeensemble2.learner.SplitCandidate)13 TreeNodeNominalBinaryCondition (org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalBinaryCondition)10 DefaultDataIndexManager (org.knime.base.node.mine.treeensemble2.data.memberships.DefaultDataIndexManager)9 IDataIndexManager (org.knime.base.node.mine.treeensemble2.data.memberships.IDataIndexManager)8 RandomData (org.apache.commons.math.random.RandomData)6 TreeNodeNominalCondition (org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalCondition)6 TestDataGenerator (org.knime.base.node.mine.treeensemble2.data.TestDataGenerator)4 TreeNominalColumnData (org.knime.base.node.mine.treeensemble2.data.TreeNominalColumnData)4 BitSet (java.util.BitSet)3 PMMLCompoundPredicate (org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate)2 PMMLPredicate (org.knime.base.node.mine.decisiontree2.PMMLPredicate)2 PMMLSimplePredicate (org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate)2 PredictorRecord (org.knime.base.node.mine.treeensemble2.data.PredictorRecord)2 BigInteger (java.math.BigInteger)1