Search in sources :

Example 31 with TreeEnsembleLearnerConfiguration

use of org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration in project knime-core by knime.

the class TreeNumericColumnDataTest method testCalcBestSplitRegression.

@Test
public void testCalcBestSplitRegression() throws InvalidSettingsException {
    String dataCSV = "1,2,3,4,5,6,7,8,9,10";
    String targetCSV = "1,5,4,4.3,6.5,6.5,4,3,3,4";
    TreeEnsembleLearnerConfiguration config = new TreeEnsembleLearnerConfiguration(true);
    config.setNrModels(1);
    config.setDataSelectionWithReplacement(false);
    config.setUseDifferentAttributesAtEachNode(false);
    config.setDataFractionPerTree(1.0);
    config.setColumnSamplingMode(ColumnSamplingMode.None);
    TestDataGenerator dataGen = new TestDataGenerator(config);
    RandomData rd = config.createRandomData();
    TreeTargetNumericColumnData target = TestDataGenerator.createNumericTargetColumn(targetCSV);
    TreeNumericColumnData attribute = dataGen.createNumericAttributeColumn(dataCSV, "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));
    SplitCandidate firstSplit = attribute.calcBestSplitRegression(rootMem, target.getPriors(rootMem, config), target, rd);
    // calculated via OpenOffice calc
    assertEquals(10.885444, firstSplit.getGainValue(), 1e-5);
    TreeNodeCondition[] firstConditions = firstSplit.getChildConditions();
    assertEquals(2, firstConditions.length);
    for (int i = 0; i < firstConditions.length; i++) {
        assertThat(firstConditions[i], instanceOf(TreeNodeNumericCondition.class));
        TreeNodeNumericCondition numCond = (TreeNodeNumericCondition) firstConditions[i];
        assertEquals(1.5, numCond.getSplitValue(), 0);
    }
    // left child contains only one row therefore only look at right child
    BitSet expectedInChild = new BitSet(10);
    expectedInChild.set(1, 10);
    BitSet inChild = attribute.updateChildMemberships(firstConditions[1], rootMem);
    assertEquals(expectedInChild, inChild);
    DataMemberships childMem = rootMem.createChildMemberships(inChild);
    SplitCandidate secondSplit = attribute.calcBestSplitRegression(childMem, target.getPriors(childMem, config), target, rd);
    assertEquals(6.883555, secondSplit.getGainValue(), 1e-5);
    TreeNodeCondition[] secondConditions = secondSplit.getChildConditions();
    for (int i = 0; i < secondConditions.length; i++) {
        assertThat(secondConditions[i], instanceOf(TreeNodeNumericCondition.class));
        TreeNodeNumericCondition numCond = (TreeNodeNumericCondition) secondConditions[i];
        assertEquals(6.5, numCond.getSplitValue(), 0);
    }
}
Also used : TreeEnsembleLearnerConfiguration(org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration) RootDataMemberships(org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships) RandomData(org.apache.commons.math.random.RandomData) TreeNodeNumericCondition(org.knime.base.node.mine.treeensemble2.model.TreeNodeNumericCondition) BitSet(java.util.BitSet) NumericSplitCandidate(org.knime.base.node.mine.treeensemble2.learner.NumericSplitCandidate) SplitCandidate(org.knime.base.node.mine.treeensemble2.learner.SplitCandidate) NumericMissingSplitCandidate(org.knime.base.node.mine.treeensemble2.learner.NumericMissingSplitCandidate) DataMemberships(org.knime.base.node.mine.treeensemble2.data.memberships.DataMemberships) RootDataMemberships(org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships) DefaultDataIndexManager(org.knime.base.node.mine.treeensemble2.data.memberships.DefaultDataIndexManager) TreeNodeCondition(org.knime.base.node.mine.treeensemble2.model.TreeNodeCondition) Test(org.junit.Test)

Example 32 with TreeEnsembleLearnerConfiguration

use of org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration in project knime-core by knime.

the class TreeNumericColumnDataTest method createConfig.

private static TreeEnsembleLearnerConfiguration createConfig() throws InvalidSettingsException {
    TreeEnsembleLearnerConfiguration config = new TreeEnsembleLearnerConfiguration(false);
    config.setColumnSamplingMode(ColumnSamplingMode.None);
    config.setSplitCriterion(SplitCriterion.Gini);
    config.setNrModels(1);
    config.setDataSelectionWithReplacement(false);
    config.setUseDifferentAttributesAtEachNode(false);
    config.setDataFractionPerTree(1.0);
    return config;
}
Also used : TreeEnsembleLearnerConfiguration(org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration)

Example 33 with TreeEnsembleLearnerConfiguration

use of org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration 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 34 with TreeEnsembleLearnerConfiguration

use of org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration 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 35 with TreeEnsembleLearnerConfiguration

use of org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration 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)

Aggregations

TreeEnsembleLearnerConfiguration (org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration)62 Test (org.junit.Test)29 DataMemberships (org.knime.base.node.mine.treeensemble2.data.memberships.DataMemberships)27 RootDataMemberships (org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships)26 SplitCandidate (org.knime.base.node.mine.treeensemble2.learner.SplitCandidate)19 RandomData (org.apache.commons.math.random.RandomData)17 BitSet (java.util.BitSet)16 DefaultDataIndexManager (org.knime.base.node.mine.treeensemble2.data.memberships.DefaultDataIndexManager)15 NominalBinarySplitCandidate (org.knime.base.node.mine.treeensemble2.learner.NominalBinarySplitCandidate)15 IDataIndexManager (org.knime.base.node.mine.treeensemble2.data.memberships.IDataIndexManager)13 NominalMultiwaySplitCandidate (org.knime.base.node.mine.treeensemble2.learner.NominalMultiwaySplitCandidate)13 TreeData (org.knime.base.node.mine.treeensemble2.data.TreeData)10 TreeNodeNominalBinaryCondition (org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalBinaryCondition)10 TestDataGenerator (org.knime.base.node.mine.treeensemble2.data.TestDataGenerator)9 TreeAttributeColumnData (org.knime.base.node.mine.treeensemble2.data.TreeAttributeColumnData)8 NumericSplitCandidate (org.knime.base.node.mine.treeensemble2.learner.NumericSplitCandidate)8 TreeNodeNumericCondition (org.knime.base.node.mine.treeensemble2.model.TreeNodeNumericCondition)7 NumericMissingSplitCandidate (org.knime.base.node.mine.treeensemble2.learner.NumericMissingSplitCandidate)6 TreeNodeNominalCondition (org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalCondition)6 TreeTargetNominalColumnData (org.knime.base.node.mine.treeensemble2.data.TreeTargetNominalColumnData)5