Search in sources :

Example 1 with ClassificationPriors

use of org.knime.base.node.mine.treeensemble.data.ClassificationPriors in project knime-core by knime.

the class TreeLearnerClassification method learnSingleTree.

/**
 * {@inheritDoc}
 */
@Override
public TreeModelClassification learnSingleTree(final ExecutionMonitor exec, final RandomData rd) throws CanceledExecutionException {
    final TreeData data = getData();
    final RowSample rowSampling = getRowSampling();
    final TreeEnsembleLearnerConfiguration config = getConfig();
    final TreeTargetNominalColumnData targetColumn = (TreeTargetNominalColumnData) data.getTargetColumn();
    double[] dataMemberships = new double[data.getNrRows()];
    for (int i = 0; i < dataMemberships.length; i++) {
        // dataMemberships[i] = m_rowSampling.getCountFor(i) > 0 ? 1.0 : 0.0;
        dataMemberships[i] = rowSampling.getCountFor(i);
    }
    ClassificationPriors targetPriors = targetColumn.getDistribution(dataMemberships, config);
    BitSet forbiddenColumnSet = new BitSet(data.getNrAttributes());
    // TreeNodeMembershipController rootMembershipController = new TreeNodeMembershipController(data, dataMemberships);
    TreeNodeMembershipController rootMembershipController = null;
    TreeNodeClassification rootNode = buildTreeNode(exec, 0, dataMemberships, TreeNodeSignature.ROOT_SIGNATURE, targetPriors, forbiddenColumnSet, rootMembershipController);
    assert forbiddenColumnSet.cardinality() == 0;
    rootNode.setTreeNodeCondition(TreeNodeTrueCondition.INSTANCE);
    return new TreeModelClassification(rootNode);
}
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) BitSet(java.util.BitSet) TreeData(org.knime.base.node.mine.treeensemble.data.TreeData) RowSample(org.knime.base.node.mine.treeensemble.sample.row.RowSample) TreeTargetNominalColumnData(org.knime.base.node.mine.treeensemble.data.TreeTargetNominalColumnData) ClassificationPriors(org.knime.base.node.mine.treeensemble.data.ClassificationPriors) TreeModelClassification(org.knime.base.node.mine.treeensemble.model.TreeModelClassification)

Example 2 with ClassificationPriors

use of org.knime.base.node.mine.treeensemble.data.ClassificationPriors 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)

Aggregations

ClassificationPriors (org.knime.base.node.mine.treeensemble.data.ClassificationPriors)2 TreeData (org.knime.base.node.mine.treeensemble.data.TreeData)2 TreeNodeMembershipController (org.knime.base.node.mine.treeensemble.data.TreeNodeMembershipController)2 TreeTargetNominalColumnData (org.knime.base.node.mine.treeensemble.data.TreeTargetNominalColumnData)2 TreeNodeClassification (org.knime.base.node.mine.treeensemble.model.TreeNodeClassification)2 TreeEnsembleLearnerConfiguration (org.knime.base.node.mine.treeensemble.node.learner.TreeEnsembleLearnerConfiguration)2 BitSet (java.util.BitSet)1 TreeAttributeColumnData (org.knime.base.node.mine.treeensemble.data.TreeAttributeColumnData)1 TreeModelClassification (org.knime.base.node.mine.treeensemble.model.TreeModelClassification)1 TreeNodeCondition (org.knime.base.node.mine.treeensemble.model.TreeNodeCondition)1 TreeNodeSignature (org.knime.base.node.mine.treeensemble.model.TreeNodeSignature)1 RowSample (org.knime.base.node.mine.treeensemble.sample.row.RowSample)1