use of org.knime.base.node.mine.treeensemble2.model.TreeNodeRegression 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.TreeNodeRegression 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;
}
use of org.knime.base.node.mine.treeensemble2.model.TreeNodeRegression in project knime-core by knime.
the class TreeModelPMMLTranslator method addTreeNode.
/**
* @param pmmlNode
* @param node
*/
private void addTreeNode(final Node pmmlNode, final AbstractTreeNode node) {
int index = m_nodeIndex++;
pmmlNode.setId(Integer.toString(index));
if (node instanceof TreeNodeClassification) {
final TreeNodeClassification clazNode = (TreeNodeClassification) node;
pmmlNode.setScore(clazNode.getMajorityClassName());
float[] targetDistribution = clazNode.getTargetDistribution();
NominalValueRepresentation[] targetVals = clazNode.getTargetMetaData().getValues();
double sum = 0.0;
for (Float v : targetDistribution) {
sum += v;
}
pmmlNode.setRecordCount(sum);
// adding score distribution (class counts)
for (int i = 0; i < targetDistribution.length; i++) {
String className = targetVals[i].getNominalValue();
double freq = targetDistribution[i];
ScoreDistribution pmmlScoreDist = pmmlNode.addNewScoreDistribution();
pmmlScoreDist.setValue(className);
pmmlScoreDist.setRecordCount(freq);
}
} else if (node instanceof TreeNodeRegression) {
final TreeNodeRegression regNode = (TreeNodeRegression) node;
pmmlNode.setScore(Double.toString(regNode.getMean()));
}
TreeNodeCondition condition = node.getCondition();
if (condition instanceof TreeNodeTrueCondition) {
pmmlNode.addNewTrue();
} else if (condition instanceof TreeNodeColumnCondition) {
final TreeNodeColumnCondition colCondition = (TreeNodeColumnCondition) condition;
handleColumnCondition(colCondition, pmmlNode);
} else if (condition instanceof AbstractTreeNodeSurrogateCondition) {
final AbstractTreeNodeSurrogateCondition surrogateCond = (AbstractTreeNodeSurrogateCondition) condition;
setValuesFromPMMLCompoundPredicate(pmmlNode.addNewCompoundPredicate(), surrogateCond.toPMMLPredicate());
} else {
throw new IllegalStateException("Unsupported condition (not " + "implemented): " + condition.getClass().getSimpleName());
}
for (int i = 0; i < node.getNrChildren(); i++) {
addTreeNode(pmmlNode.addNewNode(), node.getChild(i));
}
}
use of org.knime.base.node.mine.treeensemble2.model.TreeNodeRegression in project knime-core by knime.
the class AbstractRegressionContentParser method createNode.
@Override
public final TreeNodeRegression createNode(final Node node, final TargetColumnHelper<TreeTargetNumericColumnMetaData> targetHelper, final TreeNodeSignature signature, final List<TreeNodeRegression> children) {
double mean = Double.parseDouble(node.getScore());
OptionalDouble totalSum = node.getExtensionList().stream().filter(e -> e.getName().equals(TranslationUtil.TOTAL_SUM_KEY)).mapToDouble(e -> Double.parseDouble(e.getValue())).findFirst();
OptionalDouble sumSquaredDeviation = node.getExtensionList().stream().filter(e -> e.getName().equals(TranslationUtil.SUM_SQUARED_DEVIATION_KEY)).mapToDouble(e -> Double.parseDouble(e.getValue())).findFirst();
return createNodeInternal(node, targetHelper.getMetaData(), signature, mean, totalSum.orElse(-1), sumSquaredDeviation.orElse(-1), children.toArray(new TreeNodeRegression[children.size()]));
}
use of org.knime.base.node.mine.treeensemble2.model.TreeNodeRegression in project knime-core by knime.
the class AbstractGBTModelImporter method readTreeModel.
private Pair<TreeModelRegression, Map<TreeNodeSignature, Double>> readTreeModel(final Segment segment) {
GBTRegressionContentParser contentParser = new GBTRegressionContentParser();
TreeModelImporter<TreeNodeRegression, TreeModelRegression, TreeTargetNumericColumnMetaData> treeImporter = new TreeModelImporter<TreeNodeRegression, TreeModelRegression, TreeTargetNumericColumnMetaData>(m_metaDataMapper, m_conditionParser, m_signatureFactory, contentParser, m_treeFactory);
TreeModel treeModel = segment.getTreeModel();
TreeModelRegression tree = treeImporter.importFromPMML(treeModel);
Map<TreeNodeSignature, Double> coefficientMap = contentParser.getCoefficientMap();
return new Pair<>(tree, coefficientMap);
}
Aggregations