Skip to main content

Slim Trees

CI conda-forge pypi-version python-version

slim-trees is a Python package for saving and loading compressed sklearn Tree-based and lightgbm models. The compression is performed by modifying how the model is pickled by Python's pickle module.

We presented this library at PyData Berlin 2023, check out the slides!

Installation

pip install slim-trees
# or
micromamba install slim-trees -c conda-forge
# or
pixi add slim-trees

Usage

Using slim-trees does not affect your training pipeline. Simply call dump_sklearn_compressed or dump_lgbm_compressed to save your model.

Example for a RandomForestClassifier:

# example, you can also use other Tree-based models
from sklearn.ensemble import RandomForestClassifier
from slim_trees import dump_sklearn_compressed

# load training data
X, y = ...
model = RandomForestClassifier()
model.fit(X, y)

dump_sklearn_compressed(model, "model.pkl")
# or alternatively with compression
dump_sklearn_compressed(model, "model.pkl.lzma")

Example for a LGBMRegressor:

from lightgbm import LGBMRegressor
from slim_trees import dump_lgbm_compressed

# load training data
X, y = ...
model = LGBMRegressor()
model.fit(X, y)

dump_lgbm_compressed(model, "model.pkl")
# or alternatively with compression
dump_lgbm_compressed(model, "model.pkl.lzma")

Later, you can load the model using load_compressed or pickle.load.

import pickle
from slim_trees import load_compressed

model = load_compressed("model.pkl")

# or alternatively with pickle.load
with open("model.pkl", "rb") as f:
    model = pickle.load(f)

Save your model as bytes

You can also save the model as bytes instead of in a file similar to the pickle.dumps method.

from slim_trees import dumps_sklearn_compressed, loads_compressed

X, y = ...
model = RandomForestClassifier()
model.fit(X, y)

data = dumps_sklearn_compressed(model, compression="lzma")
...
model_loaded = loads_compressed(data, compression="lzma")

Drop-in replacement for pickle

You can also use the slim_trees.sklearn_tree.dump or slim_trees.lgbm_booster.dump functions as drop-in replacements for pickle.dump.

from slim_trees import sklearn_tree, lgbm_booster

# for sklearn models
with open("model.pkl", "wb") as f:
    sklearn_tree.dump(model, f)  # instead of pickle.dump(...)

# for lightgbm models
with open("model.pkl", "wb") as f:
    lgbm_booster.dump(model, f)  # instead of pickle.dump(...)

Development Installation

You can install the package in development mode using the new conda package manager pixi:

❯ git clone https://github.com/quantco/slim-trees.git
❯ cd slim-trees

❯ pixi install
❯ pixi run postinstall
❯ pixi run test
[...]
❯ pixi run py312 python
>>> import slim_trees
[...]

Benchmark

As a general overview on what you can expect in terms of savings: This is a 1.2G large sklearn RandomForestRegressor.

benchmark

The new file is 9x smaller than the original pickle file.

Metadata

Release files for slim-trees 0.2.15

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for slim-trees 0.2.15
File Size Uploaded Provenance Actions
slim_trees-0.2.15.tar.gz 11.7 kB Without provenance Details for slim_trees-0.2.15.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for slim-trees 0.2.15
File Interpreter ABI Platform Actions
slim_trees-0.2.15-py3-none-any.whl Python 3 none any Details for slim_trees-0.2.15-py3-none-any.whl

Total release size: 25.8 kB

Release files / slim_trees-0.2.15.tar.gz

Download URL slim_trees-0.2.15.tar.gz
Size 11.7 kB
Tags Source
SHA-256 checksum
How to use checksums
587385f0161cb343c03fb9f16f8236ae4cf684c804f03d2a208bf4f36030784b
BLAKE2b-256 checksum
How to use checksums
58413e9d0b5d1cec9e5f2c02059e9190b0a5402abe74157f83f9f1dd9e21c6a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / slim_trees-0.2.15-py3-none-any.whl

Download URL slim_trees-0.2.15-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7ef8e967cd3a38ad9be17980fb423f485d4eef0ff6d3c36c95eabc6bfcc0d06f
BLAKE2b-256 checksum
How to use checksums
db74458f39b45eae3392a4e9652a5768fdcbae168e1a7f3be41797cef9098bcc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Security for slim-trees 0.2.15

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page