use of org.knime.base.node.mine.treeensemble2.learner.SplitCandidate in project knime-core by knime.
the class Surrogates method createSurrogateSplitWithDefaultDirection.
/**
* Creates a surrogate split that only contains the best split and the default (majority) direction. It does
* <b>NOT</b> calculate any surrogate splits (and is therefore more efficient).
*
* @param dataMemberships
* @param bestSplit
* @return SurrogateSplit with conditions for both children. The conditions only contain the condition for the best
* split and the default condition (true for the child the most records go to and false for the other one).
*/
public static SurrogateSplit createSurrogateSplitWithDefaultDirection(final DataMemberships dataMemberships, final SplitCandidate bestSplit) {
TreeAttributeColumnData col = bestSplit.getColumnData();
TreeNodeCondition[] conditions = bestSplit.getChildConditions();
// get child marker for best split
BitSet left = col.updateChildMemberships(conditions[0], dataMemberships);
BitSet right = col.updateChildMemberships(conditions[1], dataMemberships);
// decide which child the majority of the records goes to
boolean majorityGoesLeft = left.cardinality() < right.cardinality() ? false : true;
// create surrogate conditions
TreeNodeSurrogateOnlyDefDirCondition condLeft = new TreeNodeSurrogateOnlyDefDirCondition((TreeNodeColumnCondition) conditions[0], majorityGoesLeft);
TreeNodeSurrogateOnlyDefDirCondition condRight = new TreeNodeSurrogateOnlyDefDirCondition((TreeNodeColumnCondition) conditions[1], !majorityGoesLeft);
BitSet[] childMarkers = new BitSet[] { left, right };
fillInMissingChildMarkersWithDefault(bestSplit, childMarkers, majorityGoesLeft);
return new SurrogateSplit(new AbstractTreeNodeSurrogateCondition[] { condLeft, condRight }, new BitSet[] { left, right });
}
use of org.knime.base.node.mine.treeensemble2.learner.SplitCandidate in project knime-core by knime.
the class TreeLearnerClassification method findBestSplitClassification.
private SplitCandidate findBestSplitClassification(final int currentDepth, final DataMemberships dataMemberships, final ColumnSample columnSample, final TreeNodeSignature treeNodeSignature, final ClassificationPriors targetPriors, final BitSet forbiddenColumnSet) {
final TreeData data = getData();
final RandomData rd = getRandomData();
// final ColumnSampleStrategy colSamplingStrategy = getColSamplingStrategy();
final TreeEnsembleLearnerConfiguration config = getConfig();
final int maxLevels = config.getMaxLevels();
if (maxLevels != TreeEnsembleLearnerConfiguration.MAX_LEVEL_INFINITE && currentDepth >= maxLevels) {
return null;
}
final int minNodeSize = config.getMinNodeSize();
if (minNodeSize != TreeEnsembleLearnerConfiguration.MIN_NODE_SIZE_UNDEFINED) {
if (targetPriors.getNrRecords() < minNodeSize) {
return null;
}
}
final double priorImpurity = targetPriors.getPriorImpurity();
if (priorImpurity < TreeColumnData.EPSILON) {
return null;
}
final TreeTargetNominalColumnData targetColumn = (TreeTargetNominalColumnData) data.getTargetColumn();
SplitCandidate splitCandidate = null;
if (currentDepth == 0 && config.getHardCodedRootColumn() != null) {
final TreeAttributeColumnData rootColumn = data.getColumn(config.getHardCodedRootColumn());
// TODO discuss whether this option makes sense with surrogates
return rootColumn.calcBestSplitClassification(dataMemberships, targetPriors, targetColumn, rd);
}
double bestGainValue = 0.0;
for (TreeAttributeColumnData col : columnSample) {
if (forbiddenColumnSet.get(col.getMetaData().getAttributeIndex())) {
continue;
}
final SplitCandidate currentColSplit = col.calcBestSplitClassification(dataMemberships, targetPriors, targetColumn, rd);
if (currentColSplit != null) {
final double currentGain = currentColSplit.getGainValue();
final boolean tiebreaker = currentGain == bestGainValue ? (rd.nextInt(0, 1) == 0) : false;
if (currentColSplit.getGainValue() > bestGainValue || tiebreaker) {
splitCandidate = currentColSplit;
bestGainValue = currentGain;
}
}
}
return splitCandidate;
}
use of org.knime.base.node.mine.treeensemble2.learner.SplitCandidate in project knime-core by knime.
the class TreeLearnerClassification method findBestSplitsClassification.
/**
* Returns a list of SplitCandidates sorted (descending) by their gain
*
* @param currentDepth
* @param rowSampleWeights
* @param treeNodeSignature
* @param targetPriors
* @param forbiddenColumnSet
* @param membershipController
* @return
*/
private SplitCandidate[] findBestSplitsClassification(final int currentDepth, final DataMemberships dataMemberships, final ColumnSample columnSample, final TreeNodeSignature treeNodeSignature, final ClassificationPriors targetPriors, final BitSet forbiddenColumnSet) {
final TreeData data = getData();
final RandomData rd = getRandomData();
// final ColumnSampleStrategy colSamplingStrategy = getColSamplingStrategy();
final TreeEnsembleLearnerConfiguration config = getConfig();
final int maxLevels = config.getMaxLevels();
if (maxLevels != TreeEnsembleLearnerConfiguration.MAX_LEVEL_INFINITE && currentDepth >= maxLevels) {
return null;
}
final int minNodeSize = config.getMinNodeSize();
if (minNodeSize != TreeEnsembleLearnerConfiguration.MIN_NODE_SIZE_UNDEFINED) {
if (targetPriors.getNrRecords() < minNodeSize) {
return null;
}
}
final double priorImpurity = targetPriors.getPriorImpurity();
if (priorImpurity < TreeColumnData.EPSILON) {
return null;
}
final TreeTargetNominalColumnData targetColumn = (TreeTargetNominalColumnData) data.getTargetColumn();
SplitCandidate splitCandidate = null;
if (currentDepth == 0 && config.getHardCodedRootColumn() != null) {
final TreeAttributeColumnData rootColumn = data.getColumn(config.getHardCodedRootColumn());
// TODO discuss whether this option makes sense with surrogates
return new SplitCandidate[] { rootColumn.calcBestSplitClassification(dataMemberships, targetPriors, targetColumn, rd) };
}
double bestGainValue = 0.0;
final Comparator<SplitCandidate> comp = new Comparator<SplitCandidate>() {
@Override
public int compare(final SplitCandidate o1, final SplitCandidate o2) {
int compareDouble = -Double.compare(o1.getGainValue(), o2.getGainValue());
return compareDouble;
}
};
ArrayList<SplitCandidate> candidates = new ArrayList<SplitCandidate>(columnSample.getNumCols());
for (TreeAttributeColumnData col : columnSample) {
if (forbiddenColumnSet.get(col.getMetaData().getAttributeIndex())) {
continue;
}
SplitCandidate currentColSplit = col.calcBestSplitClassification(dataMemberships, targetPriors, targetColumn, rd);
if (currentColSplit != null) {
candidates.add(currentColSplit);
}
}
if (candidates.isEmpty()) {
return null;
}
candidates.sort(comp);
return candidates.toArray(new SplitCandidate[candidates.size()]);
}
use of org.knime.base.node.mine.treeensemble2.learner.SplitCandidate 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);
}
use of org.knime.base.node.mine.treeensemble2.learner.SplitCandidate 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);
}
Aggregations