dmitryikh/leaves

read LightGBM model from JSON

dmitryikh opened this issue · 1 comments

read LightGBM model from JSON
d = clf.dump_model()
import json
with open('data.txt', 'w') as fout:
    json.dump(d, fout, indent=1)

data.txt:

{
 "name": "tree",
 "version": "v2",
 "num_class": 1,
 "num_tree_per_iteration": 1,
 "label_index": 0,
 "max_feature_idx": 29,
 "feature_names": [
  "Column_0",
  "Column_1",
...
 ],
 "tree_info": [
  {
   "tree_index": 0,
   "num_leaves": 7,
   "num_cat": 0,
   "shrinkage": 1,
   "tree_structure": {
    "split_index": 0,
    "split_feature": 27,
    "split_gain": 312.09600830078125,
    "threshold": 0.14545000000000005,
    "decision_type": "<=",
    "default_left": true,
    "missing_type": "None",
    "internal_value": 0,
    "internal_count": 455,
    "left_child": {
     "split_index": 1,
     "split_feature": 22,
     "split_gain": 44.737098693847656,
     "threshold": 107.45000000000002,
     "decision_type": "<=",
     "default_left": true,
     "missing_type": "None",
     "internal_value": 1.17818,
     "internal_count": 310,
     "left_child": {
      "split_index": 3,
      "split_feature": 1,
      "split_gain": 1.5678000450134277,
      "threshold": 21.295000000000005,
      "decision_type": "<=",
      "default_left": true,
      "missing_type": "None",
      "internal_value": 1.47353,
      "internal_count": 272,
      "left_child": {
       "split_index": 5,
       "split_feature": 11,
       "split_gain": 0.1967500001192093,
       "threshold": 1.917,
       "decision_type": "<=",
       "default_left": true,
       "missing_type": "None",
       "internal_value": 1.54939,
       "internal_count": 221,
       "left_child": {
        "leaf_index": 0,
        "leaf_value": 0.720832002249105,
        "leaf_count": 201
       },
       "right_child": {
        "leaf_index": 6,
        "leaf_value": 0.6991992955194886,
        "leaf_count": 20
       }
      },
...

Seems enough to restore all model information in the memory.