use of org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalBinaryCondition in project knime-core by knime.
the class LiteralConditionParser method handleSimpleSetPredicate.
private TreeNodeColumnCondition handleSimpleSetPredicate(final SimpleSetPredicate simpleSetPred, final boolean acceptsMissings) {
String field = simpleSetPred.getField();
CheckUtils.checkArgument(m_metaDataMapper.isNominal(field), "The field \"%s\" is not nominal but currently only nominal fields can be used for SimpleSetPredicates", field);
NominalAttributeColumnHelper colHelper = m_metaDataMapper.getNominalColumnHelper(field);
TreeNominalColumnMetaData metaData = colHelper.getMetaData();
boolean isInSet = simpleSetPred.getBooleanOperator().equals(SimpleSetPredicate.BooleanOperator.IS_IN);
return new TreeNodeNominalBinaryCondition(metaData, parseValuesMask(simpleSetPred, colHelper), isInSet, acceptsMissings);
}
use of org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalBinaryCondition in project knime-core by knime.
the class TreeNominalColumnData method updateChildMembershipsBinary.
private BitSet updateChildMembershipsBinary(final TreeNodeNominalBinaryCondition childBinaryCondition, final DataMemberships parentMemberships) {
ColumnMemberships columnMemberships = parentMemberships.getColumnMemberships(getMetaData().getAttributeIndex());
columnMemberships.reset();
BitSet inChild = new BitSet(columnMemberships.size());
// TODO Check if this can be done more efficiently
NominalValueRepresentation[] reps = getMetaData().getValues();
int start = 0;
boolean reachedEnd = false;
final int lengthNonMissing = containsMissingValues() ? reps.length - 1 : reps.length;
for (int att = 0; att < lengthNonMissing; att++) {
if (childBinaryCondition.testCondition(att)) {
// move columnMemberships to correct position
if (!columnMemberships.nextIndexFrom(start)) {
// reached end of columnMemberships
break;
}
int end = start + m_nominalValueCounts[att];
for (int index = columnMemberships.getIndexInColumn(); index < end; index = columnMemberships.getIndexInColumn()) {
inChild.set(columnMemberships.getIndexInDataMemberships());
if (!columnMemberships.next()) {
reachedEnd = true;
break;
}
}
}
start += m_nominalValueCounts[att];
}
if (!reachedEnd && containsMissingValues() && childBinaryCondition.acceptsMissings()) {
if (columnMemberships.nextIndexFrom(start)) {
do {
inChild.set(columnMemberships.getIndexInDataMemberships());
} while (columnMemberships.next());
}
}
return inChild;
}
use of org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalBinaryCondition in project knime-core by knime.
the class TreeNominalColumnDataTest method testCalcBestSplitRegressionBinaryXGBoostMissingValueHandling.
/**
* Tests the XGBoost missing value handling in case of a regression with binary splits.
*
* @throws Exception
*/
@Test
public void testCalcBestSplitRegressionBinaryXGBoostMissingValueHandling() throws Exception {
final TreeEnsembleLearnerConfiguration config = createConfig(true);
config.setMissingValueHandling(MissingValueHandling.XGBoost);
final TestDataGenerator dataGen = new TestDataGenerator(config);
final String noMissingCSV = "A, A, A, B, B, B, B, C, C";
final String noMissingsTarget = "1, 2, 2, 7, 6, 5, 2, 3, 1";
TreeNominalColumnData dataCol = dataGen.createNominalAttributeColumn(noMissingCSV, "noMissings", 0);
TreeTargetNumericColumnData targetCol = TestDataGenerator.createNumericTargetColumn(noMissingsTarget);
double[] weights = new double[9];
Arrays.fill(weights, 1.0);
int[] indices = new int[9];
for (int i = 0; i < indices.length; i++) {
indices[i] = i;
}
final RandomData rd = config.createRandomData();
DataMemberships dataMemberships = new MockDataColMem(indices, indices, weights);
// first test the case that there are no missing values during training (we still need to provide a missing value direction for prediction)
SplitCandidate split = dataCol.calcBestSplitRegression(dataMemberships, targetCol.getPriors(weights, config), targetCol, rd);
assertNotNull("SplitCandidate may not be null", split);
assertThat(split, instanceOf(NominalBinarySplitCandidate.class));
assertEquals("Wrong gain.", 22.755555, split.getGainValue(), 1e-5);
assertTrue("No missing values in dataCol therefore the missedRows BitSet must be empty.", split.getMissedRows().isEmpty());
NominalBinarySplitCandidate nomSplit = (NominalBinarySplitCandidate) split;
TreeNodeNominalBinaryCondition[] conditions = nomSplit.getChildConditions();
assertEquals("Binary split candidate must have two children.", 2, conditions.length);
final String[] values = new String[] { "A", "C" };
assertArrayEquals("Wrong values in split condition.", values, conditions[0].getValues());
assertArrayEquals("Wrong values in split condition.", values, conditions[1].getValues());
assertFalse("Missings should go with majority", conditions[0].acceptsMissings());
assertTrue("Missings should go with majority", conditions[1].acceptsMissings());
assertEquals("Wrong set logic.", SetLogic.IS_NOT_IN, conditions[0].getSetLogic());
assertEquals("Wrong set logic.", SetLogic.IS_IN, conditions[1].getSetLogic());
// test the case that there are missing values during training
final String missingCSV = "A, A, A, B, B, B, B, C, C, ?";
final String missingTarget = "1, 2, 2, 7, 6, 5, 2, 3, 1, 8";
dataCol = dataGen.createNominalAttributeColumn(missingCSV, "missing", 0);
targetCol = TestDataGenerator.createNumericTargetColumn(missingTarget);
weights = new double[10];
Arrays.fill(weights, 1.0);
indices = new int[10];
for (int i = 0; i < indices.length; i++) {
indices[i] = i;
}
dataMemberships = new MockDataColMem(indices, indices, weights);
split = dataCol.calcBestSplitRegression(dataMemberships, targetCol.getPriors(weights, config), targetCol, rd);
assertNotNull("SplitCandidate may not be null.", split);
assertThat(split, instanceOf(NominalBinarySplitCandidate.class));
assertEquals("Wrong gain.", 36.1, split.getGainValue(), 1e-5);
assertTrue("Conditions should handle missing values therefore the missedRows BitSet must be empty.", split.getMissedRows().isEmpty());
nomSplit = (NominalBinarySplitCandidate) split;
conditions = nomSplit.getChildConditions();
assertEquals("Binary split candidate must have two children.", 2, conditions.length);
assertArrayEquals("Wrong values in split condition.", values, conditions[0].getValues());
assertArrayEquals("Wrong values in split condition.", values, conditions[1].getValues());
assertTrue("Missings should go with B (because there target values are similar)", conditions[0].acceptsMissings());
assertFalse("Missings should go with B (because there target values are similar)", conditions[1].acceptsMissings());
assertEquals("Wrong set logic.", SetLogic.IS_NOT_IN, conditions[0].getSetLogic());
assertEquals("Wrong set logic.", SetLogic.IS_IN, conditions[1].getSetLogic());
}
use of org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalBinaryCondition in project knime-core by knime.
the class TreeNominalColumnDataTest method testCalcBestSplitCassificationBinaryTwoClassXGBoostMissingValue.
/**
* Tests the XGBoost Missing value handling in case of a two class problem <br>
* currently not tested because missing value handling will probably be implemented differently.
*
* @throws Exception
*/
// @Test
public void testCalcBestSplitCassificationBinaryTwoClassXGBoostMissingValue() throws Exception {
final TreeEnsembleLearnerConfiguration config = createConfig(false);
config.setMissingValueHandling(MissingValueHandling.XGBoost);
final TestDataGenerator dataGen = new TestDataGenerator(config);
// check correct behavior if no missing values are encountered during split search
Pair<TreeNominalColumnData, TreeTargetNominalColumnData> twoClassTennisData = twoClassTennisData(config);
TreeData treeData = dataGen.createTreeData(twoClassTennisData.getSecond(), twoClassTennisData.getFirst());
IDataIndexManager indexManager = new DefaultDataIndexManager(treeData);
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, treeData, indexManager);
TreeTargetNominalColumnData targetData = twoClassTennisData.getSecond();
TreeNominalColumnData columnData = twoClassTennisData.getFirst();
ClassificationPriors priors = targetData.getDistribution(rowWeights, config);
RandomData rd = TestDataGenerator.createRandomData();
SplitCandidate splitCandidate = columnData.calcBestSplitClassification(dataMemberships, priors, targetData, rd);
assertNotNull(splitCandidate);
assertThat(splitCandidate, instanceOf(NominalBinarySplitCandidate.class));
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());
// check if missing values go left
assertTrue(childConditions[0].acceptsMissings());
assertFalse(childConditions[1].acceptsMissings());
// check correct behavior if missing values are encountered during split search
String dataContainingMissingsCSV = "S,?,O,R,S,R,S,O,O,?";
columnData = dataGen.createNominalAttributeColumn(dataContainingMissingsCSV, "column containing missing values", 0);
treeData = dataGen.createTreeData(targetData, columnData);
indexManager = new DefaultDataIndexManager(treeData);
dataMemberships = new RootDataMemberships(rowWeights, treeData, indexManager);
splitCandidate = columnData.calcBestSplitClassification(dataMemberships, priors, targetData, null);
assertNotNull(splitCandidate);
binarySplitCandidate = (NominalBinarySplitCandidate) splitCandidate;
assertEquals("Gain was not as expected", 0.08, binarySplitCandidate.getGainValue(), 1e-8);
childConditions = binarySplitCandidate.getChildConditions();
String[] conditionValues = new String[] { "O", "?" };
assertArrayEquals("Values in nominal condition did not match", conditionValues, childConditions[0].getValues());
assertArrayEquals("Values in nominal condition did not match", conditionValues, childConditions[1].getValues());
assertEquals("Wrong set logic.", SetLogic.IS_NOT_IN, childConditions[0].getSetLogic());
assertEquals("Wrong set logic.", SetLogic.IS_IN, childConditions[1].getSetLogic());
assertFalse("Missig values are not sent to the correct child.", childConditions[0].acceptsMissings());
assertTrue("Missig values are not sent to the correct child.", childConditions[1].acceptsMissings());
}
use of org.knime.base.node.mine.treeensemble2.model.TreeNodeNominalBinaryCondition in project knime-core by knime.
the class TreeNodeNominalBinaryConditionTest method testToPMMLPredicate.
/**
* This method tests the
* {@link TreeNodeNominalBinaryCondition#toPMMLPredicate()} method.
*
* @throws Exception
*/
@Test
public void testToPMMLPredicate() throws Exception {
final TreeEnsembleLearnerConfiguration config = new TreeEnsembleLearnerConfiguration(false);
final TestDataGenerator dataGen = new TestDataGenerator(config);
final TreeNominalColumnData col = dataGen.createNominalAttributeColumn("A,A,B,C,C,D", "testcol", 0);
TreeNodeNominalBinaryCondition cond = new TreeNodeNominalBinaryCondition(col.getMetaData(), BigInteger.valueOf(1), true, false);
PMMLPredicate predicate = cond.toPMMLPredicate();
assertThat(predicate, instanceOf(PMMLSimpleSetPredicate.class));
PMMLSimpleSetPredicate setPredicate = (PMMLSimpleSetPredicate) predicate;
assertEquals("Wrong attribute", col.getMetaData().getAttributeName(), setPredicate.getSplitAttribute());
assertEquals("Wrong set predicate", PMMLSetOperator.IS_IN, setPredicate.getSetOperator());
assertArrayEquals("Wrong values", new String[] { "A" }, setPredicate.getValues().toArray(new String[1]));
cond = new TreeNodeNominalBinaryCondition(col.getMetaData(), BigInteger.valueOf(2), false, true);
predicate = cond.toPMMLPredicate();
assertEquals("Wrong attribute", col.getMetaData().getAttributeName(), predicate.getSplitAttribute());
assertThat(predicate, instanceOf(PMMLCompoundPredicate.class));
PMMLCompoundPredicate compoundPredicate = (PMMLCompoundPredicate) predicate;
assertEquals("Wrong boolean operator", PMMLBooleanOperator.OR, compoundPredicate.getBooleanOperator());
LinkedList<PMMLPredicate> preds = compoundPredicate.getPredicates();
assertEquals("Number of predicates did not match.", 2, preds.size());
assertThat(preds.get(0), instanceOf(PMMLSimpleSetPredicate.class));
setPredicate = (PMMLSimpleSetPredicate) preds.get(0);
assertEquals("Wrong attribute", col.getMetaData().getAttributeName(), setPredicate.getSplitAttribute());
assertEquals("Wrong set predicate", PMMLSetOperator.IS_NOT_IN, setPredicate.getSetOperator());
assertArrayEquals("Wrong values", new String[] { "B" }, setPredicate.getValues().toArray(new String[1]));
assertThat(preds.get(1), instanceOf(PMMLSimplePredicate.class));
PMMLSimplePredicate simplePredicate = (PMMLSimplePredicate) preds.get(1);
assertEquals("Should be isMissing", PMMLOperator.IS_MISSING, simplePredicate.getOperator());
}
Aggregations