Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lefts-0.1.1.tar.gz (24.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lefts-0.1.1-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

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

Hashes for lefts-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7fa70ff559deb5223d0274a84613c723a659136df67462da3654a6686d5ba10e
MD5 0758e95f7bc07bd26cd0486bb5843f31
BLAKE2b-256 906a843d20b5d0f4449d07e1dc29577810601ba953af44f2f054c3542dbe7de5

See more details on using hashes here.

Provenance

The following attestation bundles were made for lefts-0.1.1.tar.gz:

Publisher: publish.yml on nsmat/lefts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for lefts-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 872f4ff459e916dd3e9ee62cbffd57be819fe8ef31f7330b973d6d21dbfba892
MD5 e63743a9b861053bddccfaf79e275a83
BLAKE2b-256 ae6469bd26c3577c2becc74a396cffd89f0b31e0507196b68c06e061116ab5c2

See more details on using hashes here.

Provenance

The following attestation bundles were made for lefts-0.1.1-py3-none-any.whl:

Publisher: publish.yml on nsmat/lefts

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page