use of org.knime.base.node.mine.treeensemble2.model.TreeNodeSignature in project knime-core by knime.
the class RegressionGBTModelImporter method importFromPMMLInternal.
/**
* {@inheritDoc}
*/
@Override
public GradientBoostedTreesModel importFromPMMLInternal(final MiningModel miningModel) {
Segmentation segmentation = miningModel.getSegmentation();
CheckUtils.checkArgument(segmentation.getMultipleModelMethod() == MULTIPLEMODELMETHOD.SUM, "The provided segmentation has not the required sum as multiple model method but '%s' instead.", segmentation.getMultipleModelMethod());
Pair<List<TreeModelRegression>, List<Map<TreeNodeSignature, Double>>> treesCoeffientMapsPair = readSumSegmentation(segmentation);
List<TreeModelRegression> trees = treesCoeffientMapsPair.getFirst();
// TODO user should be warned if there is no initial value or anything else is fishy
double initialValue = miningModel.getTargets().getTargetList().get(0).getRescaleConstant();
// currently only models learned on "ordinary" columns can be read back in
return new GradientBoostedTreesModel(getMetaDataMapper().getTreeMetaData(), trees.toArray(new TreeModelRegression[trees.size()]), TreeType.Ordinary, initialValue, treesCoeffientMapsPair.getSecond());
}
use of org.knime.base.node.mine.treeensemble2.model.TreeNodeSignature in project knime-core by knime.
the class SubsetColumnSampleStrategyTest method testGetColumnSampleForTreeNode.
/**
* Tests the method {@link SubsetColumnSampleStrategy#getColumnSampleForTreeNode(org.knime.base.node.mine.treeensemble2.model.TreeNodeSignature)}
* also tests {@link SubsetColumnSample} since the both always act in combination.
*
* @throws Exception
*/
@Test
public void testGetColumnSampleForTreeNode() throws Exception {
final SubsetColumnSampleStrategy strategy = new SubsetColumnSampleStrategy(createTreeData(), RD, 5);
TreeNodeSignatureFactory sigFac = createSignatureFactory();
TreeNodeSignature rootSig = sigFac.getRootSignature();
ColumnSample sample = strategy.getColumnSampleForTreeNode(rootSig);
assertEquals("Wrong number of columns in sample.", 5, sample.getNumCols());
int[] colIndices = sample.getColumnIndices();
sample = strategy.getColumnSampleForTreeNode(sigFac.getChildSignatureFor(rootSig, (byte) 0));
assertEquals("Wrong number of columns in sample.", 5, sample.getNumCols());
assertArrayEquals(colIndices, sample.getColumnIndices());
sample = strategy.getColumnSampleForTreeNode(sigFac.getChildSignatureFor(rootSig, (byte) 1));
assertEquals("Wrong number of columns in sample.", 5, sample.getNumCols());
assertArrayEquals(colIndices, sample.getColumnIndices());
}
use of org.knime.base.node.mine.treeensemble2.model.TreeNodeSignature in project knime-core by knime.
the class TreeLearnerRegression method learnSingleTree.
/**
* {@inheritDoc}
*/
@Override
public TreeModelRegression learnSingleTree(final ExecutionMonitor exec, final RandomData rd) throws CanceledExecutionException {
final TreeTargetNumericColumnData targetColumn = getTargetData();
final TreeData data = getData();
final RowSample rowSampling = getRowSampling();
final TreeEnsembleLearnerConfiguration config = getConfig();
final IDataIndexManager indexManager = getIndexManager();
DataMemberships rootDataMemberships = new RootDataMemberships(rowSampling, data, indexManager);
RegressionPriors targetPriors = targetColumn.getPriors(rootDataMemberships, config);
BitSet forbiddenColumnSet = new BitSet(data.getNrAttributes());
boolean isGradientBoosting = config instanceof GradientBoostingLearnerConfiguration;
if (isGradientBoosting) {
m_leafs = new ArrayList<TreeNodeRegression>();
}
final TreeNodeSignature rootSignature = TreeNodeSignature.ROOT_SIGNATURE;
final ColumnSample rootColumnSample = getColSamplingStrategy().getColumnSampleForTreeNode(rootSignature);
TreeNodeRegression rootNode = buildTreeNode(exec, 0, rootDataMemberships, rootColumnSample, getSignatureFactory().getRootSignature(), targetPriors, forbiddenColumnSet);
assert forbiddenColumnSet.cardinality() == 0;
rootNode.setTreeNodeCondition(TreeNodeTrueCondition.INSTANCE);
if (isGradientBoosting) {
return new TreeModelRegression(rootNode, m_leafs);
}
return new TreeModelRegression(rootNode);
}
use of org.knime.base.node.mine.treeensemble2.model.TreeNodeSignature in project knime-core by knime.
the class AbstractGradientBoostedTreesLearner method adaptPreviousPrediction.
/**
* Adapts the previous prediction by adding the predictions of the <b>tree</b> regulated by the respective
* coefficients in <b>coefficientMap</b>.
*
* @param previousPrediction Prediction of the previous steps
* @param tree the tree of the current iteration
* @param coefficientMap contains the coefficients for the leafs of the tree
*/
protected void adaptPreviousPrediction(final double[] previousPrediction, final TreeModelRegression tree, final Map<TreeNodeSignature, Double> coefficientMap) {
TreeData data = getData();
IDataIndexManager indexManager = getIndexManager();
for (int i = 0; i < data.getNrRows(); i++) {
PredictorRecord record = createPredictorRecord(data, indexManager, i);
previousPrediction[i] += coefficientMap.get(tree.findMatchingNode(record).getSignature());
}
}
use of org.knime.base.node.mine.treeensemble2.model.TreeNodeSignature in project knime-core by knime.
the class LKGradientBoostedTreesLearner method calculateCoefficientMap.
private Map<TreeNodeSignature, Double> calculateCoefficientMap(final TreeModelRegression tree, final TreeData pseudoResiduals, final double numClasses) {
final List<TreeNodeRegression> leafs = tree.getLeafs();
final Map<TreeNodeSignature, Double> coefficientMap = new HashMap<TreeNodeSignature, Double>();
final TreeTargetNumericColumnData pseudoTarget = (TreeTargetNumericColumnData) pseudoResiduals.getTargetColumn();
double learningRate = getConfig().getLearningRate();
for (TreeNodeRegression leaf : leafs) {
final int[] indices = leaf.getRowIndicesInTreeData();
double sumTop = 0;
double sumBottom = 0;
for (int index : indices) {
double val = pseudoTarget.getValueFor(index);
sumTop += val;
double absVal = Math.abs(val);
sumBottom += Math.abs(absVal) * (1 - Math.abs(absVal));
}
final double coefficient = (numClasses - 1) / numClasses * (sumTop / sumBottom);
coefficientMap.put(leaf.getSignature(), learningRate * coefficient);
}
return coefficientMap;
}
Aggregations