---
url: /relational-machine-learning.md
description: >-
  Relational machine learning trains models on tables linked by keys, such as
  customers and their orders, not one flat table. A primer for scikit-learn
  users.
---

# What is relational machine learning?

**Relational machine learning uses records from tables linked by keys to make
a prediction for each sample.** For a customer, the model can use the
customer's country together with the amounts and types of their orders.

## From related records to one feature row {#why-one-table-is-not-enough}

A model that predicts once per customer needs one feature row per customer.
The source tables describe different things, so they have different numbers
of rows linked to that customer.

| Table | Rows per customer | Example columns |
|---|---|---|
| `customers` | 1 | `customer_id`, `country`, `signup_channel` |
| `orders` | 0 to hundreds | `customer_id`, `amount`, `type` |
| `order_items` | many per order | `order_id`, `product_id`, `quantity` |
| `products` | 0 or more through order items, shared across customers | `product_id`, `category`, `price` |

A model that only sees `customers` can use `country` but has no order history.
An aggregation can add that history as a feature, such as the number of express
orders or the mean order amount. A direct join to `orders` would repeat the
customer row and its label for every order. Before training a tabular model,
summarize those orders at the level you predict for.

## The three main approaches

1. **Propositionalization (flattening).** Compute aggregations along each
   relationship, such as `count`, `mean`, `mode` or `nunique`, and join them
   into one row per sample. Use those features to fit a tabular model. The
   choice of features determines which information the model gets. For
   example, the number of express orders and the largest order amount leave
   out whether the largest order was express.
2. **Relational learners.** Models whose splits or rules are themselves
   relational, like first-order decision trees (TILDE), multi-relational
   decision trees and first-order random forests. A split can learn to check
   whether a customer placed an express order over 100. The fitted rule names
   the rows and values it uses.
3. **Relational deep learning.** Graph neural networks and transformers that
   treat rows as nodes and foreign-key links as edges. These models learn
   numerical representations from the graph. Explaining a prediction requires
   tools that can work with those representations. See task-level
   scores and the protocols behind them on the [benchmarks page](./benchmarks).

## The main pitfall: leakage

Information can leak through the training split or the records used to
compute a feature.

* **Between folds.** When the training step chooses categories, thresholds or
  features, it can learn from held-out customers if it sees their rows first.
  Fit those choices inside each training fold. A fixed per-customer count can
  be computed in advance if it uses only records available for that sample.
* **Across time.** If a sample uses orders placed after the prediction
  cutoff, its evaluation score comes from information the model would lack
  when making that prediction.

The split defines which samples may teach the model. The cutoff defines which
timestamped records each sample can see. A temporal task usually needs both.

## Where scikit-link fits

scikit-link covers the first two approaches with the scikit-learn API:

* `RelationalFeaturizer` produces a table of features for the samples in `X`.
  Each column has a name such as `mean(orders.amount)`.
* Relational decision trees, random forests and gradient boosting learn
  conditions across tables directly.
* Relational k-means and clustering trees group entities without a label,
  and a relational isolation forest finds unusual ones.
* Both fit within scikit-learn cross-validation. They learn from rows reachable
  from the training samples and use per-sample cutoffs when configured.

scikit-link is in early development. See the [roadmap](./roadmap) and the
[comparison with other libraries](./compare/).

## Further reading

* [How to train a scikit-learn model on multiple tables](./blog/scikit-learn-multiple-tables),
  a step-by-step tutorial from `groupby` to a relational tree.
* [Relational decision trees](./blog/relational-decision-trees): how splits
  learn conditions on related tables.
* [Data leakage in multi-table feature engineering](./blog/leakage-multi-table-features),
  measured on a dataset where the future contradicts the past.
* [8 relational feature recipes for Python](./blog/relational-feature-recipes).
* The [documentation tutorials](https://scikit-link.rodrigo-arenas.com/stable/tutorials/),
  from installation to random forests and custom primitives.

## Glossary

The [glossary](./glossary) defines these and more terms, each with a link you
can cite.

* **Entity table**. One row per entity, such as `customers`.
* **Population table (`X`)**. One row per sample. Each row names its entity
  and can carry a prediction cutoff. Several samples can refer to the same
  entity.
* **Relationship**. A foreign key from a child table to a parent table, such
  as `orders.customer_id → customers.customer_id`.
* **Aggregation**. A function that summarizes related rows for a sample,
  such as `count(orders)` or `mean(orders.amount)`.
* **Cutoff time**. The point in time a prediction represents. scikit-link
  excludes timestamped rows at or after the cutoff by default.
