Search in sources :

Example 1 with PMMLSimplePredicate

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

the class RuleSetToTable method handleSurrogate.

/**
 * (This is a recursive method.)
 *
 * @param cp A SURROGATE {@link PMMLCompoundPredicate}.
 * @param predicates The predicates to be converted.
 * @param usePrecedence Should we simplify the condition?
 * @param parentOperator The parent operator's (logical connective) type, used for precedence, can be {@code null}.
 * @param types The type of input columns.
 * @return Converted {@code cp}.
 * @throws IllegalStateException If cannot be transformed.
 */
private static String handleSurrogate(final PMMLCompoundPredicate cp, final List<PMMLPredicate> predicates, final boolean usePrecedence, final PMMLBooleanOperator parentOperator, final Map<String, DataType> types) {
    // surrogate(a, b) = if not missing(a) then rel(a) else b = ((NOT MISSING a) AND rel(a)) OR ((MISSING a) AND b)
    // surrogate(a, surrogate(b, c)) = if not missing(a) then rel(a) else if not missing(b) then rel(b) else c =
    // ((NOT MISSING a) AND rel(a)) OR ((MISSING a) AND (((NOT MISSING b) AND rel(b)) OR ((MISSING b) AND rel(c))))
    PMMLPredicate first = predicates.get(0);
    List<PMMLPredicate> rest = predicates.subList(1, predicates.size());
    if (predicates.size() == 1) {
        return convertToStringPrecedence(first, usePrecedence, PMMLBooleanOperator.AND, types);
    }
    CheckUtils.checkState(first instanceof PMMLTruePredicate || first instanceof PMMLFalsePredicate || first instanceof PMMLSimplePredicate || first instanceof PMMLSimpleSetPredicate, "Compound predicates are not supported by the SURROGATE transformation: " + first + " in\n" + cp);
    if (first instanceof PMMLFalsePredicate || first instanceof PMMLTruePredicate) {
        return convertToString(first, usePrecedence, types);
    }
    if (first instanceof PMMLSimplePredicate || first instanceof PMMLSimpleSetPredicate) {
        return parentheses(!usePrecedence || (parentOperator != null && parentOperator != PMMLBooleanOperator.OR), parentheses(!usePrecedence, /*OR is outside of this AND*/
        "NOT MISSING " + dollars(first.getSplitAttribute()) + " AND " + convertToStringPrecedence(first, usePrecedence, PMMLBooleanOperator.AND, types)) + " OR " + parentheses(!usePrecedence, /*OR is outside of this AND*/
        "MISSING " + dollars(first.getSplitAttribute()) + " AND " + handleSurrogate(cp, rest, usePrecedence, PMMLBooleanOperator.AND, types)));
    }
    throw new IllegalStateException("Compound predicates are not supported at this position: " + first + " in\n" + cp);
}
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) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) PMMLFalsePredicate(org.knime.base.node.mine.decisiontree2.PMMLFalsePredicate)

Example 2 with PMMLSimplePredicate

use of org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate 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);
    }
}
Also used : PMMLDocument(org.dmg.pmml.PMMLDocument) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) BooleanOperator(org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate.BooleanOperator) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) PMMLMiningSchemaTranslator(org.knime.core.node.port.pmml.PMMLMiningSchemaTranslator) 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) RuleSetToTable(org.knime.base.node.rules.engine.totable.RuleSetToTable) PMMLSimpleSetPredicate(org.knime.base.node.mine.decisiontree2.PMMLSimpleSetPredicate) RoundingMode(java.math.RoundingMode) PortType(org.knime.core.node.port.PortType) SimplePredicate(org.dmg.pmml.SimplePredicateDocument.SimplePredicate) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) SimpleRule(org.dmg.pmml.SimpleRuleDocument.SimpleRule) SimpleSetPredicate(org.dmg.pmml.SimpleSetPredicateDocument.SimpleSetPredicate) CompoundPredicate(org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate) MathContext(java.math.MathContext) NodeModel(org.knime.core.node.NodeModel) DecisionTreeNode(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode) List(java.util.List) BufferedDataTable(org.knime.core.node.BufferedDataTable) PMMLTruePredicate(org.knime.base.node.mine.decisiontree2.PMMLTruePredicate) PMMLDataDictionaryTranslator(org.knime.core.node.port.pmml.PMMLDataDictionaryTranslator) Entry(java.util.Map.Entry) PortObject(org.knime.core.node.port.PortObject) DecisionTreeNodeSplitPMML(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNodeSplitPMML) MININGFUNCTION(org.dmg.pmml.MININGFUNCTION) RuleSetModel(org.dmg.pmml.RuleSetModelDocument.RuleSetModel) PMMLOperator(org.knime.base.node.mine.decisiontree2.PMMLOperator) Criterion(org.dmg.pmml.RuleSelectionMethodDocument.RuleSelectionMethod.Criterion) RuleSet(org.dmg.pmml.RuleSetDocument.RuleSet) PMMLDecisionTreeTranslator(org.knime.base.node.mine.decisiontree2.PMMLDecisionTreeTranslator) Enum(org.dmg.pmml.SimplePredicateDocument.SimplePredicate.Operator.Enum) ArrayList(java.util.ArrayList) ExecutionContext(org.knime.core.node.ExecutionContext) PMMLPortObjectSpec(org.knime.core.node.port.pmml.PMMLPortObjectSpec) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) DataCell(org.knime.core.data.DataCell) PMMLPortObject(org.knime.core.node.port.pmml.PMMLPortObject) PMMLPortObjectSpecCreator(org.knime.core.node.port.pmml.PMMLPortObjectSpecCreator) PMMLPredicateTranslator(org.knime.base.node.mine.decisiontree2.PMMLPredicateTranslator) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) IOException(java.io.IOException) File(java.io.File) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) DecisionTree(org.knime.base.node.mine.decisiontree2.model.DecisionTree) ScoreDistribution(org.dmg.pmml.ScoreDistributionDocument.ScoreDistribution) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) PMMLFalsePredicate(org.knime.base.node.mine.decisiontree2.PMMLFalsePredicate) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) PMMLSimplePredicate(org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate) SimplePredicate(org.dmg.pmml.SimplePredicateDocument.SimplePredicate) PMMLCompoundPredicate(org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate) PMMLTruePredicate(org.knime.base.node.mine.decisiontree2.PMMLTruePredicate) ScoreDistribution(org.dmg.pmml.ScoreDistributionDocument.ScoreDistribution) SimpleRule(org.dmg.pmml.SimpleRuleDocument.SimpleRule) PMMLSimpleSetPredicate(org.knime.base.node.mine.decisiontree2.PMMLSimpleSetPredicate) DataCell(org.knime.core.data.DataCell) PMMLSimplePredicate(org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate) PMMLCompoundPredicate(org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate) CompoundPredicate(org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate) DecisionTreeNode(org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode)

Example 3 with PMMLSimplePredicate

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

the class FromDecisionTreeNodeModel method copy.

/**
 * Copies the {@code predicate} to {@code compound}.
 *
 * @param predicate A PMML xml object for {@link CompoundPredicate}
 * @param compund A KNIME domain object for {@link PMMLCompoundPredicate}.
 */
private void copy(final CompoundPredicate predicate, final PMMLCompoundPredicate compound) {
    PMMLPredicateTranslator.exportTo(compound, predicate);
    predicate.setBooleanOperator(PMMLPredicateTranslator.getOperator(compound.getBooleanOperator()));
    for (PMMLPredicate rawPredicate : compound.getPredicates()) {
        if (rawPredicate instanceof PMMLSimplePredicate) {
            PMMLSimplePredicate sp = (PMMLSimplePredicate) rawPredicate;
            copy(predicate.addNewSimplePredicate(), sp);
        } else if (rawPredicate instanceof PMMLCompoundPredicate) {
            PMMLCompoundPredicate cp = (PMMLCompoundPredicate) rawPredicate;
            copy(predicate.addNewCompoundPredicate(), cp);
        } else if (rawPredicate instanceof PMMLSimpleSetPredicate) {
            PMMLSimpleSetPredicate ssp = (PMMLSimpleSetPredicate) rawPredicate;
            copy(predicate.addNewSimpleSetPredicate(), ssp);
        } else if (rawPredicate instanceof PMMLTruePredicate) {
            predicate.addNewTrue();
        } else if (rawPredicate instanceof PMMLFalsePredicate) {
            predicate.addNewFalse();
        }
    }
}
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) PMMLPredicate(org.knime.base.node.mine.decisiontree2.PMMLPredicate) PMMLFalsePredicate(org.knime.base.node.mine.decisiontree2.PMMLFalsePredicate) PMMLCompoundPredicate(org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate)

Example 4 with PMMLSimplePredicate

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

the class PMMLExpressionFactory method compare.

/**
 * {@inheritDoc}
 */
@Override
public PMMLPredicate compare(final Expression left, final Expression right, final DataValueComparator cmp, final int... possibleValues) {
    final PMMLOperator op;
    if (Arrays.equals(possibleValues, LT)) {
        op = PMMLOperator.LESS_THAN;
    } else if (Arrays.equals(possibleValues, LE)) {
        op = PMMLOperator.LESS_OR_EQUAL;
    } else if (Arrays.equals(possibleValues, GT)) {
        op = PMMLOperator.GREATER_THAN;
    } else if (Arrays.equals(possibleValues, GE)) {
        op = PMMLOperator.GREATER_OR_EQUAL;
    } else if (Arrays.equals(possibleValues, EQ)) {
        op = PMMLOperator.EQUAL;
    } else {
        throw new IllegalStateException("" + Arrays.toString(possibleValues));
    }
    handleSameType(left, right);
    if (left.getTreeType() == ASTType.ColRef) {
        m_usedColumns.add(expressionToString(left));
    }
    if (right.getTreeType() == ASTType.ColRef) {
        m_usedColumns.add(expressionToString(right));
        final PMMLOperator opposite;
        switch(op) {
            case EQUAL:
                opposite = op;
                break;
            case GREATER_OR_EQUAL:
                opposite = PMMLOperator.LESS_OR_EQUAL;
                break;
            case GREATER_THAN:
                opposite = PMMLOperator.LESS_THAN;
                break;
            case LESS_OR_EQUAL:
                opposite = PMMLOperator.GREATER_OR_EQUAL;
                break;
            case LESS_THAN:
                opposite = PMMLOperator.GREATER_THAN;
                break;
            default:
                throw new UnsupportedOperationException("Not supported: " + op);
        }
        return new PMMLSimplePredicate(expressionToString(right), opposite, expressionToString(left));
    }
    return new PMMLSimplePredicate(expressionToString(left), op, expressionToString(right));
}
Also used : PMMLOperator(org.knime.base.node.mine.decisiontree2.PMMLOperator) PMMLSimplePredicate(org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate)

Example 5 with PMMLSimplePredicate

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

the class PMMLRuleTranslator method setOperator.

/**
 * Sets the operator of {@code pred} based on the properties of {@code simple.}
 *
 * @param pred An xml {@link SimplePredicate}.
 * @param simple A {@link PMMLSimplePredicate}.
 */
private void setOperator(final SimplePredicate pred, final PMMLSimplePredicate simple) {
    PMMLOperator x = simple.getOperator();
    Enum e = PMMLPredicateTranslator.getOperator(x);
    if (e == null) {
        throw new UnsupportedOperationException("Unknown operator: " + x);
    }
    pred.setOperator(e);
}
Also used : Enum(org.dmg.pmml.SimplePredicateDocument.SimplePredicate.Operator.Enum) PMMLOperator(org.knime.base.node.mine.decisiontree2.PMMLOperator)

Aggregations

PMMLSimplePredicate (org.knime.base.node.mine.decisiontree2.PMMLSimplePredicate)17 PMMLCompoundPredicate (org.knime.base.node.mine.decisiontree2.PMMLCompoundPredicate)13 PMMLSimpleSetPredicate (org.knime.base.node.mine.decisiontree2.PMMLSimpleSetPredicate)12 PMMLPredicate (org.knime.base.node.mine.decisiontree2.PMMLPredicate)11 PMMLFalsePredicate (org.knime.base.node.mine.decisiontree2.PMMLFalsePredicate)8 PMMLTruePredicate (org.knime.base.node.mine.decisiontree2.PMMLTruePredicate)8 PMMLOperator (org.knime.base.node.mine.decisiontree2.PMMLOperator)7 CompoundPredicate (org.dmg.pmml.CompoundPredicateDocument.CompoundPredicate)5 SimplePredicate (org.dmg.pmml.SimplePredicateDocument.SimplePredicate)4 SimpleSetPredicate (org.dmg.pmml.SimpleSetPredicateDocument.SimpleSetPredicate)4 PMMLBooleanOperator (org.knime.base.node.mine.decisiontree2.PMMLBooleanOperator)4 DecisionTreeNode (org.knime.base.node.mine.decisiontree2.model.DecisionTreeNode)4 DecisionTreeNodeSplitPMML (org.knime.base.node.mine.decisiontree2.model.DecisionTreeNodeSplitPMML)4 DataCell (org.knime.core.data.DataCell)4 ArrayList (java.util.ArrayList)3 Enum (org.dmg.pmml.SimplePredicateDocument.SimplePredicate.Operator.Enum)3 Test (org.junit.Test)3 DecisionTreeNodeLeaf (org.knime.base.node.mine.decisiontree2.model.DecisionTreeNodeLeaf)3 LinkedHashSet (java.util.LinkedHashSet)2 Entry (java.util.Map.Entry)2