Search in sources :

Example 16 with DecisionTree

use of org.knime.base.node.mine.decisiontree2.model.DecisionTree in project knime-core by knime.

the class DecTreeLearnerGraphView method modelChanged.

/**
 * {@inheritDoc}
 */
@Override
protected void modelChanged() {
    DecisionTreeLearnerNodeModel model = this.getNodeModel();
    if (model != null) {
        m_hiLiteHdl.removeHiLiteListener(this);
        DecisionTree dt = model.getDecisionTree();
        if (dt != null) {
            m_graph.setColorColumn(model.getDecisionTree().getColorColumn());
            m_graph.setRootNode(dt.getRootNode());
            // retrieve HiLiteHandler from Input port
            m_hiLiteHdl = model.getInHiLiteHandler(DecisionTreeLearnerNodeModel.DATA_INPORT);
            // and adjust menu entries for HiLite-ing
            m_hiLiteMenu.setEnabled(m_hiLiteHdl != null);
            m_hiLiteHdl.addHiLiteListener(this);
            recreateHiLite();
        } else {
            m_graph.setColorColumn(null);
            m_graph.setRootNode(null);
        }
    }
}
Also used : DecisionTree(org.knime.base.node.mine.decisiontree2.model.DecisionTree)

Example 17 with DecisionTree

use of org.knime.base.node.mine.decisiontree2.model.DecisionTree in project knime-core by knime.

the class DecisionTreeLearnerNodeModel method execute.

/**
 * Start of decision tree induction.
 *
 * @param exec the execution context for this run
 * @param data the input data to build the decision tree from
 * @return an empty data table array, as just a model is provided
 * @throws Exception any type of exception, e.g. for cancellation,
 *         invalid input,...
 * @see NodeModel#execute(BufferedDataTable[],ExecutionContext)
 */
@Override
protected PortObject[] execute(final PortObject[] data, final ExecutionContext exec) throws Exception {
    // holds the warning message displayed after execution
    StringBuilder warningMessageSb = new StringBuilder();
    ParallelProcessing parallelProcessing = new ParallelProcessing(m_parallelProcessing.getIntValue());
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Number available threads: " + parallelProcessing.getMaxNumberThreads() + " used threads: " + parallelProcessing.getCurrentThreadsInUse());
    }
    exec.setProgress("Preparing...");
    // check input data
    assert (data != null && data[DATA_INPORT] != null);
    BufferedDataTable inData = (BufferedDataTable) data[DATA_INPORT];
    // get column with color information
    String colorColumn = null;
    for (DataColumnSpec s : inData.getDataTableSpec()) {
        if (s.getColorHandler() != null) {
            colorColumn = s.getName();
            break;
        }
    }
    // the data table must have more than 2 records
    if (inData.getRowCount() <= 1) {
        throw new IllegalArgumentException("Input data table must have at least 2 records!");
    }
    // get class column index
    int classColumnIndex = inData.getDataTableSpec().findColumnIndex(m_classifyColumn.getStringValue());
    assert classColumnIndex > -1;
    // create initial In-Memory table
    exec.setProgress("Create initial In-Memory table...");
    InMemoryTableCreator tableCreator = new InMemoryTableCreator(inData, classColumnIndex, m_minNumberRecordsPerNode.getIntValue(), m_skipColumns.getBooleanValue());
    InMemoryTable initialTable = tableCreator.createInMemoryTable(exec.createSubExecutionContext(0.05));
    int removedRows = tableCreator.getRemovedRowsDueToMissingClassValue();
    if (removedRows == inData.getRowCount()) {
        throw new IllegalArgumentException("Class column contains only " + "missing values");
    }
    if (removedRows > 0) {
        warningMessageSb.append(removedRows);
        warningMessageSb.append(" rows removed due to missing class value;");
    }
    // the all over row count is used to report progress
    m_alloverRowCount = initialTable.getSumOfWeights();
    // set the finishing counter
    // this counter will always be incremented when a leaf node is
    // created, as this determines the recursion end and can thus
    // be used for progress indication
    m_finishedCounter = new AtomicDouble(0);
    // get the number of attributes
    m_numberAttributes = initialTable.getNumAttributes();
    // create the quality measure
    final SplitQualityMeasure splitQualityMeasure;
    if (m_splitQualityMeasureType.getStringValue().equals(SPLIT_QUALITY_GINI)) {
        splitQualityMeasure = new SplitQualityGini();
    } else {
        splitQualityMeasure = new SplitQualityGainRatio();
    }
    // build the tree
    // before this set the node counter to 0
    m_counter.set(0);
    exec.setMessage("Building tree...");
    DecisionTreeNode root = null;
    root = buildTree(initialTable, exec, 0, splitQualityMeasure, parallelProcessing);
    boolean isBinaryNominal = m_binaryNominalSplitMode.getBooleanValue();
    boolean isFilterInvalidAttributeValues = m_filterNominalValuesFromParent.getBooleanValue();
    if (isBinaryNominal && isFilterInvalidAttributeValues) {
        // traverse tree nodes and remove from the children the attribute
        // values that were filtered out further up in the tree. "Bug" 3124
        root.filterIllegalAttributes(Collections.EMPTY_MAP);
    }
    // the decision tree model saved as PMML at the second out-port
    DecisionTree decisionTree = new DecisionTree(root, m_classifyColumn.getStringValue(), /* strategy has to be set explicitly as the default in PMML is
                    none, which means rows with missing values are not
                    classified. */
    PMMLMissingValueStrategy.LAST_PREDICTION);
    decisionTree.setColorColumn(colorColumn);
    // prune the tree
    exec.setMessage("Prune tree with " + m_pruningMethod.getStringValue() + "...");
    pruneTree(decisionTree);
    // add highlight patterns and color information
    exec.setMessage("Adding hilite and color info to tree...");
    addHiliteAndColorInfo(inData, decisionTree);
    LOGGER.info("Decision tree consisting of " + decisionTree.getNumberNodes() + " nodes created with pruning method " + m_pruningMethod.getStringValue());
    // set the warning message if available
    if (warningMessageSb.length() > 0) {
        setWarningMessage(warningMessageSb.toString());
    }
    // reset the number available threads
    parallelProcessing.reset();
    parallelProcessing = null;
    // no data out table is created -> return an empty table array
    exec.setMessage("Creating PMML decision tree model...");
    // handle the optional PMML input
    PMMLPortObject inPMMLPort = (PMMLPortObject) data[1];
    DataTableSpec inSpec = inData.getSpec();
    PMMLPortObjectSpec outPortSpec = createPMMLPortObjectSpec(inPMMLPort == null ? null : inPMMLPort.getSpec(), inSpec);
    PMMLPortObject outPMMLPort = new PMMLPortObject(outPortSpec, inPMMLPort, inData.getSpec());
    outPMMLPort.addModelTranslater(new PMMLDecisionTreeTranslator(decisionTree));
    m_decisionTree = decisionTree;
    return new PortObject[] { outPMMLPort };
}
Also used : DecisionTree(org.knime.base.node.mine.decisiontree2.model.DecisionTree) DataTableSpec(org.knime.core.data.DataTableSpec) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PMMLDecisionTreeTranslator(org.knime.base.node.mine.decisiontree2.PMMLDecisionTreeTranslator) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataColumnSpec(org.knime.core.data.DataColumnSpec) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) PortObject(org.knime.core.node.port.PortObject) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) DecisionTreeNode(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode)

Example 18 with DecisionTree

use of org.knime.base.node.mine.decisiontree2.model.DecisionTree in project knime-core by knime.

the class DecisionTreeLearnerNodeModel method loadInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void loadInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    File internalsFile = new File(nodeInternDir, SAVE_INTERNALS_FILE_NAME);
    if (!internalsFile.exists()) {
        // file to load internals from not available
        return;
    }
    BufferedInputStream in = new BufferedInputStream(new GZIPInputStream(new FileInputStream(internalsFile)));
    ModelContentRO decisionTreeModel = ModelContent.loadFromXML(in);
    try {
        m_decisionTree = new DecisionTree(decisionTreeModel);
    } catch (Exception e) {
        // continue, but inform the user via a message
        setWarningMessage("Internal model could not be loaded: " + e.getMessage() + ". The view will not display properly.");
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) DecisionTree(org.knime.base.node.mine.decisiontree2.model.DecisionTree) ModelContentRO(org.knime.core.node.ModelContentRO) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Example 19 with DecisionTree

use of org.knime.base.node.mine.decisiontree2.model.DecisionTree in project knime-core by knime.

the class Pruner method mdlPruning.

/**
 * Prunes a {@link DecisionTree} according to the minimum description lenght
 * (MDL) principle.
 *
 * @param decTree the decision tree to prune
 */
public static void mdlPruning(final DecisionTree decTree) {
    // traverse the tree depth first (in-fix)
    DecisionTreeNode root = decTree.getRootNode();
    mdlPruningRecurse(root);
}
Also used : DecisionTreeNode(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode)

Example 20 with DecisionTree

use of org.knime.base.node.mine.decisiontree2.model.DecisionTree in project knime-core by knime.

the class RegressionTreeModel method createDecisionTree.

public DecisionTree createDecisionTree(final DataTable sampleForHiliting) {
    final DecisionTree result;
    TreeModelRegression treeModel = getTreeModelRegression();
    result = treeModel.createDecisionTree(m_metaData);
    if (sampleForHiliting != null) {
        final DataTableSpec dataSpec = sampleForHiliting.getDataTableSpec();
        final DataTableSpec spec = getLearnAttributeSpec(dataSpec);
        for (DataRow r : sampleForHiliting) {
            try {
                DataRow fullAttributeRow = createLearnAttributeRow(r, spec);
                result.addCoveredPattern(fullAttributeRow, spec);
            } catch (Exception e) {
                // dunno what to do with that
                NodeLogger.getLogger(getClass()).error("Error updating hilite info in tree view", e);
                break;
            }
        }
    }
    return result;
}
Also used : DecisionTree(org.knime.base.node.mine.decisiontree2.model.DecisionTree) DataTableSpec(org.knime.core.data.DataTableSpec) DataRow(org.knime.core.data.DataRow) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Aggregations

DecisionTree (org.knime.base.node.mine.decisiontree2.model.DecisionTree)24 DecisionTreeNode (org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode)11 IOException (java.io.IOException)9 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)9 DataTableSpec (org.knime.core.data.DataTableSpec)8 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)7 DataRow (org.knime.core.data.DataRow)6 BufferedInputStream (java.io.BufferedInputStream)5 File (java.io.File)5 FileInputStream (java.io.FileInputStream)5 GZIPInputStream (java.util.zip.GZIPInputStream)5 PMMLDecisionTreeTranslator (org.knime.base.node.mine.decisiontree2.PMMLDecisionTreeTranslator)5 PMMLPortObject (org.knime.core.node.port.pmml.PMMLPortObject)5 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)4 DecisionTreeNodeRenderer (org.knime.base.node.mine.decisiontree2.model.DecisionTreeNodeRenderer)4 DataColumnSpec (org.knime.core.data.DataColumnSpec)4 BufferedDataTable (org.knime.core.node.BufferedDataTable)4 ModelContentRO (org.knime.core.node.ModelContentRO)4 PMMLPortObjectSpec (org.knime.core.node.port.pmml.PMMLPortObjectSpec)4 DecisionTreeNodeLeaf (org.knime.base.node.mine.decisiontree2.model.DecisionTreeNodeLeaf)3