Search in sources :

Example 11 with TreeNodeNominalBinaryCondition

use of org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalBinaryCondition in project knime-core by knime.

the class TreeNominalColumnDataTest method testCalcBestSplitClassificationBinary.

/**
 * Tests the method
 * {@link TreeNominalColumnData#calcBestSplitClassification(DataMemberships, ClassificationPriors, TreeTargetNominalColumnData, RandomData)}
 * using binary splits.
 *
 * @throws Exception
 */
@Test
public void testCalcBestSplitClassificationBinary() throws Exception {
    final TreeEnsembleLearnerConfiguration config = createConfig(false);
    Pair<TreeNominalColumnData, TreeTargetNominalColumnData> tennisData = tennisData(config);
    TreeNominalColumnData columnData = tennisData.getFirst();
    TreeTargetNominalColumnData targetData = tennisData.getSecond();
    assertEquals(SplitCriterion.Gini, config.getSplitCriterion());
    double[] rowWeights = new double[SMALL_COLUMN_DATA.length];
    Arrays.fill(rowWeights, 1.0);
    TreeData tennisTreeData = tennisTreeData(config);
    IDataIndexManager indexManager = new DefaultDataIndexManager(tennisTreeData);
    DataMemberships dataMemberships = new RootDataMemberships(rowWeights, tennisTreeData, indexManager);
    ClassificationPriors priors = targetData.getDistribution(rowWeights, config);
    SplitCandidate splitCandidate = columnData.calcBestSplitClassification(dataMemberships, priors, targetData, null);
    assertNotNull(splitCandidate);
    assertThat(splitCandidate, instanceOf(NominalBinarySplitCandidate.class));
    assertTrue(splitCandidate.canColumnBeSplitFurther());
    // manually via libre office calc
    assertEquals(0.0689342404, splitCandidate.getGainValue(), 0.00001);
    NominalBinarySplitCandidate binSplitCandidate = (NominalBinarySplitCandidate) splitCandidate;
    TreeNodeNominalBinaryCondition[] childConditions = binSplitCandidate.getChildConditions();
    assertEquals(2, childConditions.length);
    assertArrayEquals(new String[] { "R" }, childConditions[0].getValues());
    assertArrayEquals(new String[] { "R" }, childConditions[1].getValues());
    assertEquals(SetLogic.IS_NOT_IN, childConditions[0].getSetLogic());
    assertEquals(SetLogic.IS_IN, childConditions[1].getSetLogic());
    BitSet inChild = columnData.updateChildMemberships(childConditions[0], dataMemberships);
    DataMemberships child1Memberships = dataMemberships.createChildMemberships(inChild);
    ClassificationPriors childTargetPriors = targetData.getDistribution(child1Memberships, config);
    SplitCandidate splitCandidateChild = columnData.calcBestSplitClassification(child1Memberships, childTargetPriors, targetData, null);
    assertNotNull(splitCandidateChild);
    assertThat(splitCandidateChild, instanceOf(NominalBinarySplitCandidate.class));
    // manually via libre office calc
    assertEquals(0.0086419753, splitCandidateChild.getGainValue(), 0.00001);
    inChild = columnData.updateChildMemberships(childConditions[1], dataMemberships);
    DataMemberships child2Memberships = dataMemberships.createChildMemberships(inChild);
    childTargetPriors = targetData.getDistribution(child2Memberships, config);
    splitCandidateChild = columnData.calcBestSplitClassification(child2Memberships, childTargetPriors, targetData, null);
    assertNull(splitCandidateChild);
}
Also used : TreeEnsembleLearnerConfiguration(org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration) RootDataMemberships(org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships) BitSet(java.util.BitSet) IDataIndexManager(org.knime.base.node.mine.treeensemble2.data.memberships.IDataIndexManager) NominalMultiwaySplitCandidate(org.knime.base.node.mine.treeensemble2.learner.NominalMultiwaySplitCandidate) NominalBinarySplitCandidate(org.knime.base.node.mine.treeensemble2.learner.NominalBinarySplitCandidate) SplitCandidate(org.knime.base.node.mine.treeensemble2.learner.SplitCandidate) DefaultDataIndexManager(org.knime.base.node.mine.treeensemble2.data.memberships.DefaultDataIndexManager) DataMemberships(org.knime.base.node.mine.treeensemble2.data.memberships.DataMemberships) RootDataMemberships(org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships) TreeNodeNominalBinaryCondition(org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalBinaryCondition) NominalBinarySplitCandidate(org.knime.base.node.mine.treeensemble2.learner.NominalBinarySplitCandidate) Test(org.junit.Test)

Example 12 with TreeNodeNominalBinaryCondition

use of org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalBinaryCondition in project knime-core by knime.

the class TreeNominalColumnDataTest method testCalcBestSplitClassificationBinaryPCA.

/**
 * Tests the method
 * {@link TreeNominalColumnData#calcBestSplitClassification(DataMemberships, ClassificationPriors, TreeTargetNominalColumnData, RandomData)}
 * using binary splits. In this test case the data has more than two classes and the used algorithm is therefore PCA
 * based.
 *
 * @throws Exception
 */
@Test
public void testCalcBestSplitClassificationBinaryPCA() throws Exception {
    TreeEnsembleLearnerConfiguration config = createConfig(false);
    Pair<TreeNominalColumnData, TreeTargetNominalColumnData> pcaData = createPCATestData(config);
    TreeNominalColumnData columnData = pcaData.getFirst();
    TreeTargetNominalColumnData targetData = pcaData.getSecond();
    TreeData treeData = createTreeData(pcaData);
    assertEquals(SplitCriterion.Gini, config.getSplitCriterion());
    double[] rowWeights = new double[targetData.getNrRows()];
    Arrays.fill(rowWeights, 1.0);
    IDataIndexManager indexManager = new DefaultDataIndexManager(treeData);
    DataMemberships dataMemberships = new RootDataMemberships(rowWeights, treeData, indexManager);
    ClassificationPriors priors = targetData.getDistribution(rowWeights, config);
    SplitCandidate splitCandidate = columnData.calcBestSplitClassification(dataMemberships, priors, targetData, null);
    assertNotNull(splitCandidate);
    assertThat(splitCandidate, instanceOf(NominalBinarySplitCandidate.class));
    assertTrue(splitCandidate.canColumnBeSplitFurther());
    assertEquals(0.0659, splitCandidate.getGainValue(), 0.0001);
    NominalBinarySplitCandidate binarySplitCandidate = (NominalBinarySplitCandidate) splitCandidate;
    TreeNodeNominalBinaryCondition[] childConditions = binarySplitCandidate.getChildConditions();
    assertEquals(2, childConditions.length);
    assertArrayEquals(new String[] { "E" }, childConditions[0].getValues());
    assertArrayEquals(new String[] { "E" }, childConditions[1].getValues());
    assertEquals(SetLogic.IS_NOT_IN, childConditions[0].getSetLogic());
    assertEquals(SetLogic.IS_IN, childConditions[1].getSetLogic());
}
Also used : TreeEnsembleLearnerConfiguration(org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration) RootDataMemberships(org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships) IDataIndexManager(org.knime.base.node.mine.treeensemble2.data.memberships.IDataIndexManager) NominalMultiwaySplitCandidate(org.knime.base.node.mine.treeensemble2.learner.NominalMultiwaySplitCandidate) NominalBinarySplitCandidate(org.knime.base.node.mine.treeensemble2.learner.NominalBinarySplitCandidate) SplitCandidate(org.knime.base.node.mine.treeensemble2.learner.SplitCandidate) DefaultDataIndexManager(org.knime.base.node.mine.treeensemble2.data.memberships.DefaultDataIndexManager) DataMemberships(org.knime.base.node.mine.treeensemble2.data.memberships.DataMemberships) RootDataMemberships(org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships) TreeNodeNominalBinaryCondition(org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalBinaryCondition) NominalBinarySplitCandidate(org.knime.base.node.mine.treeensemble2.learner.NominalBinarySplitCandidate) Test(org.junit.Test)

Example 13 with TreeNodeNominalBinaryCondition

use of org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalBinaryCondition in project knime-core by knime.

the class TreeNominalColumnDataTest method testUpdateChildMemberships.

/**
 * Tests the method
 * {@link TreeNominalColumnData#updateChildMemberships(org.knime.base.node.mine.treeensemble2.model.TreeNodeCondition, DataMemberships)}
 * .
 *
 * @throws Exception
 */
@Test
public void testUpdateChildMemberships() throws Exception {
    // in this case it doesn't matter if we use regression or classification (as well as binary and multiway splits)
    final TreeEnsembleLearnerConfiguration config = createConfig(true);
    final TestDataGenerator dataGen = new TestDataGenerator(config);
    final String dataCSV = "A, A, A, A, B, B, B, C, C, C, ?, ?";
    TreeNominalColumnData col = dataGen.createNominalAttributeColumn(dataCSV, "test-col", 0);
    final int[] indices = new int[12];
    final double[] weights = new double[indices.length];
    for (int i = 0; i < indices.length; i++) {
        indices[i] = i;
        weights[i] = 1.0;
    }
    final DataMemberships dataMem = new MockDataColMem(indices, indices, weights);
    TreeNodeNominalBinaryCondition binCond = new TreeNodeNominalBinaryCondition(col.getMetaData(), BigInteger.valueOf(2), true, false);
    BitSet expected = new BitSet(12);
    BitSet inChild = col.updateChildMemberships(binCond, dataMem);
    expected.set(4, 7);
    assertEquals("The produced BitSet is incorrect.", expected, inChild);
    binCond = new TreeNodeNominalBinaryCondition(col.getMetaData(), BigInteger.valueOf(2), true, true);
    expected.clear();
    expected.set(4, 7);
    expected.set(10, 12);
    inChild = col.updateChildMemberships(binCond, dataMem);
    assertEquals("The produced BitSet is incorrect.", expected, inChild);
    binCond = new TreeNodeNominalBinaryCondition(col.getMetaData(), BigInteger.valueOf(2), false, false);
    expected.clear();
    expected.set(0, 4);
    expected.set(7, 10);
    inChild = col.updateChildMemberships(binCond, dataMem);
    assertEquals("The produced BitSet is incorrect.", expected, inChild);
    binCond = new TreeNodeNominalBinaryCondition(col.getMetaData(), BigInteger.valueOf(2), false, true);
    expected.clear();
    expected.set(0, 4);
    expected.set(7, 12);
    inChild = col.updateChildMemberships(binCond, dataMem);
    assertEquals("The produced BitSet is incorrect.", expected, inChild);
    binCond = new TreeNodeNominalBinaryCondition(col.getMetaData(), BigInteger.valueOf(5), true, false);
    expected.clear();
    expected.set(0, 4);
    expected.set(7, 10);
    inChild = col.updateChildMemberships(binCond, dataMem);
    assertEquals("The produced BitSet is incorrect.", expected, inChild);
    binCond = new TreeNodeNominalBinaryCondition(col.getMetaData(), BigInteger.valueOf(5), true, true);
    expected.clear();
    expected.set(0, 4);
    expected.set(7, 12);
    inChild = col.updateChildMemberships(binCond, dataMem);
    assertEquals("The produced BitSet is incorrect.", expected, inChild);
    TreeNodeNominalCondition multiCond = new TreeNodeNominalCondition(col.getMetaData(), 0, false);
    expected.clear();
    expected.set(0, 4);
    inChild = col.updateChildMemberships(multiCond, dataMem);
    assertEquals("The produced BitSet is incorrect.", expected, inChild);
    multiCond = new TreeNodeNominalCondition(col.getMetaData(), 0, true);
    expected.clear();
    expected.set(0, 4);
    expected.set(10, 12);
    inChild = col.updateChildMemberships(multiCond, dataMem);
    assertEquals("The produced BitSet is incorrect.", expected, inChild);
    multiCond = new TreeNodeNominalCondition(col.getMetaData(), 2, false);
    expected.clear();
    expected.set(7, 10);
    inChild = col.updateChildMemberships(multiCond, dataMem);
    assertEquals("The produced BitSet is incorrect.", expected, inChild);
    multiCond = new TreeNodeNominalCondition(col.getMetaData(), 2, true);
    expected.clear();
    expected.set(7, 12);
    inChild = col.updateChildMemberships(multiCond, dataMem);
    assertEquals("The produced BitSet is incorrect.", expected, inChild);
}
Also used : TreeEnsembleLearnerConfiguration(org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration) TreeNodeNominalCondition(org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalCondition) TreeNodeNominalBinaryCondition(org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalBinaryCondition) BitSet(java.util.BitSet) DataMemberships(org.knime.base.node.mine.treeensemble2.data.memberships.DataMemberships) RootDataMemberships(org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships) Test(org.junit.Test)

Aggregations

TreeEnsembleLearnerConfiguration (org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration)11 Test (org.junit.Test)10 TreeNodeNominalBinaryCondition (org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalBinaryCondition)10 DataMemberships (org.knime.base.node.mine.treeensemble2.data.memberships.DataMemberships)9 RootDataMemberships (org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships)9 NominalBinarySplitCandidate (org.knime.base.node.mine.treeensemble2.learner.NominalBinarySplitCandidate)8 NominalMultiwaySplitCandidate (org.knime.base.node.mine.treeensemble2.learner.NominalMultiwaySplitCandidate)8 SplitCandidate (org.knime.base.node.mine.treeensemble2.learner.SplitCandidate)8 DefaultDataIndexManager (org.knime.base.node.mine.treeensemble2.data.memberships.DefaultDataIndexManager)5 IDataIndexManager (org.knime.base.node.mine.treeensemble2.data.memberships.IDataIndexManager)5 BitSet (java.util.BitSet)3 RandomData (org.apache.commons.math.random.RandomData)3 TestDataGenerator (org.knime.base.node.mine.treeensemble2.data.TestDataGenerator)2 TreeNominalColumnData (org.knime.base.node.mine.treeensemble2.data.TreeNominalColumnData)2 PMMLCompoundPredicate (org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate)1 PMMLPredicate (org.knime.base.node.mine.decisiontree2.PMMLPredicate)1 PMMLSimplePredicate (org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate)1 PMMLSimpleSetPredicate (org.knime.base.node.mine.decisiontree2.PMMLSimpleSetPredicate)1 PredictorRecord (org.knime.base.node.mine.treeensemble2.data.PredictorRecord)1 TreeNominalColumnMetaData (org.knime.base.node.mine.treeensemble2.data.TreeNominalColumnMetaData)1