Package: slim-trees
Slim Treesslim-treesis a Python package for saving and loading compressedsklearnTree-based andlightgbmmodels.
The compression is performed by modifying how the model is pickled by Python'spicklemodule.We presented this library at PyData Berlin 2023, check out theslides!Installationpipinstallslim-trees# ormicromambainstallslim-trees-cconda-forge# orpixiaddslim-treesUsageUsingslim-treesdoes not affect your training pipeline.
Simply calldump_sklearn_compressedordump_lgbm_compressedto save your model.[!WARNING]slim-treesdoes not save all the data that would be saved bysklearn:
only the parameters that are relevant for inference are saved. If you want to save the full model includingimpurityetc. for analytic purposes, we suggest saving both the original usingpickle.dumpfor analytics
and the slimmed down version usingslim-treesfor production.Example for aRandomForestClassifier:# example, you can also use other Tree-based modelsfromsklearn.ensembleimportRandomForestClassifierfromslim_treesimportdump_sklearn_compressed# load training dataX,y=...model=RandomForestClassifier()model.fit(X,y)dump_sklearn_compressed(model,"model.pkl")# or alternatively with compressiondump_sklearn_compressed(model,"model.pkl.lzma")Example for aLGBMRegressor:fromlightgbmimportLGBMRegressorfromslim_treesimportdump_lgbm_compressed# load training dataX,y=...model=LGBMRegressor()model.fit(X,y)dump_lgbm_compressed(model,"model.pkl")# or alternatively with compressiondump_lgbm_compressed(model,"model.pkl.lzma")Later, you can load the model usingload_compressedorpickle.load.importpicklefromslim_treesimportload_compressedmodel=load_compressed("model.pkl")# or alternatively with pickle.loadwithopen("model.pkl","rb")asf:model=pickle.load(f)Save your model asbytesYou can also save the model asbytesinstead of in a file similar to thepickle.dumpsmethod.fromslim_treesimportdumps_sklearn_compressed,loads_compressedX,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 pickleYou can also use theslim_trees.sklearn_tree.dumporslim_trees.lgbm_booster.dumpfunctions as drop-in replacements forpickle.dump.fromslim_treesimportsklearn_tree,lgbm_booster# for sklearn modelswithopen("model.pkl","wb")asf:sklearn_tree.dump(model,f)# instead of pickle.dump(...)# for lightgbm modelswithopen("model.pkl","wb")asf:lgbm_booster.dump(model,f)# instead of pickle.dump(...)Development InstallationYou can install the package in development mode using the new conda package managerpixi:❯gitclonehttps://github.com/quantco/slim-trees.git
❯cdslim-trees

❯pixiinstall
❯pixirunpre-commitinstall
❯pixirunpipinstall--no-deps--disable-pip-version-check--no-build-isolation-e.
❯pixirunpython
>>>importslim_trees[...]❯pixiruntest[...]Alternatively, you can usemicromamba(ormambaorconda):❯micromambacreate-fenvironment.yml
❯micromambaactivateslim_trees

❯pre-commitinstall
❯pipinstall--no-build-isolation-e.BenchmarkAs a general overview on what you can expect in terms of savings:
This is a 1.2G large sklearnRandomForestRegressor.The new file is 9x smaller than the original pickle file.