Composable transformations for model experimentation
Project description
Lefts: composable transformations for creative machine learning
Lefts is a very simple domain specific language for building complex machine learning workflows from simple ones. Starting with your favourite machine learning models, you can use Lefts operations to:
- Build complex ensembles.
- Build complex cross validation and hyper-parametrisation procedures.
- Allow a model to create features or targets for another model.
- And any creative combination of the above.
Without making subsequent model fitting, evaluation or experimentation any more complex than it was with the original model. This implementation is built on top of the excellent Polars DataFrame library.
Installation
pip install lefts
Commands
Lefts has five commands, which give it its name:
- Lift: trains multiple copies of a model across different subsets of data.
- Ensemble: Takes a set of models and makes them evaluate as one.
- Tune: Allows a model to learn its hyperparameters from another.
- Feed: Allows the output of one model to be used as a feature or target by another.
- Split: Trains a model on a given train/test/validation split.
These commands can be applied to any model that is defined by:
- a fit method, which maps from training data into the model parameters
- a predict method, which maps from model parameters and test data into predictions.
A Lefts command creates a new model by transforming these functions into a new .fit and .predict. Because this new model also has a .fit and .predict, it can be transformed with further Lefts commands.
An example
The following code creates a rolling monthly retrain workflow: twelve copies of a Ridge regression, each trained on all data before its month and evaluated on that month only.
from lefts import leaf, lift
from lefts.helpers import tabular_model
import polars as pl
from sklearn.linear_model import Ridge
import datetime as dt
features = ["temp", "humidity", "hour"]
target = "demand"
dates = [dt.date(2024, m, 1) for m in range(1, 13)]
model = lift(
leaf(tabular_model(Ridge, features=features, target=target), label='ridge'),
name='monthly_retrain',
values=dates,
train_filter=lambda d: pl.col('date') < d,
test_filter=lambda d: pl.col('date').dt.month() == d.month,
aggregate_with=pl.coalesce,
)
# Fits 12 independent models
model.fit(df)
# Each row gets the prediction from the model trained up to its month
predictions = model.predict(df)
You can inspect the workflow before running it with model.print_tree():
Lift 'monthly_retrain' (12 models): [2024-01-01, ..., 2024-12-01] ⇒ coalesce → "monthly_retrain"
└── Leaf 'ridge'
For more complex examples, see the 'Examples' section in the docs.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lefts-0.1.1.tar.gz.
File metadata
- Download URL: lefts-0.1.1.tar.gz
- Upload date:
- Size: 24.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7fa70ff559deb5223d0274a84613c723a659136df67462da3654a6686d5ba10e
|
|
| MD5 |
0758e95f7bc07bd26cd0486bb5843f31
|
|
| BLAKE2b-256 |
906a843d20b5d0f4449d07e1dc29577810601ba953af44f2f054c3542dbe7de5
|
Provenance
The following attestation bundles were made for lefts-0.1.1.tar.gz:
Publisher:
publish.yml on nsmat/lefts
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lefts-0.1.1.tar.gz -
Subject digest:
7fa70ff559deb5223d0274a84613c723a659136df67462da3654a6686d5ba10e - Sigstore transparency entry: 2257033014
- Sigstore integration time:
-
Permalink:
nsmat/lefts@27df0c626862cbadd979ec5cb2e8e9d614efc26b -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/nsmat
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@27df0c626862cbadd979ec5cb2e8e9d614efc26b -
Trigger Event:
release
-
Statement type:
File details
Details for the file lefts-0.1.1-py3-none-any.whl.
File metadata
- Download URL: lefts-0.1.1-py3-none-any.whl
- Upload date:
- Size: 19.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
872f4ff459e916dd3e9ee62cbffd57be819fe8ef31f7330b973d6d21dbfba892
|
|
| MD5 |
e63743a9b861053bddccfaf79e275a83
|
|
| BLAKE2b-256 |
ae6469bd26c3577c2becc74a396cffd89f0b31e0507196b68c06e061116ab5c2
|
Provenance
The following attestation bundles were made for lefts-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on nsmat/lefts
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lefts-0.1.1-py3-none-any.whl -
Subject digest:
872f4ff459e916dd3e9ee62cbffd57be819fe8ef31f7330b973d6d21dbfba892 - Sigstore transparency entry: 2257033029
- Sigstore integration time:
-
Permalink:
nsmat/lefts@27df0c626862cbadd979ec5cb2e8e9d614efc26b -
Branch / Tag:
refs/tags/0.1.1 - Owner: https://github.com/nsmat
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@27df0c626862cbadd979ec5cb2e8e9d614efc26b -
Trigger Event:
release
-
Statement type: