Search in sources :

Example 1 with TreeNodeNumericCondition

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

the class TreeNumericColumnData method updateChildMemberships.

@Override
public BitSet updateChildMemberships(final TreeNodeCondition childCondition, final DataMemberships parentMemberships) {
    final TreeNodeNumericCondition numCondition = (TreeNodeNumericCondition) childCondition;
    final NumericOperator numOperator = numCondition.getNumericOperator();
    final double splitValue = numCondition.getSplitValue();
    final ColumnMemberships columnMemberships = parentMemberships.getColumnMemberships(getMetaData().getAttributeIndex());
    columnMemberships.reset();
    final BitSet inChild = new BitSet(columnMemberships.size());
    int startIndex = 0;
    // }
    if (!columnMemberships.nextIndexFrom(startIndex)) {
        throw new IllegalStateException("The current columnMemberships object contains no element that satisfies the splitcondition");
    }
    final int lengthNonMissing = getLengthNonMissing();
    do {
        final double value = getSorted(columnMemberships.getIndexInColumn());
        boolean matches;
        switch(numOperator) {
            case LessThanOrEqual:
                matches = value <= splitValue;
                break;
            case LargerThan:
                matches = value > splitValue;
                break;
            case LessThanOrEqualOrMissing:
                matches = Double.isNaN(value) ? true : value <= splitValue;
                break;
            case LargerThanOrMissing:
                matches = Double.isNaN(value) ? true : value > splitValue;
                break;
            default:
                throw new IllegalStateException("Unknown operator " + numOperator);
        }
        if (matches) {
            inChild.set(columnMemberships.getIndexInDataMemberships());
        }
    } while (columnMemberships.next() && columnMemberships.getIndexInColumn() < lengthNonMissing);
    // reached end of columnMemberships
    if (columnMemberships.getIndexInColumn() < lengthNonMissing) {
        return inChild;
    }
    // handle missing values
    if (numOperator.equals(NumericOperator.LessThanOrEqualOrMissing) || numOperator.equals(NumericOperator.LargerThanOrMissing) || numCondition.acceptsMissings()) {
        do {
            inChild.set(columnMemberships.getIndexInDataMemberships());
        } while (columnMemberships.next());
    }
    return inChild;
}
Also used : TreeNodeNumericCondition(org.knime.base.node.mine.treeensemble2.model.TreeNodeNumericCondition) BitSet(java.util.BitSet) ColumnMemberships(org.knime.base.node.mine.treeensemble2.data.memberships.ColumnMemberships) NumericOperator(org.knime.base.node.mine.treeensemble2.model.TreeNodeNumericCondition.NumericOperator)

Example 2 with TreeNodeNumericCondition

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

the class TreeNumericColumnDataTest method testCalcBestSplitClassification.

@Test
public void testCalcBestSplitClassification() throws Exception {
    TreeEnsembleLearnerConfiguration config = createConfig();
    /* data from J. Fuernkranz, Uni Darmstadt:
         * http://www.ke.tu-darmstadt.de/lehre/archiv/ws0809/mldm/dt.pdf */
    final double[] data = asDataArray("60,70,75,85, 90, 95, 100,120,125,220");
    final String[] target = asStringArray("No,No,No,Yes,Yes,Yes,No, No, No, No");
    Pair<TreeOrdinaryNumericColumnData, TreeTargetNominalColumnData> exampleData = exampleData(config, data, target);
    RandomData rd = config.createRandomData();
    TreeNumericColumnData columnData = exampleData.getFirst();
    TreeTargetNominalColumnData targetData = exampleData.getSecond();
    assertEquals(SplitCriterion.Gini, config.getSplitCriterion());
    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());
    // libre office calc
    assertEquals(/*0.42 - 0.300 */
    0.12, splitCandidate.getGainValue(), 0.00001);
    NumericSplitCandidate numSplitCandidate = (NumericSplitCandidate) splitCandidate;
    TreeNodeNumericCondition[] childConditions = numSplitCandidate.getChildConditions();
    assertEquals(2, childConditions.length);
    assertEquals((95.0 + 100.0) / 2.0, childConditions[0].getSplitValue(), 0.0);
    assertEquals((95.0 + 100.0) / 2.0, childConditions[1].getSplitValue(), 0.0);
    assertEquals(NumericOperator.LessThanOrEqual, childConditions[0].getNumericOperator());
    assertEquals(NumericOperator.LargerThan, childConditions[1].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);
    assertNotNull(splitCandidateChild);
    assertThat(splitCandidateChild, instanceOf(NumericSplitCandidate.class));
    // manually via libre office calc
    assertEquals(0.5, splitCandidateChild.getGainValue(), 0.00001);
    TreeNodeNumericCondition[] childConditions2 = ((NumericSplitCandidate) splitCandidateChild).getChildConditions();
    assertEquals(2, childConditions2.length);
    assertEquals((75.0 + 85.0) / 2.0, childConditions2[0].getSplitValue(), 0.0);
    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, rd);
    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 3 with TreeNodeNumericCondition

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

the class TreeNumericColumnDataTest method testUpdateChildMemberships.

/**
 * Tests the {@link TreeNumericColumnData#updateChildMemberships(TreeNodeCondition, DataMemberships)} methods with
 * different conditions including missing values.
 *
 * @throws Exception
 */
@Test
public void testUpdateChildMemberships() throws Exception {
    final TreeEnsembleLearnerConfiguration config = createConfig();
    final TestDataGenerator dataGen = new TestDataGenerator(config);
    final int[] indices = new int[] { 0, 1, 2, 3, 4, 5, 6 };
    final double[] weights = new double[7];
    Arrays.fill(weights, 1.0);
    final DataMemberships dataMem = new MockDataColMem(indices, indices, weights);
    final String noMissingsCSV = "-50, -3, -2, 2, 25, 100, 101";
    final TreeNumericColumnData col = dataGen.createNumericAttributeColumn(noMissingsCSV, "noMissings-col", 0);
    // less than or equals
    TreeNodeNumericCondition numCond = new TreeNodeNumericCondition(col.getMetaData(), -2, NumericOperator.LessThanOrEqual, false);
    BitSet inChild = col.updateChildMemberships(numCond, dataMem);
    BitSet expected = new BitSet(3);
    expected.set(0, 3);
    assertEquals("The produced BitSet is incorrect.", expected, inChild);
    // greater than
    numCond = new TreeNodeNumericCondition(col.getMetaData(), 10, NumericOperator.LargerThan, false);
    inChild = col.updateChildMemberships(numCond, dataMem);
    expected.clear();
    expected.set(4, 7);
    assertEquals("The produced BitSet is incorrect", expected, inChild);
    // with missing values
    final String missingsCSV = "-2, 0, 1, 43, 61, 66, NaN";
    final TreeNumericColumnData colWithMissings = dataGen.createNumericAttributeColumn(missingsCSV, "missings-col", 0);
    // less than or equal or missing
    numCond = new TreeNodeNumericCondition(colWithMissings.getMetaData(), 12, NumericOperator.LessThanOrEqual, true);
    inChild = colWithMissings.updateChildMemberships(numCond, dataMem);
    expected.clear();
    expected.set(0, 3);
    expected.set(6);
    assertEquals("The produced BitSet is incorrect", expected, inChild);
    // less than or equals not missing
    numCond = new TreeNodeNumericCondition(colWithMissings.getMetaData(), 12, NumericOperator.LessThanOrEqual, false);
    inChild = colWithMissings.updateChildMemberships(numCond, dataMem);
    expected.clear();
    expected.set(0, 3);
    assertEquals("The produced BitSet is incorrect", expected, inChild);
    // larger than or missing
    numCond = new TreeNodeNumericCondition(colWithMissings.getMetaData(), 43, NumericOperator.LargerThan, true);
    inChild = colWithMissings.updateChildMemberships(numCond, dataMem);
    expected.clear();
    expected.set(4, 7);
    assertEquals("The produced BitSet is incorrect", expected, inChild);
    // larger than not missing
    numCond = new TreeNodeNumericCondition(colWithMissings.getMetaData(), 12, NumericOperator.LargerThan, false);
    inChild = colWithMissings.updateChildMemberships(numCond, dataMem);
    expected.clear();
    expected.set(3, 6);
    assertEquals("The produced BitSet is incorrect", expected, inChild);
}
Also used : TreeEnsembleLearnerConfiguration(org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration) TreeNodeNumericCondition(org.knime.base.node.mine.treeensemble2.model.TreeNodeNumericCondition) 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)

Example 4 with TreeNodeNumericCondition

use of org.knime.base.node.mine.treeensemble2.model.TreeNodeNumericCondition 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 5 with TreeNodeNumericCondition

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

Aggregations

TreeNodeNumericCondition (org.knime.base.node.mine.treeensemble2.model.TreeNodeNumericCondition)9 TreeEnsembleLearnerConfiguration (org.knime.base.node.mine.treeensemble2.node.learner.TreeEnsembleLearnerConfiguration)9 Test (org.junit.Test)8 RandomData (org.apache.commons.math.random.RandomData)6 DataMemberships (org.knime.base.node.mine.treeensemble2.data.memberships.DataMemberships)6 RootDataMemberships (org.knime.base.node.mine.treeensemble2.data.memberships.RootDataMemberships)6 NumericMissingSplitCandidate (org.knime.base.node.mine.treeensemble2.learner.NumericMissingSplitCandidate)6 NumericSplitCandidate (org.knime.base.node.mine.treeensemble2.learner.NumericSplitCandidate)6 SplitCandidate (org.knime.base.node.mine.treeensemble2.learner.SplitCandidate)6 BitSet (java.util.BitSet)5 DefaultDataIndexManager (org.knime.base.node.mine.treeensemble2.data.memberships.DefaultDataIndexManager)5 IDataIndexManager (org.knime.base.node.mine.treeensemble2.data.memberships.IDataIndexManager)4 TestDataGenerator (org.knime.base.node.mine.treeensemble2.data.TestDataGenerator)2 TreeNumericColumnData (org.knime.base.node.mine.treeensemble2.data.TreeNumericColumnData)2 TreeNodeCondition (org.knime.base.node.mine.treeensemble2.model.TreeNodeCondition)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 PredictorRecord (org.knime.base.node.mine.treeensemble2.data.PredictorRecord)1 TreeNumericColumnMetaData (org.knime.base.node.mine.treeensemble2.data.TreeNumericColumnMetaData)1