Search in sources :

Example 6 with RootDataMemberships

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

the class TreeNumericColumnDataTest method testCalcBestSplitClassificationSplitAtEnd.

/**
 * Test splits at last possible split position - even if no change in target can be observed, see example data in
 * method body.
 * @throws Exception
 */
@Test
public void testCalcBestSplitClassificationSplitAtEnd() throws Exception {
    // Index:  1 2 3 4 5 6 7 8
    // Value:  1 1|2 2 2|3 3 3
    // Target: A A|A A A|A A B
    double[] data = asDataArray("1,1,2,2,2,3,3,3");
    String[] target = asStringArray("A,A,A,A,A,A,A,B");
    TreeEnsembleLearnerConfiguration config = createConfig();
    RandomData rd = config.createRandomData();
    Pair<TreeOrdinaryNumericColumnData, TreeTargetNominalColumnData> exampleData = exampleData(config, data, target);
    TreeNumericColumnData columnData = exampleData.getFirst();
    TreeTargetNominalColumnData targetData = exampleData.getSecond();
    double[] rowWeights = new double[data.length];
    Arrays.fill(rowWeights, 1.0);
    TreeData treeData = createTreeDataClassification(exampleData);
    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, rd);
    assertNotNull(splitCandidate);
    assertThat(splitCandidate, instanceOf(NumericSplitCandidate.class));
    assertTrue(splitCandidate.canColumnBeSplitFurther());
    // manually calculated
    assertEquals(/*0.21875 - 0.166666667 */
    0.05208, splitCandidate.getGainValue(), 0.001);
    NumericSplitCandidate numSplitCandidate = (NumericSplitCandidate) splitCandidate;
    TreeNodeNumericCondition[] childConditions = numSplitCandidate.getChildConditions();
    assertEquals(2, childConditions.length);
    assertEquals((2.0 + 3.0) / 2.0, childConditions[0].getSplitValue(), 0.0);
    assertEquals(NumericOperator.LessThanOrEqual, childConditions[0].getNumericOperator());
    double[] childRowWeights = new double[data.length];
    System.arraycopy(rowWeights, 0, childRowWeights, 0, rowWeights.length);
    BitSet inChild = columnData.updateChildMemberships(childConditions[0], dataMemberships);
    DataMemberships childMemberships = dataMemberships.createChildMemberships(inChild);
    ClassificationPriors childTargetPriors = targetData.getDistribution(childMemberships, config);
    SplitCandidate splitCandidateChild = columnData.calcBestSplitClassification(childMemberships, childTargetPriors, targetData, rd);
    assertNull(splitCandidateChild);
    System.arraycopy(rowWeights, 0, childRowWeights, 0, rowWeights.length);
    inChild = columnData.updateChildMemberships(childConditions[1], dataMemberships);
    childMemberships = dataMemberships.createChildMemberships(inChild);
    childTargetPriors = targetData.getDistribution(childMemberships, config);
    splitCandidateChild = columnData.calcBestSplitClassification(childMemberships, 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) RandomData(org.apache.commons.math.random.RandomData) TreeNodeNumericCondition(org.knime.base.node.mine.treeensemble2.model.TreeNodeNumericCondition) BitSet(java.util.BitSet) IDataIndexManager(org.knime.base.node.mine.treeensemble2.data.memberships.IDataIndexManager) 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) 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) NumericSplitCandidate(org.knime.base.node.mine.treeensemble2.learner.NumericSplitCandidate) Test(org.junit.Test)

Example 7 with RootDataMemberships

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

the class TreeNumericColumnDataTest method testCalcBestSplitClassificationMissingValStrategy1.

/**
 * This test is outdated and will likely be removed soon.
 *
 * @throws Exception
 */
// @Test
public void testCalcBestSplitClassificationMissingValStrategy1() throws Exception {
    TreeEnsembleLearnerConfiguration config = createConfig();
    final double[] data = asDataArray("1, 2, 3, 4, 5, 6, 7, NaN, NaN, NaN");
    final String[] target = asStringArray("Y, Y, Y, Y, N, N, N, Y, Y, Y");
    Pair<TreeOrdinaryNumericColumnData, TreeTargetNominalColumnData> exampleData = exampleData(config, data, target);
    double[] rowWeights = new double[data.length];
    Arrays.fill(rowWeights, 1.0);
    RandomData rd = config.createRandomData();
    TreeNumericColumnData columnData = exampleData.getFirst();
    TreeTargetNominalColumnData targetData = exampleData.getSecond();
    TreeData treeData = createTreeDataClassification(exampleData);
    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, rd);
    assertNotNull(splitCandidate);
    assertThat(splitCandidate, instanceOf(NumericMissingSplitCandidate.class));
    assertTrue(splitCandidate.canColumnBeSplitFurther());
    assertEquals(0.42, splitCandidate.getGainValue(), 0.0001);
    TreeNodeNumericCondition[] childConditions = ((NumericMissingSplitCandidate) splitCandidate).getChildConditions();
    assertEquals(2, childConditions.length);
    assertEquals(NumericOperator.LessThanOrEqualOrMissing, childConditions[0].getNumericOperator());
    assertEquals(NumericOperator.LargerThan, childConditions[1].getNumericOperator());
    assertEquals(4.5, childConditions[0].getSplitValue(), 0.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) IDataIndexManager(org.knime.base.node.mine.treeensemble2.data.memberships.IDataIndexManager) 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) NumericMissingSplitCandidate(org.knime.base.node.mine.treeensemble2.learner.NumericMissingSplitCandidate) 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)

Example 8 with RootDataMemberships

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

the class TreeNumericColumnDataTest method testCalcBestSplitClassificationSplitAtStart.

/**
 * Test splits at last possible split position - even if no change in target can be observed, see example data in
 * method body.
 * @throws Exception
 */
@Test
public void testCalcBestSplitClassificationSplitAtStart() throws Exception {
    // Index:  1 2 3 4 5 6 7
    // Value:  1 1 1|2 2|3 3
    // Target: A A A|A A|A B
    double[] data = asDataArray("1,1,1,2,2,3,3");
    String[] target = asStringArray("A,A,A,A,B,A,B");
    TreeEnsembleLearnerConfiguration config = createConfig();
    Pair<TreeOrdinaryNumericColumnData, TreeTargetNominalColumnData> exampleData = exampleData(config, data, target);
    TreeNumericColumnData columnData = exampleData.getFirst();
    TreeTargetNominalColumnData targetData = exampleData.getSecond();
    double[] rowWeights = new double[data.length];
    Arrays.fill(rowWeights, 1.0);
    TreeData treeData = createTreeDataClassification(exampleData);
    IDataIndexManager indexManager = new DefaultDataIndexManager(treeData);
    DataMemberships dataMemberships = new RootDataMemberships(rowWeights, treeData, indexManager);
    ClassificationPriors priors = targetData.getDistribution(rowWeights, config);
    RandomData rd = config.createRandomData();
    SplitCandidate splitCandidate = columnData.calcBestSplitClassification(dataMemberships, priors, targetData, rd);
    double gain = (1.0 - Math.pow(5.0 / 7.0, 2.0) - Math.pow(2.0 / 7.0, 2.0)) - 0.0 - 4.0 / 7.0 * (1.0 - Math.pow(2.0 / 4.0, 2.0) - Math.pow(2.0 / 4.0, 2.0));
    // manually calculated
    assertEquals(gain, splitCandidate.getGainValue(), 0.000001);
    NumericSplitCandidate numSplitCandidate = (NumericSplitCandidate) splitCandidate;
    TreeNodeNumericCondition[] childConditions = numSplitCandidate.getChildConditions();
    assertEquals(2, childConditions.length);
    assertEquals((1.0 + 2.0) / 2.0, childConditions[0].getSplitValue(), 0.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) IDataIndexManager(org.knime.base.node.mine.treeensemble2.data.memberships.IDataIndexManager) 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) 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) NumericSplitCandidate(org.knime.base.node.mine.treeensemble2.learner.NumericSplitCandidate) Test(org.junit.Test)

Example 9 with RootDataMemberships

use of org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships 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 10 with RootDataMemberships

use of org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships 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)

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