Search in sources :

Example 1 with TreeNodeSignature

use of org.knime.base.node.mine.treeensemble.model.TreeNodeSignature in project knime-core by knime.

the class TreeLearnerClassification method buildTreeNode.

private TreeNodeClassification buildTreeNode(final ExecutionMonitor exec, final int currentDepth, final double[] rowSampleWeights, final TreeNodeSignature treeNodeSignature, final ClassificationPriors targetPriors, final BitSet forbiddenColumnSet, final TreeNodeMembershipController membershipController) throws CanceledExecutionException {
    final TreeData data = getData();
    final TreeEnsembleLearnerConfiguration config = getConfig();
    exec.checkCanceled();
    SplitCandidate bestSplit = findBestSplitClassification(currentDepth, rowSampleWeights, treeNodeSignature, targetPriors, forbiddenColumnSet, membershipController);
    if (bestSplit == null) {
        return new TreeNodeClassification(treeNodeSignature, targetPriors, getConfig());
    }
    TreeAttributeColumnData splitColumn = bestSplit.getColumnData();
    final int attributeIndex = splitColumn.getMetaData().getAttributeIndex();
    boolean markAttributeAsForbidden = !bestSplit.canColumnBeSplitFurther();
    forbiddenColumnSet.set(attributeIndex, markAttributeAsForbidden);
    TreeNodeCondition[] childConditions = bestSplit.getChildConditions();
    if (childConditions.length > Short.MAX_VALUE) {
        throw new RuntimeException("Too many children when splitting " + "attribute " + bestSplit.getColumnData() + " (maximum supported: " + Short.MAX_VALUE + "): " + childConditions.length);
    }
    TreeNodeClassification[] childNodes = new TreeNodeClassification[childConditions.length];
    final double[] dataMemberships = rowSampleWeights;
    // final double[] dataMemberships = rowSampleWeights.getMemberships();
    final double[] childMemberships = new double[dataMemberships.length];
    final TreeTargetNominalColumnData targetColumn = (TreeTargetNominalColumnData) data.getTargetColumn();
    for (int i = 0; i < childConditions.length; i++) {
        System.arraycopy(dataMemberships, 0, childMemberships, 0, dataMemberships.length);
        TreeNodeCondition cond = childConditions[i];
        splitColumn.updateChildMemberships(cond, dataMemberships, childMemberships);
        // TreeNodeMembershipController childMembershipController = membershipController.createChildTreeNodeMembershipController(childMemberships);
        TreeNodeMembershipController childMembershipController = null;
        ClassificationPriors childTargetPriors = targetColumn.getDistribution(childMemberships, config);
        TreeNodeSignature childSignature = treeNodeSignature.createChildSignature((short) i);
        childNodes[i] = buildTreeNode(exec, currentDepth + 1, childMemberships, childSignature, childTargetPriors, forbiddenColumnSet, childMembershipController);
        childNodes[i].setTreeNodeCondition(cond);
    }
    if (markAttributeAsForbidden) {
        forbiddenColumnSet.set(attributeIndex, false);
    }
    return new TreeNodeClassification(treeNodeSignature, targetPriors, childNodes, getConfig());
}
Also used : TreeEnsembleLearnerConfiguration(org.knime.base.node.mine.treeensemble.node.learner.TreeEnsembleLearnerConfiguration) TreeNodeClassification(org.knime.base.node.mine.treeensemble.model.TreeNodeClassification) TreeNodeMembershipController(org.knime.base.node.mine.treeensemble.data.TreeNodeMembershipController) TreeAttributeColumnData(org.knime.base.node.mine.treeensemble.data.TreeAttributeColumnData) TreeNodeSignature(org.knime.base.node.mine.treeensemble.model.TreeNodeSignature) TreeData(org.knime.base.node.mine.treeensemble.data.TreeData) TreeNodeCondition(org.knime.base.node.mine.treeensemble.model.TreeNodeCondition) TreeTargetNominalColumnData(org.knime.base.node.mine.treeensemble.data.TreeTargetNominalColumnData) ClassificationPriors(org.knime.base.node.mine.treeensemble.data.ClassificationPriors)

Example 2 with TreeNodeSignature

use of org.knime.base.node.mine.treeensemble.model.TreeNodeSignature in project knime-core by knime.

the class TreeEnsembleLearner method createColumnStatisticTable.

public BufferedDataTable createColumnStatisticTable(final ExecutionContext exec) throws CanceledExecutionException {
    BufferedDataContainer c = exec.createDataContainer(getColumnStatisticTableSpec());
    final int nrModels = m_ensembleModel.getNrModels();
    final TreeAttributeColumnData[] columns = m_data.getColumns();
    final int nrAttributes = columns.length;
    int[][] columnOnLevelCounts = new int[REPORT_LEVEL][nrAttributes];
    int[][] columnInLevelSampleCounts = new int[REPORT_LEVEL][nrAttributes];
    for (int i = 0; i < nrModels; i++) {
        final AbstractTreeModel<?> treeModel = m_ensembleModel.getTreeModel(i);
        for (int level = 0; level < REPORT_LEVEL; level++) {
            for (AbstractTreeNode treeNodeOnLevel : treeModel.getTreeNodes(level)) {
                TreeNodeSignature sig = treeNodeOnLevel.getSignature();
                ColumnSampleStrategy colStrat = m_columnSampleStrategies[i];
                ColumnSample cs = colStrat.getColumnSampleForTreeNode(sig);
                for (TreeAttributeColumnData col : cs) {
                    final int index = col.getMetaData().getAttributeIndex();
                    columnInLevelSampleCounts[level][index] += 1;
                }
                int splitAttIdx = treeNodeOnLevel.getSplitAttributeIndex();
                if (splitAttIdx >= 0) {
                    columnOnLevelCounts[level][splitAttIdx] += 1;
                }
            }
        }
    }
    for (int i = 0; i < nrAttributes; i++) {
        String name = columns[i].getMetaData().getAttributeName();
        int[] counts = new int[2 * REPORT_LEVEL];
        for (int level = 0; level < REPORT_LEVEL; level++) {
            counts[level] = columnOnLevelCounts[level][i];
            counts[REPORT_LEVEL + level] = columnInLevelSampleCounts[level][i];
        }
        DataRow row = new DefaultRow(name, counts);
        c.addRowToTable(row);
        exec.checkCanceled();
    }
    c.close();
    return c.getTable();
}
Also used : ColumnSampleStrategy(org.knime.base.node.mine.treeensemble.sample.column.ColumnSampleStrategy) TreeAttributeColumnData(org.knime.base.node.mine.treeensemble.data.TreeAttributeColumnData) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) ColumnSample(org.knime.base.node.mine.treeensemble.sample.column.ColumnSample) AbstractTreeNode(org.knime.base.node.mine.treeensemble.model.AbstractTreeNode) TreeNodeSignature(org.knime.base.node.mine.treeensemble.model.TreeNodeSignature) DataRow(org.knime.core.data.DataRow) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 3 with TreeNodeSignature

use of org.knime.base.node.mine.treeensemble.model.TreeNodeSignature in project knime-core by knime.

the class TreeLearnerRegression method buildTreeNode.

private TreeNodeRegression buildTreeNode(final ExecutionMonitor exec, final int currentDepth, final double[] rowSampleWeights, final TreeNodeSignature treeNodeSignature, final RegressionPriors targetPriors, final BitSet forbiddenColumnSet, final TreeNodeMembershipController membershipController) throws CanceledExecutionException {
    final TreeData data = getData();
    final TreeEnsembleLearnerConfiguration config = getConfig();
    exec.checkCanceled();
    SplitCandidate bestSplit = findBestSplitRegression(currentDepth, rowSampleWeights, treeNodeSignature, targetPriors, forbiddenColumnSet, membershipController);
    if (bestSplit == null) {
        return new TreeNodeRegression(treeNodeSignature, targetPriors);
    }
    TreeAttributeColumnData splitColumn = bestSplit.getColumnData();
    final int attributeIndex = splitColumn.getMetaData().getAttributeIndex();
    boolean markAttributeAsForbidden = !bestSplit.canColumnBeSplitFurther();
    forbiddenColumnSet.set(attributeIndex, markAttributeAsForbidden);
    TreeNodeCondition[] childConditions = bestSplit.getChildConditions();
    if (childConditions.length > Short.MAX_VALUE) {
        throw new RuntimeException("Too many children when splitting " + "attribute " + bestSplit.getColumnData() + " (maximum supported: " + Short.MAX_VALUE + "): " + childConditions.length);
    }
    TreeNodeRegression[] childNodes = new TreeNodeRegression[childConditions.length];
    final double[] dataMemberships = rowSampleWeights;
    final double[] childMemberships = new double[dataMemberships.length];
    final TreeTargetNumericColumnData targetColumn = (TreeTargetNumericColumnData) data.getTargetColumn();
    for (int i = 0; i < childConditions.length; i++) {
        System.arraycopy(dataMemberships, 0, childMemberships, 0, dataMemberships.length);
        TreeNodeCondition cond = childConditions[i];
        splitColumn.updateChildMemberships(cond, dataMemberships, childMemberships);
        RegressionPriors childTargetPriors = targetColumn.getPriors(childMemberships, config);
        TreeNodeSignature childSignature = treeNodeSignature.createChildSignature((short) i);
        TreeNodeMembershipController childMembershipController = splitColumn.getChildNodeMembershipController(cond, membershipController);
        childNodes[i] = buildTreeNode(exec, currentDepth + 1, childMemberships, childSignature, childTargetPriors, forbiddenColumnSet, childMembershipController);
        childNodes[i].setTreeNodeCondition(cond);
    }
    if (markAttributeAsForbidden) {
        forbiddenColumnSet.set(attributeIndex, false);
    }
    return new TreeNodeRegression(treeNodeSignature, targetPriors, childNodes);
}
Also used : TreeEnsembleLearnerConfiguration(org.knime.base.node.mine.treeensemble.node.learner.TreeEnsembleLearnerConfiguration) TreeNodeMembershipController(org.knime.base.node.mine.treeensemble.data.TreeNodeMembershipController) TreeAttributeColumnData(org.knime.base.node.mine.treeensemble.data.TreeAttributeColumnData) RegressionPriors(org.knime.base.node.mine.treeensemble.data.RegressionPriors) TreeTargetNumericColumnData(org.knime.base.node.mine.treeensemble.data.TreeTargetNumericColumnData) TreeNodeSignature(org.knime.base.node.mine.treeensemble.model.TreeNodeSignature) TreeNodeRegression(org.knime.base.node.mine.treeensemble.model.TreeNodeRegression) TreeData(org.knime.base.node.mine.treeensemble.data.TreeData) TreeNodeCondition(org.knime.base.node.mine.treeensemble.model.TreeNodeCondition)

Aggregations

TreeAttributeColumnData (org.knime.base.node.mine.treeensemble.data.TreeAttributeColumnData)3 TreeNodeSignature (org.knime.base.node.mine.treeensemble.model.TreeNodeSignature)3 TreeData (org.knime.base.node.mine.treeensemble.data.TreeData)2 TreeNodeMembershipController (org.knime.base.node.mine.treeensemble.data.TreeNodeMembershipController)2 TreeNodeCondition (org.knime.base.node.mine.treeensemble.model.TreeNodeCondition)2 TreeEnsembleLearnerConfiguration (org.knime.base.node.mine.treeensemble.node.learner.TreeEnsembleLearnerConfiguration)2 ClassificationPriors (org.knime.base.node.mine.treeensemble.data.ClassificationPriors)1 RegressionPriors (org.knime.base.node.mine.treeensemble.data.RegressionPriors)1 TreeTargetNominalColumnData (org.knime.base.node.mine.treeensemble.data.TreeTargetNominalColumnData)1 TreeTargetNumericColumnData (org.knime.base.node.mine.treeensemble.data.TreeTargetNumericColumnData)1 AbstractTreeNode (org.knime.base.node.mine.treeensemble.model.AbstractTreeNode)1 TreeNodeClassification (org.knime.base.node.mine.treeensemble.model.TreeNodeClassification)1 TreeNodeRegression (org.knime.base.node.mine.treeensemble.model.TreeNodeRegression)1 ColumnSample (org.knime.base.node.mine.treeensemble.sample.column.ColumnSample)1 ColumnSampleStrategy (org.knime.base.node.mine.treeensemble.sample.column.ColumnSampleStrategy)1 DataRow (org.knime.core.data.DataRow)1 DefaultRow (org.knime.core.data.def.DefaultRow)1 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)1