Search in sources :

Example 21 with DecisionTree

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

the class TreeEnsembleModel method createDecisionTree.

public DecisionTree createDecisionTree(final int modelIndex, final DataTable sampleForHiliting) {
    final DecisionTree result;
    if (m_metaData.isRegression()) {
        TreeModelRegression treeModel = getTreeModelRegression(modelIndex);
        result = treeModel.createDecisionTree(m_metaData);
    } else {
        TreeModelClassification treeModel = getTreeModelClassification(modelIndex);
        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)

Example 22 with DecisionTree

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

the class DecTreeNodeView2 method modelChanged.

/**
 * {@inheritDoc}
 */
@Override
protected void modelChanged() {
    NodeModel model = this.getNodeModel();
    DecisionTree dt = ((DecisionTreeLearnerNodeModel2) model).getDecisionTree();
    if (dt != null) {
        // set new model
        m_jTree.setModel(new DefaultTreeModel(dt.getRootNode()));
        // change default renderer
        m_jTree.setCellRenderer(new DecisionTreeNodeRenderer());
        // make sure no default height is assumed (the renderer's
        // preferred size should be used instead)
        m_jTree.setRowHeight(0);
        // retrieve HiLiteHandler from Input port
        m_hiLiteHdl = (((DecisionTreeLearnerNodeModel2) model).getInHiLiteHandler(DecisionTreeLearnerNodeModel2.DATA_INPORT));
        // and adjust menu entries for HiLite-ing
        m_hiLiteMenu.setEnabled(m_hiLiteHdl != null);
    } else {
        m_jTree.setModel(null);
    }
}
Also used : NodeModel(org.knime.core.node.NodeModel) DecisionTree(org.knime.base.node.mine.decisiontree2.model.DecisionTree) DecisionTreeNodeRenderer(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNodeRenderer) DefaultTreeModel(javax.swing.tree.DefaultTreeModel)

Example 23 with DecisionTree

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

the class PMMLDecisionTreeTranslator method parseDecTreeFromModel.

/**
 * Builds a decision tree object out of the TreeModel.
 * @param treeModel treeModel parsed from the PMML.
 *
 * @return DecisionTreeModel for further processing.
 */
public DecisionTree parseDecTreeFromModel(final TreeModel treeModel) {
    // --------------------------------------------
    // check the mining function, only classification is allowed
    final Enum functionName = CheckUtils.checkArgumentNotNull(treeModel.getFunctionName(), "Function name must not be null");
    if (MININGFUNCTION.CLASSIFICATION.equals(functionName)) {
        m_isClassification = true;
    } else if (MININGFUNCTION.REGRESSION.equals(functionName)) {
        m_isClassification = false;
    } else {
        throw new IllegalArgumentException("Unsupported function name \"" + functionName + "\"");
    }
    // --------------------------------------------
    // Find the predicted field from the mining schema
    MiningField[] miningFields = treeModel.getMiningSchema().getMiningFieldArray();
    String predictedField = "predictedField";
    for (MiningField mf : miningFields) {
        if (FIELDUSAGETYPE.PREDICTED == mf.getUsageType() || FIELDUSAGETYPE.TARGET == mf.getUsageType()) {
            predictedField = mf.getName();
            break;
        }
    }
    // ------------------------------------------------
    // Parse PMML nodes to KNIME nodes
    Node pmmlRoot = treeModel.getNode();
    DecisionTreeNode knimeRoot = addKnimeTreeNode(pmmlRoot);
    // ------------------------------------------------
    // parse no true child strategy
    PMMLNoTrueChildStrategy ntcStrategy = PMMLNoTrueChildStrategy.RETURN_NULL_PREDICTION;
    if (NOTRUECHILDSTRATEGY.RETURN_LAST_PREDICTION.equals(treeModel.getNoTrueChildStrategy())) {
        ntcStrategy = PMMLNoTrueChildStrategy.RETURN_LAST_PREDICTION;
    }
    // initialize a KNIME decision tree
    return new DecisionTree(knimeRoot, predictedField, MV_STRATEGY_TO_KNIME_MAP.get(treeModel.getMissingValueStrategy()), ntcStrategy);
}
Also used : Enum(org.dmg.pmml.MININGFUNCTION.Enum) MiningField(org.dmg.pmml.MiningFieldDocument.MiningField) DecisionTree(org.knime.base.node.mine.decisiontree2.model.DecisionTree) Node(org.dmg.pmml.NodeDocument.Node) DecisionTreeNode(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode) DecisionTreeNode(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode)

Example 24 with DecisionTree

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

the class DecTreeToImageNodeModel method loadInternals.

/**
 * Load internals.
 *
 * @param nodeInternDir The intern node directory to load tree from.
 * @param exec Used to report progress or cancel saving.
 * @throws IOException Always, since this method has not been implemented
 *             yet.
 * @see org.knime.core.node.NodeModel
 *      #loadInternals(java.io.File,ExecutionMonitor)
 */
@Override
protected void loadInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException {
    // read the decision tree
    File internalsFile = new File(nodeInternDir, DEC_TREE_FILE_NAME);
    if (!internalsFile.exists()) {
        // file to load internals from not available
        setWarningMessage("Internal model could not be loaded.");
        return;
    }
    BufferedInputStream in2 = new BufferedInputStream(new GZIPInputStream(new FileInputStream(internalsFile)));
    ModelContentRO binModel = ModelContent.loadFromXML(in2);
    try {
        m_decTree = new DecisionTree(binModel);
    } catch (InvalidSettingsException ise) {
        LOGGER.warn("Model (internals) could not be loaded.", ise);
        setWarningMessage("Internal model could not be loaded.");
    }
    exec.setProgress(0.5);
    // read image content
    File f = new File(nodeInternDir, IMAGE_FILE_NAME);
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(f));
    try {
        m_imageContent = new PNGImageContent(in);
    } catch (Exception e) {
        in.close();
        LOGGER.warn("Model (internals) could not be loaded.", e);
        setWarningMessage("Internal model could not be loaded.");
    }
    in.close();
    exec.setProgress(1.0);
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) PNGImageContent(org.knime.core.data.image.png.PNGImageContent) DecisionTree(org.knime.base.node.mine.decisiontree2.model.DecisionTree) ModelContentRO(org.knime.core.node.ModelContentRO) BufferedInputStream(java.io.BufferedInputStream) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) File(java.io.File) FileInputStream(java.io.FileInputStream) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 25 with DecisionTree

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

the class DecisionTreeLearnerNodeModel2 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)

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