Search in sources :

Example 11 with RootDataMemberships

use of org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships in project knime-core by knime.

the class RootDescendantDataMembershipsTest method testCreateChildDataMemberships.

@Test
public void testCreateChildDataMemberships() {
    TreeEnsembleLearnerConfiguration config = new TreeEnsembleLearnerConfiguration(false);
    TestDataGenerator dataGen = new TestDataGenerator(config);
    TreeData data = dataGen.createTennisData();
    DefaultDataIndexManager indexManager = new DefaultDataIndexManager(data);
    int nrRows = data.getNrRows();
    RowSample rowSample = new DefaultRowSample(nrRows);
    RootDataMemberships rootMemberships = new RootDataMemberships(rowSample, data, indexManager);
    BitSet firstHalf = new BitSet(nrRows);
    firstHalf.set(0, nrRows / 2);
    DataMemberships firstHalfChildMemberships = rootMemberships.createChildMemberships(firstHalf);
    assertThat(firstHalfChildMemberships, instanceOf(BitSetDescendantDataMemberships.class));
    BitSetDescendantDataMemberships bitSetFirstHalfChildMemberships = (BitSetDescendantDataMemberships) firstHalfChildMemberships;
    assertEquals(firstHalf, bitSetFirstHalfChildMemberships.getBitSet());
    BitSet firstQuarter = new BitSet(nrRows);
    firstQuarter.set(0, nrRows / 4);
    DataMemberships firstQuarterGrandChild = firstHalfChildMemberships.createChildMemberships(firstQuarter);
    assertThat(firstQuarterGrandChild, instanceOf(BitSetDescendantDataMemberships.class));
    BitSetDescendantDataMemberships bitSetFirstQuarterGrandChild = (BitSetDescendantDataMemberships) firstQuarterGrandChild;
    assertEquals(firstQuarter, bitSetFirstQuarterGrandChild.getBitSet());
}
Also used : TreeEnsembleLearnerConfiguration(org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration) DefaultRowSample(org.knime.base.node.mine.treeensemble2.sample.row.DefaultRowSample) BitSet(java.util.BitSet) TreeData(org.knime.base.node.mine.treeensemble2.data.TreeData) DefaultRowSample(org.knime.base.node.mine.treeensemble2.sample.row.DefaultRowSample) RowSample(org.knime.base.node.mine.treeensemble2.sample.row.RowSample) TestDataGenerator(org.knime.base.node.mine.treeensemble2.data.TestDataGenerator) Test(org.junit.Test)

Example 12 with RootDataMemberships

use of org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships in project knime-core by knime.

the class RootDescendantDataMembershipsTest method testGetColumnMemberships.

@Test
public void testGetColumnMemberships() {
    TreeEnsembleLearnerConfiguration config = new TreeEnsembleLearnerConfiguration(false);
    TestDataGenerator dataGen = new TestDataGenerator(config);
    TreeData data = dataGen.createTennisData();
    DefaultDataIndexManager indexManager = new DefaultDataIndexManager(data);
    int nrRows = data.getNrRows();
    RowSample rowSample = new DefaultRowSample(nrRows);
    RootDataMemberships rootMemberships = new RootDataMemberships(rowSample, data, indexManager);
    ColumnMemberships rootColMem = rootMemberships.getColumnMemberships(0);
    assertThat(rootColMem, instanceOf(IntArrayColumnMemberships.class));
    assertEquals(nrRows, rootColMem.size());
    int[] expectedOriginalIndices = new int[] { 0, 1, 7, 8, 10, 2, 6, 11, 12, 3, 4, 5, 9, 13 };
    for (int i = 0; rootColMem.next(); i++) {
        // in this case originalIndex and indexInDataMemberships are the same
        assertEquals(expectedOriginalIndices[i], rootColMem.getIndexInDataMemberships());
        assertEquals(expectedOriginalIndices[i], rootColMem.getIndexInDataMemberships());
        assertEquals(i, rootColMem.getIndexInColumn());
    }
    BitSet lastHalf = new BitSet(nrRows);
    lastHalf.set(nrRows / 2, nrRows);
    DataMemberships lastHalfChild = rootMemberships.createChildMemberships(lastHalf);
    ColumnMemberships childColMem = lastHalfChild.getColumnMemberships(0);
    assertThat(childColMem, instanceOf(DescendantColumnMemberships.class));
    assertEquals(nrRows / 2, childColMem.size());
    expectedOriginalIndices = new int[] { 7, 8, 10, 11, 12, 9, 13 };
    int[] expectedIndexInColumn = new int[] { 2, 3, 4, 7, 8, 12, 13 };
    int[] expectedIndexInDataMemberships = new int[] { 7, 8, 10, 11, 12, 9, 13 };
    for (int i = 0; childColMem.next(); i++) {
        assertEquals(expectedOriginalIndices[i], childColMem.getOriginalIndex());
        assertEquals(expectedIndexInColumn[i], childColMem.getIndexInColumn());
        assertEquals(expectedIndexInDataMemberships[i], childColMem.getIndexInDataMemberships());
    }
}
Also used : TreeEnsembleLearnerConfiguration(org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration) DefaultRowSample(org.knime.base.node.mine.treeensemble2.sample.row.DefaultRowSample) BitSet(java.util.BitSet) TestDataGenerator(org.knime.base.node.mine.treeensemble2.data.TestDataGenerator) TreeData(org.knime.base.node.mine.treeensemble2.data.TreeData) DefaultRowSample(org.knime.base.node.mine.treeensemble2.sample.row.DefaultRowSample) RowSample(org.knime.base.node.mine.treeensemble2.sample.row.RowSample) Test(org.junit.Test)

Example 13 with RootDataMemberships

use of org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships in project knime-core by knime.

the class TreeNominalColumnDataTest method testCalcBestSplitClassificationBinaryTwoClass.

/**
 * Tests the method
 * {@link TreeNominalColumnData#calcBestSplitClassification(DataMemberships, ClassificationPriors, TreeTargetNominalColumnData, RandomData)}
 * in case of a two class problem.
 *
 * @throws Exception
 */
@Test
public void testCalcBestSplitClassificationBinaryTwoClass() throws Exception {
    TreeEnsembleLearnerConfiguration config = createConfig(false);
    config.setMissingValueHandling(MissingValueHandling.Surrogate);
    Pair<TreeNominalColumnData, TreeTargetNominalColumnData> twoClassTennisData = twoClassTennisData(config);
    TreeNominalColumnData columnData = twoClassTennisData.getFirst();
    TreeTargetNominalColumnData targetData = twoClassTennisData.getSecond();
    TreeData twoClassTennisTreeData = twoClassTennisTreeData(config);
    IDataIndexManager indexManager = new DefaultDataIndexManager(twoClassTennisTreeData);
    assertEquals(SplitCriterion.Gini, config.getSplitCriterion());
    double[] rowWeights = new double[TWO_CLASS_INDICES.length];
    Arrays.fill(rowWeights, 1.0);
    // DataMemberships dataMemberships = TestDataGenerator.createMockDataMemberships(TWO_CLASS_INDICES.length);
    DataMemberships dataMemberships = new RootDataMemberships(rowWeights, twoClassTennisTreeData, 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 open office calc
    assertEquals(0.1371428, 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());
    assertFalse(childConditions[0].acceptsMissings());
    assertFalse(childConditions[1].acceptsMissings());
}
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 14 with RootDataMemberships

use of org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships in project knime-core by knime.

the class TreeNominalColumnDataTest method testCalcBestSplitRegressionMultiway.

/**
 * Tests the method
 * {@link TreeNominalColumnData#calcBestSplitRegression(DataMemberships, RegressionPriors, TreeTargetNumericColumnData, RandomData)}
 * using multiway splits.
 *
 * @throws Exception
 */
@Test
public void testCalcBestSplitRegressionMultiway() throws Exception {
    TreeEnsembleLearnerConfiguration config = createConfig(true);
    config.setUseBinaryNominalSplits(false);
    Pair<TreeNominalColumnData, TreeTargetNumericColumnData> tennisDataRegression = tennisDataRegression(config);
    TreeNominalColumnData columnData = tennisDataRegression.getFirst();
    TreeTargetNumericColumnData targetData = tennisDataRegression.getSecond();
    TreeData treeData = createTreeDataRegression(tennisDataRegression);
    double[] rowWeights = new double[SMALL_COLUMN_DATA.length];
    Arrays.fill(rowWeights, 1.0);
    IDataIndexManager indexManager = new DefaultDataIndexManager(treeData);
    DataMemberships dataMemberships = new RootDataMemberships(rowWeights, treeData, indexManager);
    RegressionPriors priors = targetData.getPriors(rowWeights, config);
    SplitCandidate splitCandidate = columnData.calcBestSplitRegression(dataMemberships, priors, targetData, null);
    assertNotNull(splitCandidate);
    assertThat(splitCandidate, instanceOf(NominalMultiwaySplitCandidate.class));
    assertFalse(splitCandidate.canColumnBeSplitFurther());
    assertEquals(36.9643, splitCandidate.getGainValue(), 0.0001);
    NominalMultiwaySplitCandidate multiwaySplitCandidate = (NominalMultiwaySplitCandidate) splitCandidate;
    TreeNodeNominalCondition[] childConditions = multiwaySplitCandidate.getChildConditions();
    assertEquals(3, childConditions.length);
    assertEquals("S", childConditions[0].getValue());
    assertEquals("O", childConditions[1].getValue());
    assertEquals("R", childConditions[2].getValue());
}
Also used : TreeEnsembleLearnerConfiguration(org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration) RootDataMemberships(org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships) TreeNodeNominalCondition(org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalCondition) 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) NominalMultiwaySplitCandidate(org.knime.base.node.mine.treeensemble2.learner.NominalMultiwaySplitCandidate) Test(org.junit.Test)

Example 15 with RootDataMemberships

use of org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships in project knime-core by knime.

the class TreeNominalColumnDataTest method testCalcBestSplitRegressionBinary.

/**
 * Tests the method
 * {@link TreeNominalColumnData#calcBestSplitRegression(DataMemberships, RegressionPriors, TreeTargetNumericColumnData, RandomData)}
 * using binary splits
 *
 * @throws Exception
 */
@Test
public void testCalcBestSplitRegressionBinary() throws Exception {
    TreeEnsembleLearnerConfiguration config = new TreeEnsembleLearnerConfiguration(true);
    Pair<TreeNominalColumnData, TreeTargetNumericColumnData> tennisDataRegression = tennisDataRegression(config);
    TreeNominalColumnData columnData = tennisDataRegression.getFirst();
    TreeTargetNumericColumnData targetData = tennisDataRegression.getSecond();
    TreeData treeData = createTreeDataRegression(tennisDataRegression);
    double[] rowWeights = new double[SMALL_COLUMN_DATA.length];
    Arrays.fill(rowWeights, 1.0);
    IDataIndexManager indexManager = new DefaultDataIndexManager(treeData);
    DataMemberships dataMemberships = new RootDataMemberships(rowWeights, treeData, indexManager);
    RegressionPriors priors = targetData.getPriors(rowWeights, config);
    SplitCandidate splitCandidate = columnData.calcBestSplitRegression(dataMemberships, priors, targetData, null);
    assertNotNull(splitCandidate);
    assertThat(splitCandidate, instanceOf(NominalBinarySplitCandidate.class));
    assertTrue(splitCandidate.canColumnBeSplitFurther());
    assertEquals(32.9143, splitCandidate.getGainValue(), 0.0001);
    NominalBinarySplitCandidate binarySplitCandidate = (NominalBinarySplitCandidate) splitCandidate;
    TreeNodeNominalBinaryCondition[] childConditions = binarySplitCandidate.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());
}
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)

Aggregations

TreeEnsembleLearnerConfiguration (org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration)18 RootDataMemberships (org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships)16 DataMemberships (org.knime.base.node.mine.treeensemble2.data.memberships.DataMemberships)15 Test (org.junit.Test)14 DefaultDataIndexManager (org.knime.base.node.mine.treeensemble2.data.memberships.DefaultDataIndexManager)14 IDataIndexManager (org.knime.base.node.mine.treeensemble2.data.memberships.IDataIndexManager)12 SplitCandidate (org.knime.base.node.mine.treeensemble2.learner.SplitCandidate)12 BitSet (java.util.BitSet)8 NominalBinarySplitCandidate (org.knime.base.node.mine.treeensemble2.learner.NominalBinarySplitCandidate)7 NominalMultiwaySplitCandidate (org.knime.base.node.mine.treeensemble2.learner.NominalMultiwaySplitCandidate)7 RandomData (org.apache.commons.math.random.RandomData)6 NumericMissingSplitCandidate (org.knime.base.node.mine.treeensemble2.learner.NumericMissingSplitCandidate)5 NumericSplitCandidate (org.knime.base.node.mine.treeensemble2.learner.NumericSplitCandidate)5 TreeNodeNominalBinaryCondition (org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalBinaryCondition)5 TreeNodeNumericCondition (org.knime.base.node.mine.treeensemble2.model.TreeNodeNumericCondition)5 TreeData (org.knime.base.node.mine.treeensemble2.data.TreeData)4 RowSample (org.knime.base.node.mine.treeensemble2.sample.row.RowSample)4 TestDataGenerator (org.knime.base.node.mine.treeensemble2.data.TestDataGenerator)2 TreeNodeNominalCondition (org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalCondition)2 TreeNodeSignature (org.knime.base.node.mine.treeensemble2.model.TreeNodeSignature)2