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;
}
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);
}
}
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);
}
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);
}
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.");
}
}
Aggregations