scikit-link 0.1.0 is out
scikit-link 0.1.0, the first release, is on PyPI. It trains scikit-learn models when the data is in several tables linked by keys, such as customers and their orders, patients and their visits, or machines and their sensor readings, without building a flat feature table by hand.
pip install "scikit-link[pandas]"The introduction explains why we built it. This post is about what the release contains and where to start.
A first model in a few lines
make_relational_classification generates a database of customers, orders, order items and products, and a label hidden in the orders. A relational tree finds that rule, and a featurizer feeds the same tables to any scikit-learn model.
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
db, X, y = make_relational_classification(1000, signal="conditional", random_state=0)
common = {"entity": "customers", "key": "customer_id"}
tree = RelationalDecisionTreeClassifier(db, max_depth=2, **common).fit(X, y)
print(export_text(tree))
pipe = make_pipeline(
RelationalFeaturizer(db, **common), HistGradientBoostingClassifier()
)
print(f"accuracy {cross_val_score(pipe, X, y, cv=5).mean():.3f}")|--- exists(orders[amount > 100.06 & type == 'express']) <= 0.500
| |--- class: 0
|--- exists(orders[amount > 100.06 & type == 'express']) > 0.500
| |--- class: 1
accuracy 0.960The tree's question is a rule you can read and check: whether the customer has an express order above an amount that the tree learned. On your own data, a Database declares the tables, keys and relationships, and X has one row per prediction.
What is in 0.1.0
- Features for any estimator.
RelationalFeaturizerturns related tables into columns, such ascount(orders[type == 'express']), inside a pipeline, so each cross-validation fold learns its catalog from its own training rows. - Models that learn on related rows. Relational decision trees, random forests and gradient boosting, for classification and regression, learn which related rows to aggregate and with which conditions while they fit. Gradient boosting has the log loss for classification and the squared error, absolute error, Huber and quantile losses for regression.
- No labels needed.
RelationalKMeansandRelationalClusteringTreegroup entities, andRelationalIsolationForestranks unusual ones. - Time without leakage. A cutoff date per row of
Xlimits every feature and split to the rows dated before it, andTemporalCutoffSplitvalidates on later dates, with a label horizon and a gap. - Explanations.
export_textprints a tree as rules,explainshows what a model used for one prediction,aggregate_importancesgroups importances by table or expression, andsklink.plottingdraws schemas, trees and predictions without a plotting library. - scikit-learn all the way. The estimators are tested with scikit-learn's estimator checks and work in pipelines, searches, ensembles and threshold tuning.
TransformedTablesEstimatorfits scikit-learn transformers on the tables themselves, such as a scaler, or aTfidfVectorizerfollowed byTruncatedSVDto embed a text column. - Ready for production.
sklink.detachreturns a copy of a fitted model without its table data, ready to pickle or save with skops, andsklink.rebindattaches the current tables after loading. Tables can be pandas, Polars or PyArrow tables, Parquet files or dictionaries of NumPy arrays, and Numba, which installs with the package, compiles the inner loops of trees, forests and gradient boosting.
The release notes list every change.
Where to start
- Quickstart: two models on a small shop database that comes with the package.
- Case studies: churn from raw tables to a deployed model, scikit-learn ensembles and cost-based thresholds, forecasts with calibrated intervals, and segments and anomalies without labels.
- Use scikit-link with scikit-learn if you already have pipelines.
- Migrate from Featuretools if you use deep feature synthesis today.
- Benchmarks compare scikit-link with flattened-table pipelines on public relational datasets.
What comes next
The roadmap lists the plans after 0.1.0. If you work with multi-table data, tell us about your use case in Discussions, and report problems in Issues. If scikit-link helps your research, the README explains how to cite it.