pulsatrix 1.1.0
Loading...
Searching...
No Matches
rule_extraction.hpp File Reference

Rules from trees, run by the Datalog engine (roadmap KD-3): a surrogate or distilled tree becomes a Datalog program that classifies the same way. More...

#include <cstdint>
#include <string>
#include <vector>
#include "pulsatrix/datalog_fact_database.hpp"
#include "pulsatrix/datalog_rule.hpp"
#include "pulsatrix/datalog_weighted_fact_database.hpp"
#include "pulsatrix/json.hpp"
#include "pulsatrix/soft_decision_tree.hpp"
#include "pulsatrix/surrogate_tree.hpp"
Include dependency graph for rule_extraction.hpp:

Go to the source code of this file.

Classes

struct  pulsatrix::RuleCondition
 One test on a path: x[feature] > threshold when greater, x[feature] <= threshold otherwise. More...
 
struct  pulsatrix::ExtractedRule
 One leaf of a crisp tree as a rule. More...
 
struct  pulsatrix::TreeRules
 A crisp tree's rules, one per leaf. More...
 
struct  pulsatrix::SoftTreeRules
 A soft decision tree's rules, one per leaf, over weighted facts. More...
 

Namespaces

namespace  pulsatrix
 

Functions

TreeRules pulsatrix::ExtractRules (const SurrogateTree &tree, const std::vector< std::string > &feature_names={}, const std::vector< std::string > &class_names={})
 The rules of a classification tree, one per leaf, in the tree's node order.
 
std::vector< int64_t > pulsatrix::ClassifyWithRules (const TreeRules &rules, const std::vector< std::vector< float > > &inputs)
 Runs the rules on each input with the Datalog engine and returns the class whose predict fact it derives, or -1 if none.
 
SoftTreeRules pulsatrix::ExtractRules (const SoftDecisionTree &tree, const std::vector< std::string > &class_names={})
 The rules of a soft decision tree.
 
std::vector< double > pulsatrix::ClassProbabilitiesFromRules (const SoftTreeRules &rules, const SoftDecisionTree &tree, int64_t row)
 Each class's probability for row row of tree's last forward(), from the rules evaluated over the real semiring.
 
std::string pulsatrix::FormatRule (const datalog::Rule &rule)
 A rule in Datalog's usual text: head(A, b) :- body1(A), body2(A, c).
 
JsonValue pulsatrix::ToJson (const TreeRules &rules)
 The rules as a pulsatrix.tree_rules.v1 document: each rule's text, conditions, class, support and confidence.
 

Detailed Description

Rules from trees, run by the Datalog engine (roadmap KD-3): a surrogate or distilled tree becomes a Datalog program that classifies the same way.

Every root-to-leaf path of a tree is one rule. A crisp tree (FitDecisionTree's, or FitSurrogateTree's, distilled from a model) gives

predict(S, versicolor) :- sample(S), gt(S, petal_length, 2.45), le(S, petal_width, 1.75).

with the conditions on one feature merged to the tightest (two le on a feature keep the smaller threshold), and facts le(s, f, t) or gt(s, f, t) for each sample and each threshold the rules use. The Datalog engine's fixpoint then holds predict(s, c) for exactly the class the tree predicts.

A SoftDecisionTree gives one rule per leaf over weighted facts: right(s, n) with the probability of going right at node n, left(s, n) with its complement, and leaf(l, c) with leaf l's probability of class c:

predict(S, C) :- left(S, n0), right(S, n1), leaf(l1, C).

Evaluated over the real semiring, which sums over derivations the product of their facts' weights, predict(s, c) is Σ_ℓ P_ℓ(s) Q_ℓc: exactly the tree's class probability.

Datalog constants are strings, so thresholds are written with 9 significant digits, enough to tell any two floats apart; the facts are written the same way, so rules and facts match.