Search in sources :

Example 6 with PMMLPredicate

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) ArrayList(java.util.ArrayList) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) ParseState(org.knime.base.node.rules.engine.BaseRuleParser.ParseState) DataRow(org.knime.core.data.DataRow) SimpleRule(org.dmg.pmml.SimpleRuleDocument.SimpleRule) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) Expression(org.knime.base.node.rules.engine.Expression) DataType(org.knime.core.data.DataType) ParseException(java.text.ParseException) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) Pair(org.knime.core.util.Pair)

Example 7 with PMMLPredicate

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);
    }
}
Also used : PMMLBooleanOperator(org.knime.base.node.mine.decisiontree2.PMMLBooleanOperator) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) PMMLBooleanOperator(org.knime.base.node.mine.decisiontree2.PMMLBooleanOperator)

Example 8 with PMMLPredicate

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;
}
Also used : PMMLTruePredicate(org.knime.base.node.mine.decisiontree2.PMMLTruePredicate) AbstractCellFactory(org.knime.core.data.container.AbstractCellFactory) PMMLDocument(org.dmg.pmml.PMMLDocument) CompoundRule(org.dmg.pmml.CompoundRuleDocument.CompoundRule) PMMLMiningSchemaTranslator(org.knime.core.node.port.pmml.PMMLMiningSchemaTranslator) SimpleRuleDocument(org.dmg.pmml.SimpleRuleDocument) PMMLCompoundPredicate(org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate) PMMLSimplePredicate(org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate) BigDecimal(java.math.BigDecimal) PMML(org.dmg.pmml.PMMLDocument.PMML) PMMLFalsePredicate(org.knime.base.node.mine.decisiontree2.PMMLFalsePredicate) PMMLBooleanOperator(org.knime.base.node.mine.decisiontree2.PMMLBooleanOperator) Map(java.util.Map) SchemaType(org.apache.xmlbeans.SchemaType) PMMLSimpleSetPredicate(org.knime.base.node.mine.decisiontree2.PMMLSimpleSetPredicate) SimplePredicate(org.dmg.pmml.SimplePredicateDocument.SimplePredicate) SimpleRule(org.dmg.pmml.SimpleRuleDocument.SimpleRule) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicateDocument.SimpleSetPredicate) CompoundPredicate(org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate) Collectors(java.util.stream.Collectors) Value(org.dmg.pmml.ValueDocument.Value) List(java.util.List) DerivedFieldMapper(org.knime.core.node.port.pmml.preproc.DerivedFieldMapper) PMMLTruePredicate(org.knime.base.node.mine.decisiontree2.PMMLTruePredicate) RuleSelectionMethod(org.dmg.pmml.RuleSelectionMethodDocument.RuleSelectionMethod) Entry(java.util.Map.Entry) MININGFUNCTION(org.dmg.pmml.MININGFUNCTION) RuleSetModel(org.dmg.pmml.RuleSetModelDocument.RuleSetModel) PMMLTranslator(org.knime.core.node.port.pmml.PMMLTranslator) PMMLOperator(org.knime.base.node.mine.decisiontree2.PMMLOperator) XmlCursor(org.apache.xmlbeans.XmlCursor) Criterion(org.dmg.pmml.RuleSelectionMethodDocument.RuleSelectionMethod.Criterion) CompoundRuleDocument(org.dmg.pmml.CompoundRuleDocument) RuleSet(org.dmg.pmml.RuleSetDocument.RuleSet) HashMap(java.util.HashMap) Enum(org.dmg.pmml.SimplePredicateDocument.SimplePredicate.Operator.Enum) ArrayList(java.util.ArrayList) Pair(org.knime.core.util.Pair) LinkedHashMap(java.util.LinkedHashMap) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) XmlObject(org.apache.xmlbeans.XmlObject) DataField(org.dmg.pmml.DataFieldDocument.DataField) LinkedList(java.util.LinkedList) PMMLPredicateTranslator(org.knime.base.node.mine.decisiontree2.PMMLPredicateTranslator) PMMLConditionTranslator(org.knime.base.node.mine.decisiontree2.PMMLConditionTranslator) DataDictionary(org.dmg.pmml.DataDictionaryDocument.DataDictionary) BitSet(java.util.BitSet) Collections(java.util.Collections) ScoreDistribution(org.dmg.pmml.ScoreDistributionDocument.ScoreDistribution) PMMLCompoundPredicate(org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate) CompoundPredicate(org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) CompoundRule(org.dmg.pmml.CompoundRuleDocument.CompoundRule) SimpleRule(org.dmg.pmml.SimpleRuleDocument.SimpleRule) PMMLFalsePredicate(org.knime.base.node.mine.decisiontree2.PMMLFalsePredicate)

Example 9 with PMMLPredicate

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);
    }
}
Also used : PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) LinkedHashMap(java.util.LinkedHashMap) DecisionTreeNodeLeaf(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNodeLeaf) StringCell(org.knime.core.data.def.StringCell) DecisionTreeNodeSplitPMML(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNodeSplitPMML) DataCell(org.knime.core.data.DataCell) DecisionTreeNode(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode)

Example 10 with PMMLPredicate

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 + "\".");
        }
    }
}
Also used : PMMLTruePredicate(org.knime.base.node.mine.decisiontree2.PMMLTruePredicate) PMMLSimpleSetPredicate(org.knime.base.node.mine.decisiontree2.PMMLSimpleSetPredicate) PMMLSimplePredicate(org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate) PMMLCompoundPredicate(org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate) CompoundPredicate(org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) PMMLFalsePredicate(org.knime.base.node.mine.decisiontree2.PMMLFalsePredicate) PMMLBooleanOperator(org.knime.base.node.mine.decisiontree2.PMMLBooleanOperator) PMMLCompoundPredicate(org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate)

Aggregations

PMMLPredicate (org.knime.base.node.mine.decisiontree2.PMMLPredicate)26 PMMLSimplePredicate (org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate)19 PMMLCompoundPredicate (org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate)17 PMMLSimpleSetPredicate (org.knime.base.node.mine.decisiontree2.PMMLSimpleSetPredicate)14 PMMLFalsePredicate (org.knime.base.node.mine.decisiontree2.PMMLFalsePredicate)10 PMMLTruePredicate (org.knime.base.node.mine.decisiontree2.PMMLTruePredicate)10 DecisionTreeNode (org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode)9 DecisionTreeNodeSplitPMML (org.knime.base.node.mine.decisiontree2.model.DecisionTreeNodeSplitPMML)9 DataCell (org.knime.core.data.DataCell)9 ArrayList (java.util.ArrayList)8 DecisionTreeNodeLeaf (org.knime.base.node.mine.decisiontree2.model.DecisionTreeNodeLeaf)8 CompoundPredicate (org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate)7 SimplePredicate (org.dmg.pmml.SimplePredicateDocument.SimplePredicate)6 SimpleRule (org.dmg.pmml.SimpleRuleDocument.SimpleRule)6 LinkedHashMap (java.util.LinkedHashMap)5 SimpleSetPredicate (org.dmg.pmml.SimpleSetPredicateDocument.SimpleSetPredicate)5 PMMLBooleanOperator (org.knime.base.node.mine.decisiontree2.PMMLBooleanOperator)4 PMMLOperator (org.knime.base.node.mine.decisiontree2.PMMLOperator)4 LinkedHashSet (java.util.LinkedHashSet)3 Entry (java.util.Map.Entry)3