lleaves 🍃

class lleaves.Model(model_file)

The base class of lleaves.


Initialize the uncompiled model.


model_file – Path to the model.txt.

compile(cache=None, *, raw_score=False, fblocksize=34, fcodemodel='large', finline=True, froot_func_name='forest_root')

Generate the LLVM IR for this model and compile it to ASM.

For most users tweaking the compilation flags (fcodemodel, fblocksize, finline) will be unnecessary as the default configuration is already very fast. Modifying the flags is useful only if you’re trying to squeeze out the last few percent of performance.

  • cache – Path to a cache file. If this path doesn’t exist, binary will be dumped at path after compilation. If path exists, binary will be loaded and compilation skipped. No effort is made to check staleness / consistency.

  • raw_score – If true, compile the tree to always return raw predictions, without applying the objective function. Equivalent to the raw_score parameter of LightGBM’s Booster.predict().

  • fblocksize – Trees are cache-blocked into blocks of this size, reducing the icache miss-rate. For deep trees or small caches a lower blocksize is better. For single-row predictions cache-blocking adds overhead, set fblocksize=Model.num_trees() to disable it.

  • fcodemodel – The LLVM codemodel. Relates to the maximum offsets that may appear in an ASM instruction. One of {“small”, “large”}. The small codemodel will give speedups for most forests, but will segfault when used for compiling very large forests.

  • finline – Whether or not to inline function. Setting this to False will speed-up compilation time significantly but will slow down prediction.

  • froot_func_name – Name of entry point function in the compiled binary. This is the function to link when writing a C function wrapper. Defaults to “forest_root”.


Returns the number of features used by this model.


Returns the number of models per iteration.

This is equal to the number of classes for multiclass models, else will be 1.


Returns the number of trees in this model.

predict(data, n_jobs=2)

Return predictions for the given data.

The model needs to be compiled before prediction.

  • data – Pandas df, numpy 2D array or Python list. Shape should be (n_rows, model.num_feature()). 2D float64 numpy arrays have the lowest overhead.

  • n_jobs – Number of threads to use for prediction. Defaults to number of CPUs. For single-row prediction this should be set to 1.


1D numpy array, dtype float64. If multiclass model: 2D numpy array of shape (n_rows, model.num_model_per_iteration())