use of org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode in project knime-core by knime.
the class FromDecisionTreeNodeModel method addRules.
/**
* Adds the rules to {@code rs} (recursively on each leaf).
*
* @param rs The output {@link RuleSet}.
* @param parents The parent stack.
* @param node The actual node.
*/
private void addRules(final RuleSet rs, final List<DecisionTreeNode> parents, final DecisionTreeNode node) {
if (node.isLeaf()) {
SimpleRule rule = rs.addNewSimpleRule();
if (m_rulesToTable.getScorePmmlRecordCount().getBooleanValue()) {
// This increases the PMML quite significantly
BigDecimal sum = BigDecimal.ZERO;
final MathContext mc = new MathContext(7, RoundingMode.HALF_EVEN);
final boolean computeProbability = m_rulesToTable.getScorePmmlProbability().getBooleanValue();
if (computeProbability) {
sum = new BigDecimal(node.getClassCounts().entrySet().stream().mapToDouble(e -> e.getValue().doubleValue()).sum(), mc);
}
for (final Entry<DataCell, Double> entry : node.getClassCounts().entrySet()) {
final ScoreDistribution scoreDistrib = rule.addNewScoreDistribution();
scoreDistrib.setValue(entry.getKey().toString());
scoreDistrib.setRecordCount(entry.getValue());
if (computeProbability) {
if (Double.compare(entry.getValue().doubleValue(), 0.0) == 0) {
scoreDistrib.setProbability(new BigDecimal(0.0));
} else {
scoreDistrib.setProbability(new BigDecimal(entry.getValue().doubleValue(), mc).divide(sum, mc));
}
}
}
}
CompoundPredicate and = rule.addNewCompoundPredicate();
and.setBooleanOperator(BooleanOperator.AND);
DecisionTreeNode n = node;
do {
PMMLPredicate pmmlPredicate = ((DecisionTreeNodeSplitPMML) n.getParent()).getSplitPred()[n.getParent().getIndex(n)];
if (pmmlPredicate instanceof PMMLSimplePredicate) {
PMMLSimplePredicate simple = (PMMLSimplePredicate) pmmlPredicate;
SimplePredicate predicate = and.addNewSimplePredicate();
copy(predicate, simple);
} else if (pmmlPredicate instanceof PMMLCompoundPredicate) {
PMMLCompoundPredicate compound = (PMMLCompoundPredicate) pmmlPredicate;
CompoundPredicate predicate = and.addNewCompoundPredicate();
copy(predicate, compound);
} else if (pmmlPredicate instanceof PMMLSimpleSetPredicate) {
PMMLSimpleSetPredicate simpleSet = (PMMLSimpleSetPredicate) pmmlPredicate;
copy(and.addNewSimpleSetPredicate(), simpleSet);
} else if (pmmlPredicate instanceof PMMLTruePredicate) {
and.addNewTrue();
} else if (pmmlPredicate instanceof PMMLFalsePredicate) {
and.addNewFalse();
}
n = n.getParent();
} while (n.getParent() != null);
// Simple fix for the case when a single condition was used.
while (and.getFalseList().size() + and.getCompoundPredicateList().size() + and.getSimplePredicateList().size() + and.getSimpleSetPredicateList().size() + and.getTrueList().size() < 2) {
and.addNewTrue();
}
if (m_rulesToTable.getProvideStatistics().getBooleanValue()) {
rule.setNbCorrect(node.getOwnClassCount());
rule.setRecordCount(node.getEntireClassCount());
}
rule.setScore(node.getMajorityClass().toString());
} else {
parents.add(node);
for (int i = 0; i < node.getChildCount(); ++i) {
addRules(rs, parents, node.getChildAt(i));
}
parents.remove(node);
}
}
use of org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode in project knime-core by knime.
the class FromDecisionTreeNodeModel method execute.
/**
* {@inheritDoc}
* @throws CanceledExecutionException Execution cancelled.
* @throws InvalidSettingsException No or more than one RuleSet model is in the PMML input.
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws CanceledExecutionException, InvalidSettingsException {
PMMLPortObject decTreeModel = (PMMLPortObject) inData[0];
PMMLDecisionTreeTranslator treeTranslator = new PMMLDecisionTreeTranslator();
decTreeModel.initializeModelTranslator(treeTranslator);
DecisionTree decisionTree = treeTranslator.getDecisionTree();
decisionTree.getRootNode();
PMMLPortObject ruleSetModel = new PMMLPortObject(decTreeModel.getSpec());
PMMLDocument document = PMMLDocument.Factory.newInstance();
PMML pmml = document.addNewPMML();
PMMLPortObjectSpec.writeHeader(pmml);
pmml.setVersion(PMMLPortObject.PMML_V4_2);
new PMMLDataDictionaryTranslator().exportTo(document, decTreeModel.getSpec());
RuleSetModel newRuleSetModel = pmml.addNewRuleSetModel();
PMMLMiningSchemaTranslator.writeMiningSchema(decTreeModel.getSpec(), newRuleSetModel);
newRuleSetModel.setFunctionName(MININGFUNCTION.CLASSIFICATION);
newRuleSetModel.setAlgorithmName("RuleSet");
RuleSet ruleSet = newRuleSetModel.addNewRuleSet();
ruleSet.addNewRuleSelectionMethod().setCriterion(Criterion.FIRST_HIT);
addRules(ruleSet, new ArrayList<DecisionTreeNode>(), decisionTree.getRootNode());
// TODO: Return a BufferedDataTable for each output port
PMMLPortObject pmmlPortObject = new PMMLPortObject(ruleSetModel.getSpec(), document);
return new PortObject[] { pmmlPortObject, new RuleSetToTable(m_rulesToTable).execute(exec, pmmlPortObject) };
}
use of org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode in project knime-core by knime.
the class DecTreePredictorNodeView method changeSelectedHiLite.
// /////////////////////////////
// routines for HiLite Support
// /////////////////////////////
/*
* hilite or unhilite all items that are covered by currently selected
* branches in the tree
*
* @param state if true hilite, otherwise unhilite selection
*/
private void changeSelectedHiLite(final boolean state) {
TreePath[] selectedPaths = m_jTree.getSelectionPaths();
if (selectedPaths == null) {
// nothing selected
return;
}
for (int i = 0; i < selectedPaths.length; i++) {
assert (selectedPaths[i] != null);
if (selectedPaths[i] == null) {
return;
}
TreePath path = selectedPaths[i];
Object lastNode = path.getLastPathComponent();
assert (lastNode != null);
assert (lastNode instanceof DecisionTreeNode);
Set<RowKey> covPat = ((DecisionTreeNode) lastNode).coveredPattern();
if (state) {
m_hiLiteHdl.fireHiLiteEvent(covPat);
} else {
m_hiLiteHdl.fireUnHiLiteEvent(covPat);
}
}
}
use of org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode in project knime-core by knime.
the class RegressionTreeLearnerNodeView method updateHiLite.
// ///////////////////////////////////////////////////
private void updateHiLite(final boolean state) {
DecisionTreeNode selected = m_graph.getSelected();
if (selected == null) {
return;
}
Set<RowKey> covPat = new HashSet<RowKey>();
covPat.addAll(selected.coveredPattern());
if (state) {
m_hiLiteHdl.fireHiLiteEvent(covPat);
} else {
m_hiLiteHdl.fireUnHiLiteEvent(covPat);
}
}
use of org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode in project knime-core by knime.
the class RegressionTreeLearnerNodeView method recreateHiLite.
private void recreateHiLite() {
HiLiteHandler handler = m_hiLiteHdl;
if (handler == null) {
return;
}
Set<RowKey> hilited = handler.getHiLitKeys();
Set<DecisionTreeNode> toHilite = new HashSet<DecisionTreeNode>();
DecisionTreeNode root = m_graph.getRootNode();
List<DecisionTreeNode> toProcess = new LinkedList<DecisionTreeNode>();
if (null != root) {
toProcess.add(0, root);
}
// Traverse the tree breadth first
while (!toProcess.isEmpty()) {
DecisionTreeNode curr = toProcess.remove(0);
// cover any pattern
if (curr.coveredPattern().isEmpty()) {
continue;
}
if (hilited.containsAll(curr.coveredPattern())) {
// hilite subtree starting from curr
toHilite.addAll(getSubtree(curr));
} else {
for (int i = 0; i < curr.getChildCount(); i++) {
toProcess.add(0, curr.getChildAt(i));
}
}
}
m_graph.hiLite(toHilite);
}
Aggregations