use of org.apache.commons.math.random.RandomData 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);
}
use of org.apache.commons.math.random.RandomData in project knime-core by knime.
the class TreeNumericColumnDataTest method testXGBoostMissingValueHandling.
/**
* This method tests if the conditions for child nodes are correct in case of XGBoostMissingValueHandling
*
* @throws Exception
*/
@Test
public void testXGBoostMissingValueHandling() throws Exception {
TreeEnsembleLearnerConfiguration config = createConfig();
config.setMissingValueHandling(MissingValueHandling.XGBoost);
final TestDataGenerator dataGen = new TestDataGenerator(config);
final RandomData rd = config.createRandomData();
final int[] indices = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
final double[] weights = new double[10];
Arrays.fill(weights, 1.0);
final MockDataColMem dataMem = new MockDataColMem(indices, indices, weights);
final String dataCSV = "1,2,2,3,4,5,6,7,NaN,NaN";
final String target1CSV = "A,A,A,A,B,B,B,B,A,A";
final String target2CSV = "A,A,A,A,B,B,B,B,B,B";
final double expectedGain = 0.48;
final TreeNumericColumnData col = dataGen.createNumericAttributeColumn(dataCSV, "testCol", 0);
final TreeTargetNominalColumnData target1 = TestDataGenerator.createNominalTargetColumn(target1CSV);
final SplitCandidate split1 = col.calcBestSplitClassification(dataMem, target1.getDistribution(weights, config), target1, rd);
assertEquals("Wrong gain.", expectedGain, split1.getGainValue(), 1e-8);
final TreeNodeCondition[] childConds1 = split1.getChildConditions();
final TreeNodeNumericCondition numCondLeft1 = (TreeNodeNumericCondition) childConds1[0];
assertEquals("Wrong split point.", 3.5, numCondLeft1.getSplitValue(), 1e-8);
assertTrue("Missings were not sent in the correct direction.", numCondLeft1.acceptsMissings());
final TreeNodeNumericCondition numCondRight1 = (TreeNodeNumericCondition) childConds1[1];
assertEquals("Wrong split point.", 3.5, numCondRight1.getSplitValue(), 1e-8);
assertFalse("Missings were not sent in the correct direction.", numCondRight1.acceptsMissings());
final TreeTargetNominalColumnData target2 = TestDataGenerator.createNominalTargetColumn(target2CSV);
final SplitCandidate split2 = col.calcBestSplitClassification(dataMem, target2.getDistribution(weights, config), target2, rd);
assertEquals("Wrong gain.", expectedGain, split2.getGainValue(), 1e-8);
final TreeNodeCondition[] childConds2 = split2.getChildConditions();
final TreeNodeNumericCondition numCondLeft2 = (TreeNodeNumericCondition) childConds2[0];
assertEquals("Wrong split point.", 3.5, numCondLeft2.getSplitValue(), 1e-8);
assertFalse("Missings were not sent in the correct direction.", numCondLeft2.acceptsMissings());
final TreeNodeNumericCondition numCondRight2 = (TreeNodeNumericCondition) childConds2[1];
assertEquals("Wrong split point.", 3.5, numCondRight2.getSplitValue(), 1e-8);
assertTrue("Missings were not sent in the correct direction.", numCondRight2.acceptsMissings());
}
use of org.apache.commons.math.random.RandomData 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);
}
}
use of org.apache.commons.math.random.RandomData in project knime-core by knime.
the class RFSubsetColumnSampleStrategy method getColumnSampleForTreeNode.
/**
* {@inheritDoc}
*/
@Override
public ColumnSample getColumnSampleForTreeNode(final TreeNodeSignature treeNodeSignature) {
short[] signature = treeNodeSignature.getSignaturePath();
JDKRandomGenerator generator = new JDKRandomGenerator();
generator.setSeed(m_seed);
int[] newSeed = new int[signature.length];
for (int i = 0; i < signature.length; i++) {
for (int p = 0; p <= signature[i]; p++) {
newSeed[i] = generator.nextInt();
}
}
generator.setSeed(newSeed);
int totalColCount = m_data.getColumns().length;
RandomData rd = new RandomDataImpl(generator);
int[] includes = rd.nextPermutation(totalColCount, m_subsetSize);
Arrays.sort(includes);
return new SubsetColumnSample(m_data, includes);
}
use of org.apache.commons.math.random.RandomData in project knime-core by knime.
the class TreeNominalColumnDataTest method testCalcBestSplitClassificationBinaryPCAXGBoostMissingValueHandling.
/**
* Tests the XGBoost missing value handling in the case of binary splits calculated with the pca method (multiple classes)
*
* @throws Exception
*/
@Test
public void testCalcBestSplitClassificationBinaryPCAXGBoostMissingValueHandling() throws Exception {
final TreeEnsembleLearnerConfiguration config = createConfig(false);
config.setMissingValueHandling(MissingValueHandling.XGBoost);
final TestDataGenerator dataGen = new TestDataGenerator(config);
final RandomData rd = config.createRandomData();
// test the case that there are no missing values in the training data
final String noMissingCSV = "a, a, a, b, b, b, b, c, c";
final String noMissingTarget = "A, B, B, C, C, C, B, A, B";
TreeNominalColumnData dataCol = dataGen.createNominalAttributeColumn(noMissingCSV, "noMissings", 0);
TreeTargetNominalColumnData targetCol = TestDataGenerator.createNominalTargetColumn(noMissingTarget);
DataMemberships dataMem = createMockDataMemberships(targetCol.getNrRows());
SplitCandidate split = dataCol.calcBestSplitClassification(dataMem, targetCol.getDistribution(dataMem, config), targetCol, rd);
assertNotNull("There is a possible split.", split);
assertEquals("Incorrect gain.", 0.2086, split.getGainValue(), 1e-3);
assertThat(split, instanceOf(NominalBinarySplitCandidate.class));
NominalBinarySplitCandidate nomSplit = (NominalBinarySplitCandidate) split;
assertTrue("No missing values in the column.", nomSplit.getMissedRows().isEmpty());
TreeNodeNominalBinaryCondition[] conditions = nomSplit.getChildConditions();
assertEquals("A binary split must have 2 child conditions.", 2, conditions.length);
String[] values = new String[] { "a", "c" };
assertArrayEquals("Wrong values in child condition.", values, conditions[0].getValues());
assertArrayEquals("Wrong values in child condition.", values, conditions[1].getValues());
assertEquals("Wrong set logic.", SetLogic.IS_NOT_IN, conditions[0].getSetLogic());
assertEquals("Wrong set logic.", SetLogic.IS_IN, conditions[1].getSetLogic());
assertFalse("Missing values should be sent to the majority child (i.e. right)", conditions[0].acceptsMissings());
assertTrue("Missing values should be sent to the majority child (i.e. right)", conditions[1].acceptsMissings());
// test the case that there are missing values in the training data
final String missingCSV = "a, a, a, b, b, b, b, c, c, ?";
final String missingTarget = "A, B, B, C, C, C, B, A, B, C";
dataCol = dataGen.createNominalAttributeColumn(missingCSV, "missings", 0);
targetCol = TestDataGenerator.createNominalTargetColumn(missingTarget);
dataMem = createMockDataMemberships(targetCol.getNrRows());
split = dataCol.calcBestSplitClassification(dataMem, targetCol.getDistribution(dataMem, config), targetCol, rd);
assertNotNull("There is a possible split.", split);
assertEquals("Incorrect gain.", 0.24, split.getGainValue(), 1e-3);
assertThat(split, instanceOf(NominalBinarySplitCandidate.class));
nomSplit = (NominalBinarySplitCandidate) split;
assertTrue("Split should handle missing values.", nomSplit.getMissedRows().isEmpty());
conditions = nomSplit.getChildConditions();
assertEquals("Wrong number of child conditions.", 2, conditions.length);
assertArrayEquals("Wrong values in child condition.", values, conditions[0].getValues());
assertArrayEquals("Wrong values in child condition.", values, conditions[1].getValues());
assertEquals("Wrong set logic.", SetLogic.IS_NOT_IN, conditions[0].getSetLogic());
assertEquals("Wrong set logic.", SetLogic.IS_IN, conditions[1].getSetLogic());
assertTrue("Missing values should be sent to left child", conditions[0].acceptsMissings());
assertFalse("Missing values should be sent to left child", conditions[1].acceptsMissings());
}
Aggregations