Relational decision trees: splits that learn conditions on related tables
A relational decision tree can ask questions about related rows, such as whether a customer has an express order above 100. When a node splits, the tree can choose an aggregation, conditions on its input rows and a threshold for sending the sample down one branch or the other.
The idea in one example
A tree that gets only max(amount) and count(express orders) has lost track of which order was both large and express. Two customers can have identical values for both features. If their largest purchases have different order types, they can still need different predictions.
A relational tree keeps the rows. At each node it considers questions of the form
primitive(table[condition & condition]) <= thresholdThe conditions inside the brackets select rows. The primitive summarizes those rows, and the threshold outside the brackets tests the result for the sample.
See it on data with a known rule
from sklearn.model_selection import train_test_split
from sklink.datasets import make_relational_classification
from sklink.inspection import export_text
from sklink.tree import RelationalDecisionTreeClassifier
db, X, y, rule = make_relational_classification(
n_entities=1000, signal="conditional", return_rule=True, random_state=0
)
print("rule:", rule)
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=0)
for max_conditions in [0, 1, 2]:
tree = RelationalDecisionTreeClassifier(
db,
entity="customers",
key="customer_id",
max_depth=3,
max_conditions=max_conditions,
random_state=0,
).fit(X_train, y_train)
print(f"max_conditions={max_conditions}: accuracy {tree.score(X_test, y_test):.3f}")
print(export_text(tree))rule: count(orders[amount > 100.0 & type == 'express']) >= 1
max_conditions=0: accuracy 0.652
max_conditions=1: accuracy 0.928
max_conditions=2: accuracy 0.996
|--- exists(orders[amount > 99.935 & type == 'express']) <= 0.500
| |--- class: 0
|--- exists(orders[amount > 99.935 & type == 'express']) > 0.500
| |--- class: 1The printed tree is the last one, with max_conditions=2, the default. max_conditions limits the filters inside one aggregation. With one condition the tree can ask about express orders or about large orders. With two, the example tree can check both on the same order. exists returns 0 when no order qualifies and 1 when one does, so a split at 0.5 separates those cases.
How the search stays fast
The tree limits the candidates it tests and shares computations.
- Binning. Numeric columns are cut at up to
max_condition_binscandidate thresholds, refined around the best one; categorical columns try theirmax_categoriesmost frequent values, and columns with more categories also try sets of categories ranked once infitby a smoothed mean target, so that rare categories rank in the middle (in regression and binary classification). - Decomposable aggregates. Learned conditions are combined with primitives that can be computed from running sums, such as
count,sum,mean,existsandproportion, so that many thresholds are scored in one pass. - Shared candidates. In a random forest, candidate expressions are evaluated and binned once and shared by all the trees.
Tuning in a few lines
The parameters that matter most are max_depth, max_conditions and min_samples_leaf. They are ordinary hyperparameters; this search tunes the first two:
from sklearn.model_selection import GridSearchCV
search = GridSearchCV(
RelationalDecisionTreeClassifier(
db, entity="customers", key="customer_id", random_state=0
),
{"max_depth": [2, 3], "max_conditions": [1, 2]},
cv=3,
).fit(X_train, y_train)
print(search.best_params_, f"{search.score(X_test, y_test):.3f}"){'max_conditions': 2, 'max_depth': 2} 0.996You can also fit a relational random forest, which averages trees, or relational gradient boosting, which adds trees that each correct the previous ones. You can still inspect a rule in an individual tree.
Where this comes from
Relational trees are not new: TILDE and multi-relational decision trees were studied in inductive logic programming in the late 1990s, and getML offers condition learning in its paid editions. scikit-link brings the idea to the scikit-learn API, with readable canonical names, missing value handling and leakage-safe fitting.
Learn more
- Relational decision trees tutorial, for choosing split conditions and tracing a customer's path.
- What is relational machine learning?
- How to train a scikit-learn model on multiple tables