use of org.knime.base.node.mine.treeensemble2.data.TreeNominalColumnData in project knime-core by knime.
the class TreeNominalColumnDataTest method createPCATestData.
private static Pair<TreeNominalColumnData, TreeTargetNominalColumnData> createPCATestData(final TreeEnsembleLearnerConfiguration config) {
DataColumnSpec colSpec = new DataColumnSpecCreator("test-col", StringCell.TYPE).createSpec();
final String[] attVals = new String[] { "A", "B", "C", "D", "E" };
final String[] classes = new String[] { "T1", "T2", "T3" };
TreeNominalColumnDataCreator colCreator = new TreeNominalColumnDataCreator(colSpec);
DataColumnSpecCreator specCreator = new DataColumnSpecCreator("target-col", StringCell.TYPE);
specCreator.setDomain(new DataColumnDomainCreator(Arrays.stream(classes).distinct().map(s -> new StringCell(s)).toArray(i -> new StringCell[i])).createDomain());
DataColumnSpec targetSpec = specCreator.createSpec();
TreeTargetColumnDataCreator targetCreator = new TreeTargetNominalColumnDataCreator(targetSpec);
long rowKeyCounter = 0;
final int[][] classDistributions = new int[][] { { 40, 10, 10 }, { 10, 40, 10 }, { 20, 30, 10 }, { 20, 15, 25 }, { 10, 5, 45 } };
for (int i = 0; i < attVals.length; i++) {
for (int j = 0; j < classes.length; j++) {
for (int k = 0; k < classDistributions[i][j]; k++) {
RowKey key = RowKey.createRowKey(rowKeyCounter++);
colCreator.add(key, new StringCell(attVals[i]));
targetCreator.add(key, new StringCell(classes[j]));
}
}
}
final TreeNominalColumnData testColData = colCreator.createColumnData(0, config);
testColData.getMetaData().setAttributeIndex(0);
return Pair.create(testColData, (TreeTargetNominalColumnData) targetCreator.createColumnData());
}
use of org.knime.base.node.mine.treeensemble2.data.TreeNominalColumnData in project knime-core by knime.
the class TreeNominalColumnDataTest method testCalcBestSplitClassificationMultiWay.
/**
* Tests the method
* {@link TreeNominalColumnData#calcBestSplitClassification(DataMemberships, ClassificationPriors, TreeTargetNominalColumnData, RandomData)}
* using multiway splits
*
* @throws Exception
*/
@Test
public void testCalcBestSplitClassificationMultiWay() throws Exception {
TreeEnsembleLearnerConfiguration config = createConfig(false);
config.setUseBinaryNominalSplits(false);
Pair<TreeNominalColumnData, TreeTargetNominalColumnData> tennisData = tennisData(config);
TreeNominalColumnData columnData = tennisData.getFirst();
TreeTargetNominalColumnData targetData = tennisData.getSecond();
TreeData treeData = createTreeData(tennisData);
assertEquals(SplitCriterion.Gini, config.getSplitCriterion());
double[] rowWeights = new double[SMALL_COLUMN_DATA.length];
Arrays.fill(rowWeights, 1.0);
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, null);
assertNotNull(splitCandidate);
assertThat(splitCandidate, instanceOf(NominalMultiwaySplitCandidate.class));
assertFalse(splitCandidate.canColumnBeSplitFurther());
// manually via libre office calc
assertEquals(0.0744897959, splitCandidate.getGainValue(), 0.00001);
NominalMultiwaySplitCandidate multiWaySplitCandidate = (NominalMultiwaySplitCandidate) splitCandidate;
TreeNodeNominalCondition[] childConditions = multiWaySplitCandidate.getChildConditions();
assertEquals(3, childConditions.length);
assertEquals("S", childConditions[0].getValue());
assertEquals("O", childConditions[1].getValue());
assertEquals("R", childConditions[2].getValue());
}
use of org.knime.base.node.mine.treeensemble2.data.TreeNominalColumnData 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.data.TreeNominalColumnData in project knime-core by knime.
the class TreeNominalColumnDataTest method testCalcBestSplitRegressionMultiwayXGBoostMissingValueHandling.
/**
* This method tests the XGBoost missing value handling in case of a regression task and multiway splits.
*
* @throws Exception
*/
@Test
public void testCalcBestSplitRegressionMultiwayXGBoostMissingValueHandling() throws Exception {
final TreeEnsembleLearnerConfiguration config = createConfig(true);
config.setMissingValueHandling(MissingValueHandling.XGBoost);
config.setUseBinaryNominalSplits(false);
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(NominalMultiwaySplitCandidate.class));
assertEquals("Wrong gain.", 22.888888, split.getGainValue(), 1e-5);
assertTrue("No missing values in dataCol therefore the missedRows BitSet must be empty.", split.getMissedRows().isEmpty());
NominalMultiwaySplitCandidate nomSplit = (NominalMultiwaySplitCandidate) split;
TreeNodeNominalCondition[] conditions = nomSplit.getChildConditions();
assertEquals("3 nominal values therefore there must be 3 children.", 3, conditions.length);
assertEquals("Wrong value.", "A", conditions[0].getValue());
assertEquals("Wrong value.", "B", conditions[1].getValue());
assertEquals("Wrong value.", "C", conditions[2].getValue());
assertFalse("Missings should go with majority", conditions[0].acceptsMissings());
assertTrue("Missings should go with majority", conditions[1].acceptsMissings());
assertFalse("Missings should go with majority", conditions[2].acceptsMissings());
// 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(NominalMultiwaySplitCandidate.class));
// assertEquals("Wrong gain.", 36.233333333, split.getGainValue(), 1e-5);
assertTrue("Conditions should handle missing values therefore the missedRows BitSet must be empty.", split.getMissedRows().isEmpty());
nomSplit = (NominalMultiwaySplitCandidate) split;
conditions = nomSplit.getChildConditions();
assertEquals("3 values (not counting missing values) therefore there must be 3 children.", 3, conditions.length);
assertEquals("Wrong value.", "A", conditions[0].getValue());
assertEquals("Wrong value.", "B", conditions[1].getValue());
assertEquals("Wrong value.", "C", conditions[2].getValue());
assertFalse("Missings should go with majority", conditions[0].acceptsMissings());
assertTrue("Missings should go with majority", conditions[1].acceptsMissings());
assertFalse("Missings should go with majority", conditions[2].acceptsMissings());
}
use of org.knime.base.node.mine.treeensemble2.data.TreeNominalColumnData 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());
}
Aggregations