use of org.knime.base.node.mine.decisiontree2.PMMLPredicate in project knime-core by knime.
the class PMMLRuleEditorNodeModel method createRearranger.
/**
* Creates the {@link ColumnRearranger} that can compute the new column.
*
* @param tableSpec The spec of the input table.
* @param ruleSet The {@link RuleSet} xml object where the rules should be added.
* @param parser The parser for the rules.
* @return The {@link ColumnRearranger}.
* @throws ParseException Problem during parsing.
* @throws InvalidSettingsException if settings are invalid
*/
private ColumnRearranger createRearranger(final DataTableSpec tableSpec, final RuleSet ruleSet, final PMMLRuleParser parser) throws ParseException, InvalidSettingsException {
if (m_settings.isAppendColumn() && m_settings.getNewColName().isEmpty()) {
throw new InvalidSettingsException("No name for prediction column provided");
}
Set<String> outcomes = new LinkedHashSet<String>();
List<DataType> outcomeTypes = new ArrayList<DataType>();
int line = 0;
final List<Pair<PMMLPredicate, Expression>> rules = new ArrayList<Pair<PMMLPredicate, Expression>>();
for (String ruleText : m_settings.rules()) {
++line;
if (RuleSupport.isComment(ruleText)) {
continue;
}
try {
ParseState state = new ParseState(ruleText);
PMMLPredicate expression = parser.parseBooleanExpression(state);
SimpleRule simpleRule = ruleSet.addNewSimpleRule();
setCondition(simpleRule, expression);
state.skipWS();
state.consumeText("=>");
state.skipWS();
Expression outcome = parser.parseOutcomeOperand(state, null);
// Only constants are allowed in the outcomes.
assert outcome.isConstant() : outcome;
rules.add(new Pair<PMMLPredicate, Expression>(expression, outcome));
outcomeTypes.add(outcome.getOutputType());
simpleRule.setScore(outcome.toString());
// simpleRule.setConfidence(confidenceForRule(simpleRule, line, ruleText));
simpleRule.setWeight(weightForRule(simpleRule, line, ruleText));
outcomes.add(simpleRule.getScore());
} catch (ParseException e) {
throw Util.addContext(e, ruleText, line);
}
}
DataType outcomeType = RuleEngineNodeModel.computeOutputType(outcomeTypes, true);
ColumnRearranger rearranger = new ColumnRearranger(tableSpec);
DataColumnSpecCreator specProto = new DataColumnSpecCreator(m_settings.isAppendColumn() ? DataTableSpec.getUniqueColumnName(tableSpec, m_settings.getNewColName()) : m_settings.getReplaceColumn(), outcomeType);
specProto.setDomain(new DataColumnDomainCreator(toCells(outcomes, outcomeType)).createDomain());
SingleCellFactory cellFactory = new SingleCellFactory(true, specProto.createSpec()) {
@Override
public DataCell getCell(final DataRow row) {
for (Pair<PMMLPredicate, Expression> pair : rules) {
if (pair.getFirst().evaluate(row, tableSpec) == Boolean.TRUE) {
return pair.getSecond().evaluate(row, null).getValue();
}
}
return DataType.getMissingCell();
}
};
if (m_settings.isAppendColumn()) {
rearranger.append(cellFactory);
} else {
rearranger.replace(cellFactory, m_settings.getReplaceColumn());
}
return rearranger;
}
use of org.knime.base.node.mine.decisiontree2.PMMLPredicate in project knime-core by knime.
the class PMMLRuleTranslator method setCompound.
/**
* Sets {@code cp}s xml content based on {@code compound}'s properties.
*
* @param cp An xml {@link CompoundPredicate}.
* @param compound A {@link PMMLCompoundPredicate}.
*/
private void setCompound(final CompoundPredicate cp, final PMMLCompoundPredicate compound) {
PMMLBooleanOperator op = compound.getBooleanOperator();
org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate.BooleanOperator.Enum boolOp = PMMLPredicateTranslator.getOperator(op);
if (boolOp == null) {
throw new UnsupportedOperationException("Not supported: " + op);
}
cp.setBooleanOperator(boolOp);
for (PMMLPredicate pp : compound.getPredicates()) {
setPredicate(cp, pp);
}
}
use of org.knime.base.node.mine.decisiontree2.PMMLPredicate in project knime-core by knime.
the class PMMLRuleTranslator method createRule.
/**
* Converts an xml {@link SimpleRule} to {@link Rule}.
*
* @param r An xml {@link SimpleRule}.
* @return The corresponding {@link Rule} object.
*/
private Rule createRule(final SimpleRule r) {
PMMLPredicate pred;
if (r.getTrue() != null) {
pred = new PMMLTruePredicate();
} else if (r.getFalse() != null) {
pred = new PMMLFalsePredicate();
} else if (r.getCompoundPredicate() != null) {
CompoundPredicate c = r.getCompoundPredicate();
pred = parseCompoundPredicate(c);
} else if (r.getSimplePredicate() != null) {
pred = parseSimplePredicate(r.getSimplePredicate());
} else if (r.getSimpleSetPredicate() != null) {
pred = parseSimpleSetPredicate(r.getSimpleSetPredicate());
} else {
throw new UnsupportedOperationException(r.toString());
}
final Map<String, ScoreProbabilityAndRecordCount> scores = r.getScoreDistributionList().stream().map(sd -> Pair.create(sd.getValue(), new ScoreProbabilityAndRecordCount(sd.isSetProbability() ? sd.getProbability() : null, sd.getRecordCount()))).collect(Collectors.toMap(Pair::getFirst, Pair::getSecond));
final Rule ret = new Rule(pred, r.getScore(), r.isSetWeight() ? r.getWeight() : null, r.isSetConfidence() ? r.getConfidence() : null, scores);
if (r.isSetNbCorrect()) {
ret.setNbCorrect(r.getNbCorrect());
}
if (r.isSetRecordCount()) {
ret.setRecordCount(r.getRecordCount());
}
return ret;
}
use of org.knime.base.node.mine.decisiontree2.PMMLPredicate in project knime-core by knime.
the class TreeNodeRegression method createDecisionTreeNode.
/**
* @param metaData
* @return
*/
public DecisionTreeNode createDecisionTreeNode(final MutableInteger idGenerator, final TreeMetaData metaData) {
DataCell majorityCell = new StringCell(DoubleFormat.formatDouble(m_mean));
final int nrChildren = getNrChildren();
LinkedHashMap<DataCell, Double> distributionMap = new LinkedHashMap<DataCell, Double>();
distributionMap.put(majorityCell, m_totalSum);
if (nrChildren == 0) {
return new DecisionTreeNodeLeaf(idGenerator.inc(), majorityCell, distributionMap);
} else {
int id = idGenerator.inc();
DecisionTreeNode[] childNodes = new DecisionTreeNode[nrChildren];
int splitAttributeIndex = getSplitAttributeIndex();
assert splitAttributeIndex >= 0 : "non-leaf node has no split";
String splitAttribute = metaData.getAttributeMetaData(splitAttributeIndex).getAttributeName();
PMMLPredicate[] childPredicates = new PMMLPredicate[nrChildren];
for (int i = 0; i < nrChildren; i++) {
final TreeNodeRegression treeNode = getChild(i);
TreeNodeCondition cond = treeNode.getCondition();
childPredicates[i] = cond.toPMMLPredicate();
childNodes[i] = treeNode.createDecisionTreeNode(idGenerator, metaData);
}
return new DecisionTreeNodeSplitPMML(id, majorityCell, distributionMap, splitAttribute, childPredicates, childNodes);
}
}
use of org.knime.base.node.mine.decisiontree2.PMMLPredicate in project knime-core by knime.
the class ConditionExporter method setValuesFromPMMLCompoundPredicate.
private void setValuesFromPMMLCompoundPredicate(final CompoundPredicate to, final PMMLCompoundPredicate from) {
final PMMLBooleanOperator boolOp = from.getBooleanOperator();
switch(boolOp) {
case AND:
to.setBooleanOperator(CompoundPredicate.BooleanOperator.AND);
break;
case OR:
to.setBooleanOperator(CompoundPredicate.BooleanOperator.OR);
break;
case SURROGATE:
to.setBooleanOperator(CompoundPredicate.BooleanOperator.SURROGATE);
break;
case XOR:
to.setBooleanOperator(CompoundPredicate.BooleanOperator.XOR);
break;
default:
throw new IllegalStateException("Unknown boolean predicate \"" + boolOp + "\".");
}
final List<PMMLPredicate> predicates = from.getPredicates();
for (final PMMLPredicate predicate : predicates) {
if (predicate instanceof PMMLSimplePredicate) {
setValuesFromPMMLSimplePredicate(to.addNewSimplePredicate(), (PMMLSimplePredicate) predicate);
} else if (predicate instanceof PMMLSimpleSetPredicate) {
setValuesFromPMMLSimpleSetPredicate(to.addNewSimpleSetPredicate(), (PMMLSimpleSetPredicate) predicate);
} else if (predicate instanceof PMMLTruePredicate) {
to.addNewTrue();
} else if (predicate instanceof PMMLFalsePredicate) {
to.addNewFalse();
} else if (predicate instanceof PMMLCompoundPredicate) {
final CompoundPredicate compound = to.addNewCompoundPredicate();
final PMMLCompoundPredicate knimeCompound = (PMMLCompoundPredicate) predicate;
setValuesFromPMMLCompoundPredicate(compound, knimeCompound);
} else {
throw new IllegalStateException("Unknown predicate type \"" + predicate + "\".");
}
}
}
Aggregations