What is the best way to convert a Python Lightgbm tree JSON dump into a VBA formula?

11 hours ago 1
ARTICLE AD BOX

I have a LightGBM model in Python that generates a decision tree–based model for a dataset with many predictors and one response variable. I exported the model to JSON using:

model_json = lgb_model.dump_model() with open(f"lightgbm_model_{i + 1}.json", "w") as f: json.dump(model_json, f, indent=2)

Here’s a simplified excerpt of one tree (the full output is HUGE):

{ "tree_info": [ { "tree_index": 0, "num_leaves": 3, "tree_structure": { "split_index": 0, "split_feature": 0, "threshold": 10, "decision_type": "<=", "left_child": { "leaf_value": 1 }, "right_child": { "leaf_value": -1 } } } ], "feature_names": ["Column_0"] }

I also have a variable feat_set that contains a set of actual column names that could map these Column_n names to the actual column names in an Excel table (these correspond to structured references like [@FeatureName]).

Instead of trying to generate a single Excel formula (which would exceed Excel’s formula length limit for real trees), I want to generate a VBA user-defined function (UDF) that evaluates the tree for a given row.

For the example above, the equivalent logic in VBA would look something like:

Function EvalTree(FeatureA As Double) As Double If FeatureA <= 10 Then EvalTree = 1 Else EvalTree = -1 End If End Function

In the real model, the trees are deeper and involve many features, so this would need to be generated programmatically from the JSON.

My questions:

What’s a good approach to traverse the LightGBM JSON tree and emit equivalent VBA code (nested If...Then...Else)?

Are there existing patterns or tools for converting tree-based models into VBA, or is this typically done with a custom code generator?

Any pitfalls to watch for when mapping split_feature indices to Excel inputs (e.g., passing values from a table row into the UDF)?

Read Entire Article