Python API
Generated from the docstrings of lignum 0.4.1 by docs/scripts/api_reference.py.
lignum.load
Section titled “lignum.load”lignum.load(filepath: str, format: str = 'auto', binned: bool = True, compiled: str | os.PathLike | None = None, validate_features: bool = True) -> ModelLoad a trained model: LightGBM or XGBoost files, or a lignum .bin.
Parameters
filepath(str) Path to the model file on disk.format({‘auto’, ‘lignum’, ‘lightgbm’, ‘xgboost’, ‘catboost’}, default=‘auto’) Library of the model. The file’s own bytes tell which of its formats it is, so ‘auto’ loads a lignum.bin, a LightGBMmodel.txt(save_model) ordump_model()JSON, an XGBoost.jsonor.ubj(save_model) and a CatBoost JSON (save_model(..., format="json")). Naming the library only adds a check.binned(bool, default=True) Prepare the binned kernel: inputs are mapped once per row to integer bin codes and categorical decisions are precomputed, so the tree walk compares small integers. Results are identical either way; False keeps only the direct traversal (lower memory use).compiled(path, optional) Library written byModel.compile(library=...)for this model, or the folder it was written to (the file for this model and platform is picked); predictions then use the compiled trees without compiling again. Needsbinned=True. A library for another model or lignum version raisesRuntimeError. Only load libraries you trust.validate_features(bool, default=True) Default ofModel.predict(..., validate_features=...): check the column names of pandas and Arrow inputs against the model’s feature names. Also settable later asmodel.validate_features.
Returns
model(lignum.Model) The loaded model, ready for immediate inference.
Raises
FileNotFoundErrorIf the specifiedfilepathdoes not exist.
Examples
import lignum
model = lignum.load("xgboost_model.json", format="xgboost")lignum.catboost_hash
Section titled “lignum.catboost_hash”lignum.catboost_hash(category) -> intCatBoost’s hash of a category: a string, or an integer (hashed as its decimal text, like
CatBoost). Model.predict hashes categorical columns itself; this is the value the kernel
sees, and what C and C++ callers put in the categorical columns of a float64 matrix.
lignum.cache_dir
Section titled “lignum.cache_dir”lignum.cache_dir() -> strFolder where Model.compile() keeps compiled libraries: LIGNUM_CACHE_DIR if set, otherwise
~/.cache/lignum (Linux, or $XDG_CACHE_HOME/lignum), ~/Library/Caches/lignum (macOS) or
%LOCALAPPDATA%\lignum\Cache (Windows).
lignum.clear_cache
Section titled “lignum.clear_cache”lignum.clear_cache() -> intRemove the compiled libraries from cache_dir() and return how many were removed. Models that
already use one keep working; on Windows a library loaded by a running process stays.
Libraries written with compile(library=...) are not in the cache and are not touched.
lignum.Model
Section titled “lignum.Model”A loaded LightGBM, XGBoost or CatBoost model.
Create it with lignum.load(). Predictions use the interpreter, or the compiled trees after
compile() / load(..., compiled=...); both give the same, bit-identical results.
Model.autotune
Section titled “Model.autotune”Model.autotune(X: np.ndarray, n_jobs: int = -1, budget: float = 1.0) -> dictMeasure the interpreter and the compiled trees on a sample and use each where it is faster.
Which one wins depends on the model, the data and the CPU: compiled code is usually faster,
but on data whose splits are hard to predict the branch-free interpreter can win, mostly
for small batches. Batches of 1, 4, 16, … rows up to the sample size are timed with
both (about budget seconds in total), and compiled_min_rows is set to the smallest
size from which the compiled trees win at every larger size (sys.maxsize: never).
Predictions are identical either way.
Parameters
X(array-like, shape (n_samples, n_features)) Representative rows (a few thousand are enough).n_jobs(int, default=-1) Threads, as inpredict.budget(float, default=1.0) Approximate time to spend measuring, in seconds.
Returns
dictcompiled_min_rowsandtimings: {rows: (interpreter_us, compiled_us)}, median per call.
Model.categorical_features
Section titled “Model.categorical_features”Property. Columns that hold categories (CatBoost models with categorical features), in order.
Model.compile
Section titled “Model.compile”Model.compile(compiler: str = 'auto', flags: str = '', *, library: str | os.PathLike | None = None, jobs: int = 0, block_trees: int = 16, fallback: bool = False, autotune: np.ndarray | None = None) -> dictExperimental: compile the trees to native code with the system C compiler.
Each tree becomes C code with constant thresholds; leaves are read from per-block
tables. Trees are grouped in blocks of block_trees, and a block is walked for a whole
chunk of rows before the next one; binning of categorical features, threads and
transforms stay in lignum and every row sums its trees in model order, so predictions
are unchanged (bit-identical).
Without library, the library is kept in a per-user cache (lignum.cache_dir()) and
compiling the same model again only loads it; lignum.clear_cache() empties the cache.
The code is built in a temporary folder that is always removed. Where the library is:
model.compiled_library, and library in the returned dict.
Parameters
compiler(str, default=‘auto’) C compiler.'auto'usesccon Linux and macOS; on Windowsclif it is on PATH, otherwise the newest Visual Studio with C++ tools (found with vswhere), thengcc(MinGW) andclang. MSVC-style compilers (cl,clang-cl) are driven with their own options.flags(str, default=‘’) Optimisation flags; empty means-O2(gcc, clang) or/O2(MSVC).library(path, optional) Write the library here instead of the cache, e.g. next to the.binmodel, so production can attach it withlignum.load(model_path, compiled=library)without a compiler. A file path, or a folder (an existing one, or a path ending in/) that getslignum_<model fingerprint>_<platform>.so(.dllon Windows); one folder can hold the libraries of several models and platforms. Always compiled anew.jobs(int, default=0) Source files compiled in parallel; 0 means one per CPU core.block_trees(int, default=16) Trees per generated function; larger blocks compile slower and run about the same.fallback(bool, default=False) When the compiler is missing or fails, warn and keep the interpreter instead of raising. The returned dict then hascompiled=Falseand theerror.autotune(array, optional) Sample of the data to predict on; after compiling,autotune(sample)picks the batch size from which the compiled trees are used (result underautotune).
Returns
dictcompiled, timings (codegen_ms,compile_ms,load_ms),source_bytes,cached, thelibrarypath and thecompilerused (empty for a cached library).
Examples
model = lignum.load("model.json", format="lightgbm")model.save("model.bin")model.compile(library="model.so") # build machine: needs a C compiler
model = lignum.load("model.bin", compiled="model.so") # production: no compilerModel.compiled_library
Section titled “Model.compiled_library”Property. Path of the compiled library the model uses (in the cache, or where it was written or loaded from), or None when it uses the interpreter.
Model.compiled_min_rows
Section titled “Model.compiled_min_rows”Property. Batches with fewer rows use the interpreter even when compiled (set by autotune).
Model.decompile
Section titled “Model.decompile”Model.decompile() -> NoneGo back to the interpreter.
Model.feature_names
Section titled “Model.feature_names”Property. Names of the columns the model was trained on, in order, or None when the model has no
names (trained on a matrix). X[model.feature_names] puts a DataFrame’s columns in the
model’s order.
Model.info
Section titled “Model.info”Model.info() -> dictSummary of the model, for logs and checks before serving it.
Returns
dictsource,objective,transform(from raw scores to predictions: “identity”, “sigmoid”, “exp”, “log1p_exp”, “softmax” or “argmax”),n_trees,n_leaves,n_features,n_outputs,feature_names,categorical_features,kernel(“catboost”, “compiled”, “binned” or “direct”),compiled_libraryandwritten_by(lignum version that wrote a.binfile, otherwise None).
Examples
lignum.load(“model.txt”).info()[“n_trees”] 500
Model.load_compiled
Section titled “Model.load_compiled”Model.load_compiled(library: str | os.PathLike) -> NoneAttach a library built earlier by compile(library=...) for this model: the file, or
the folder it was written to (the file for this model and platform is picked).
The library records which model it was generated from and the lignum ABI; a library
for another model or lignum version raises RuntimeError. Loading runs the library’s
native code, so only load libraries you trust (like a pickle).
Model.n_features
Section titled “Model.n_features”Property. Minimum number of columns the input must have.
Model.n_outputs
Section titled “Model.n_outputs”Property. Number of model outputs: the number of classes for multiclass models, otherwise 1.
Model.n_trees
Section titled “Model.n_trees”Property. Number of trees. Multiclass models have one tree per class and iteration, all counted
(like LightGBM’s num_trees()).
Model.objective
Section titled “Model.objective”Property. Objective as the source library names it (“binary”, “multi:softprob”, “Logloss”), or None when the file does not say.
Model.predict
Section titled “Model.predict”Model.predict(X: np.ndarray, raw_score: bool = False, n_jobs: int = -1, strategy: str = 'auto', validate_features: bool | None = None) -> np.ndarrayPredict values for the given input data.
For optimal performance, ensure X is a C-contiguous float64 array.
Otherwise, an internal copy will be triggered, which may degrade performance.
Parameters
X(array-like or Arrow data, shape (n_samples, n_features)) The input samples to predict: anything numpy converts to a float64 matrix, or Arrow data through the PyCapsule interface (pyarrow.RecordBatch/Table/RecordBatchReader, polarsDataFrame, …). Arrow columns are used by position; float64, float32, integer, boolean and null columns are read without copying the table, nulls are NaN and dictionary (categorical) columns give their category codes. Predictions equal those for the same float64 matrix. pandas DataFrames are converted with numpy; passpyarrow.Table.from_pandas(df)for categorical columns. A 1-D array or a flat list is one row. CatBoost models predict pandas frames of 50,000 rows or more through pyarrow when it is installed (faster, same results). CatBoost models with categorical features take their categories as strings or integers: a pandas DataFrame, a numpy object array, or a list (one row) or list of rows; seecategorical_featuresandcatboost_hash.raw_score(bool, default=False) Whether to output raw scores (leaf values) instead of transformed predictions.n_jobs(int, default=-1) Number of OpenMP threads to use.-1means using all available logical cores.strategy({‘auto’, ‘serial’, ‘rows’}, default=‘auto’) How the work is spread across threads. All strategies return identical results; only speed differs.'serial'runs on the calling thread (lowest latency for a few rows),'rows'splits rows between threads.'auto'uses'serial'below 8 rows or with one thread,'rows'otherwise.validate_features(bool, optional) Check that the column names of a pandas DataFrame or Arrow data start withfeature_names, in the same order, and raiseValueErrorotherwise (a reordered or missing column would silently give other predictions). Default: the model’svalidate_features(True unless set withload(..., validate_features=False)). numpy arrays have no names and are always used by position, as are frames without column names and models without feature names. The check costs about 1-5 µs per call.
Returns
preds(ndarray, shape (n_samples,) or (n_samples, n_classes)) The predicted values. Multiclass models return one column per class (probabilities, or margins withraw_score=True), except XGBoostmulti:softmax, which returns the predicted class index.
Examples
import numpy as npimport lignum
model = lignum.load("model.bin")X = np.ascontiguousarray(np.array([[1.2, 3.4], [5.6, 7.8]], dtype=np.float64))model.predict(X, n_jobs=2)# array([0.45, 0.89])Model.save
Section titled “Model.save”Model.save(filepath: str) -> NoneSave the current model to the Lignum binary format.
This format is optimized for fast, memory-mapped (mmap) loading in subsequent sessions.
Parameters
filepath(str) Destination file path for the compiled.binmodel.
Examples
model.save("optimized_model.bin")Model.source
Section titled “Model.source”Property. Library the model was trained with: “lightgbm”, “xgboost” or “catboost”.
Model.uses_binned
Section titled “Model.uses_binned”Property. Whether batches use the binned kernel (False when disabled or unsupported by the model).
Model.uses_compiled
Section titled “Model.uses_compiled”Property. Whether predictions use compiled trees.