Metadata-Version: 2.5
Name: scikit-link
Version: 0.1.0
Summary: Machine learning on multiple related tables, with the scikit-learn API: relational features, decision trees, random forests, gradient boosting and clustering
Project-URL: Homepage, https://scikit-link.rodrigo-arenas.com/
Project-URL: Documentation, https://scikit-link.rodrigo-arenas.com/stable/
Project-URL: Source, https://github.com/rodrigo-arenas/scikit-link
Project-URL: Issues, https://github.com/rodrigo-arenas/scikit-link/issues
Project-URL: Changelog, https://scikit-link.rodrigo-arenas.com/stable/release-notes
Author-email: Rodrigo Arenas <rodrigo.arenas456@gmail.com>
License-Expression: MIT
License-File: LICENSE
Keywords: automated feature engineering,deep feature synthesis,feature engineering,featuretools,machine learning,multi-table,propositionalization,random forest,relational database,relational decision tree,relational learning,scikit-learn,tabular data
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Database
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.12
Requires-Dist: duckdb>=1.4
Requires-Dist: narwhals>=2.0
Requires-Dist: numba>=0.61; (sys_platform != 'darwin' or platform_machine == 'arm64') and python_version < '3.16'
Requires-Dist: numpy>=2.0
Requires-Dist: scikit-learn>=1.8
Requires-Dist: scipy>=1.13
Provides-Extra: pandas
Requires-Dist: pandas>=2.2; extra == 'pandas'
Provides-Extra: polars
Requires-Dist: polars>=1.30; extra == 'polars'
Provides-Extra: pyarrow
Requires-Dist: pyarrow>=18; extra == 'pyarrow'
Description-Content-Type: text/markdown

<h1 align="center">
  <picture><img src="https://raw.githubusercontent.com/rodrigo-arenas/scikit-link/main/docs/content/public/brand/atlas-latente.svg" width="36%" align="middle" alt=""></picture>
  <picture><img src="https://raw.githubusercontent.com/rodrigo-arenas/scikit-link/main/docs/content/public/brand/scikit-link-wordmark.svg" width="45%" align="middle" alt="scikit-link"></picture>
</h1>

<p align="center">
  <a href="https://pypi.org/project/scikit-link/"><img src="https://img.shields.io/pypi/v/scikit-link?color=e8704e" alt="PyPI version"></a>
  <a href="https://pypi.org/project/scikit-link/"><img src="https://img.shields.io/pypi/pyversions/scikit-link" alt="Python versions"></a>
  <a href="https://github.com/rodrigo-arenas/scikit-link/actions/workflows/ci.yml"><img src="https://github.com/rodrigo-arenas/scikit-link/actions/workflows/ci.yml/badge.svg?branch=main" alt="CI"></a>
  <a href="https://scikit-link.rodrigo-arenas.com/stable/"><img src="https://img.shields.io/badge/docs-stable-2f6f5e" alt="Documentation"></a>
  <a href="LICENSE"><img src="https://img.shields.io/pypi/l/scikit-link" alt="License: MIT"></a>
  <a href="https://github.com/astral-sh/ruff"><img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json" alt="Ruff"></a>
</p>

<p align="center"><strong>Machine learning on multiple related tables, with the scikit-learn API.</strong></p>

Plenty of data lives in several tables: a customer and their orders, a
patient and their visits, a machine and its sensor readings, an account and
its transactions. scikit-link learns from those related rows directly. Put
one entity key per row in `X`, describe the tables and their keys in a
`Database`, and fit estimators that follow the keys inside ordinary
scikit-learn pipelines and cross-validation.

<p align="center">
  <a href="https://scikit-link.rodrigo-arenas.com/relational-machine-learning"><img src="https://raw.githubusercontent.com/rodrigo-arenas/scikit-link/main/docs/assets/readme/how-it-works.svg" width="100%" alt="Drawn with sklink.plotting: a database of customers, orders, order items and products linked by keys, and a relational decision tree of depth two that first asks about each customer's express orders, then follows the orders to their products and asks about the share of items that are toys."></a>
</p>

<p align="center">
  <a href="https://scikit-link.rodrigo-arenas.com/">Website</a> ·
  <a href="https://scikit-link.rodrigo-arenas.com/stable/getting-started/quickstart">Quickstart</a> ·
  <a href="https://scikit-link.rodrigo-arenas.com/stable/">Documentation</a> ·
  <a href="https://scikit-link.rodrigo-arenas.com/stable/case-studies/">Case studies</a> ·
  <a href="https://scikit-link.rodrigo-arenas.com/benchmarks">Benchmarks</a> ·
  <a href="https://scikit-link.rodrigo-arenas.com/compare/">Comparison</a> ·
  <a href="https://scikit-link.rodrigo-arenas.com/roadmap">Roadmap</a>
</p>

## Why scikit-link

- **No hand-written joins and group-bys.** The estimators compute aggregates
  of the related rows themselves, such as `count(orders[type == 'express'])`,
  and relational trees learn which rows to count and with which conditions.
- **No leakage.** Every fold of a cross-validation learns its features from
  its own training rows, and a cutoff date per row of `X` hides the rows that
  happened after it.
- **Rules you can read.** A relational tree prints its splits as questions
  about the related tables, and `explain` shows what one prediction used.
- **Everything you know from scikit-learn.** `fit`, `predict`, `Pipeline`,
  `GridSearchCV`, ensembles and threshold tuning work as usual, and
  `RelationalFeaturizer` feeds any scikit-learn model, XGBoost or LightGBM.

## Install

scikit-link requires Python 3.12 or later. Install it from PyPI:

```bash
pip install "scikit-link[pandas]"
```

The extra installs pandas, which the examples use; `[polars]` and `[pyarrow]`
are the other table formats. Numba installs with the package and compiles the
tree loops, with the same models. The distribution is called `scikit-link` and
the import is `sklink`.

## Quickstart

[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/rodrigo-arenas/scikit-link/blob/main/examples/quickstart.ipynb)

```python
from sklearn.ensemble import HistGradientBoostingClassifier
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklink.datasets import make_relational_classification
from sklink.feature_extraction import RelationalFeaturizer
from sklink.inspection import export_text
from sklink.tree import RelationalDecisionTreeClassifier

# A Database of customers, orders, order items and products, and X with one
# row per customer. Build your own with sklink.Database(tables=...,
# primary_keys=..., relationships=[("orders.customer_id", "customers.customer_id")]).
db, X, likes_toys = make_relational_classification(
    1000, signal="multihop", random_state=0
)

# The label hides a rule across tables: customers who buy toys (order items
# and their products) and placed an express order above 100 (orders).
orders = db.table("orders")
big_express = orders.query("type == 'express' and amount > 100")["customer_id"]
y = (likes_toys & X["customer_id"].isin(big_express)).astype(int)

# 1. Relational features for any scikit-learn estimator.
pipe = make_pipeline(
    RelationalFeaturizer(db, entity="customers", key="customer_id"),
    HistGradientBoostingClassifier(),
)
print(f"accuracy {cross_val_score(pipe, X, y, cv=5).mean():.3f}")

# 2. A decision tree that learns conditions across tables.
tree = RelationalDecisionTreeClassifier(
    db, entity="customers", key="customer_id", max_depth=2, min_impurity_decrease=0.01
)
print(export_text(tree.fit(X, y)))
```

<!-- output -->
```text
accuracy 0.959
|--- exists(orders[amount > 100.55 & type == 'express']) <= 0.500
|   |--- class: 0
|--- exists(orders[amount > 100.55 & type == 'express']) >  0.500
|   |--- proportion(orders.order_items[products.category == 'toys']) <= 0.100
|   |   |--- class: 0
|   |--- proportion(orders.order_items[products.category == 'toys']) >  0.100
|   |   |--- class: 1
```

## What you can do

| Task | How it works |
|---|---|
| Turn related rows into features for any estimator | `RelationalFeaturizer` aggregates the related rows of each entity into columns, such as the number of visits or the mean transaction amount, one row per sample, for any scikit-learn model. |
| Classify or regress directly on related tables | Relational decision trees, random forests and gradient boosting learn which related rows to aggregate while they fit. A split can ask whether an entity has any related row above a learned threshold within a learned category. |
| Cluster entities or find anomalies, without labels | `RelationalKMeans` and `RelationalClusteringTree` group entities by their related rows; `RelationalIsolationForest` ranks the entities whose related rows look unlike anyone else's. |
| Prepare the tables with scikit-learn | `TransformedTablesEstimator` fits scikit-learn transformers, such as a text embedding or a scaler, on the related rows that the training samples reach, and the model learns from the transformed tables. |
| Predict at a point in time without leakage | Give each sample a cutoff date so only earlier rows are used, and validate on later periods with `TemporalCutoffSplit`. Every fold learns its categories and thresholds from its training samples only. |
| Explain a prediction | `sklink.inspection` traces the decision path of one sample, groups feature importances by table or column, and exports fitted rules as text or JSON. |
| Plot the schema and the model | `sklink.plotting` draws the tables and keys, a fitted tree, importances and the path of one prediction, as in the gallery below. |
| Use the tables you already have | A `Database` takes pandas, Polars or PyArrow tables, dictionaries of NumPy columns, or Parquet file paths, read a few columns at a time to keep memory low on large tables. Composite keys, several levels of relationships and dated rows are supported. |
| Add your own aggregations | Register a custom primitive, such as the spread between the largest and smallest value, and request it by name. |
| Save and deploy a model | `sklink.detach` drops the tables before pickling, and `sklink.rebind` attaches fresh tables to score new entities. |
| Fit faster | The tree loops are compiled with Numba, which installs with the package, and `n_jobs` grows forest trees in parallel, with the same fitted model. |

The examples use a small shop, with customers, orders, order items and
products, because it is easy to follow; the same code applies to any tables
linked by keys.

## Plot gallery

These four `sklink.plotting` displays use the customers and orders from
`sklink.datasets.load_shop()`. The target marks customers who placed at least
three orders. Select a figure for the full-size view or see how to create it
in the [plotting reference](https://scikit-link.rodrigo-arenas.com/stable/api/sklink.plotting).

<table>
  <tr>
    <td width="50%" valign="top">
      <a href="https://raw.githubusercontent.com/rodrigo-arenas/scikit-link/main/docs/assets/readme/schema.png"><picture><img src="https://raw.githubusercontent.com/rodrigo-arenas/scikit-link/main/docs/assets/readme/schema.png" width="100%" alt="The customer primary key links 24 customers to 56 orders."></picture></a><br>
      <strong>Database schema</strong>
    </td>
    <td width="50%" valign="top">
      <a href="https://raw.githubusercontent.com/rodrigo-arenas/scikit-link/main/docs/assets/readme/tree.png"><picture><img src="https://raw.githubusercontent.com/rodrigo-arenas/scikit-link/main/docs/assets/readme/tree.png" width="100%" alt="The selected customer follows the branch for more than 2.5 orders."></picture></a><br>
      <strong>Decision tree</strong>
    </td>
  </tr>
  <tr>
    <td width="50%" valign="top">
      <a href="https://raw.githubusercontent.com/rodrigo-arenas/scikit-link/main/docs/assets/readme/importance.png"><picture><img src="https://raw.githubusercontent.com/rodrigo-arenas/scikit-link/main/docs/assets/readme/importance.png" width="100%" alt="A flow diagram breaks down forest importance by table, column and aggregation."></picture></a><br>
      <strong>Feature importance</strong>
    </td>
    <td width="50%" valign="top">
      <a href="https://raw.githubusercontent.com/rodrigo-arenas/scikit-link/main/docs/assets/readme/explanation.png"><picture><img src="https://raw.githubusercontent.com/rodrigo-arenas/scikit-link/main/docs/assets/readme/explanation.png" width="100%" alt="Order count occurs on 10 of the 16 decision paths for this customer."></picture></a><br>
      <strong>One prediction</strong>
    </td>
  </tr>
</table>

## Learn by example

- [Churn from raw tables to a deployed model](https://scikit-link.rodrigo-arenas.com/stable/case-studies/subscription-churn):
  monthly cutoffs, temporal cross-validation, explanations and deployment.
- [scikit-learn ensembles and cost-based thresholds](https://scikit-link.rodrigo-arenas.com/stable/case-studies/scikit-learn-toolbox)
  on relational models.
- [Forecasts with calibrated prediction intervals](https://scikit-link.rodrigo-arenas.com/stable/case-studies/machine-condition-intervals)
  from sensor readings and repair logs.
- [Segments and anomalies without labels](https://scikit-link.rodrigo-arenas.com/stable/case-studies/segment-and-flag-accounts)
  from bank transactions.
- [Migrate from Featuretools](https://scikit-link.rodrigo-arenas.com/stable/guides/migrate-from-featuretools) if you
  use deep feature synthesis today.

## How it compares

| | scikit-link | Featuretools | getML community | skrub |
|---|---|---|---|---|
| scikit-learn estimator API | Yes | Partial | No | Yes |
| Learns conditions inside aggregations | Yes | No | No | No |
| Native trees, forests and gradient boosting on related tables | Yes | No | No | No |
| Install on Windows and macOS without Docker | Yes | Yes | No | Yes |

skrub and scikit-link are meant to work together. The
[comparison page](https://scikit-link.rodrigo-arenas.com/compare/) explains when
to use each one, and the [benchmarks](https://scikit-link.rodrigo-arenas.com/benchmarks)
compare scikit-link with gradient boosting on hand-built features, on the same
splits and tuning budget.

## Contribute

scikit-link is young, and contributions of every size help:

- **Share your schema.** Tell us about your tables and task in
  [Discussions](https://github.com/rodrigo-arenas/scikit-link/discussions);
  real use cases decide what comes next.
- **Report a bug or ask for a feature** in
  [Issues](https://github.com/rodrigo-arenas/scikit-link/issues).
- **Add an aggregation.** A new primitive is a small, self-contained first
  pull request; see [custom primitives](https://scikit-link.rodrigo-arenas.com/stable/tutorials/custom-primitives).
- **Add a benchmark** dataset or a documentation example.

[CONTRIBUTING.md](CONTRIBUTING.md) covers the development setup, the checks
and the testing policy. Security reports follow [SECURITY.md](SECURITY.md).
If scikit-link is useful to you, a star on GitHub helps others find it.

## Citing

If scikit-link helps your research, please cite it. GitHub's "Cite this
repository" button reads [CITATION.cff](CITATION.cff); in BibTeX:

```bibtex
@software{arenas_scikit_link,
  author  = {Arenas, Rodrigo},
  title   = {scikit-link: Machine learning on multiple related tables, with the scikit-learn API},
  year    = {2026},
  url     = {https://github.com/rodrigo-arenas/scikit-link},
  license = {MIT}
}
```

## License

[MIT](LICENSE)
