有没有一种方法可以使用graphviz更改xgboost树中的叶节点标签

时间:2019-04-26 11:33:48

标签: python graphviz xgboost

我试图放树的照片,但是我的声誉还不够...
无论如何,在XGBoost中,我检查了树以解释模型。顺便说一句,叶节点标签表示原始分数。我想在树中看到预测概率。例如,如果叶节点值为0.0856821,则所需值为1 /(1 + exp(-1 * 0.0856821)。
有没有办法更改叶节点标签?

to_graphviz(model, num_trees=99)

我认为一种方法是将xgboost模型转储为json格式。然后,更改json中的值。最后使用graphviz将json格式更改为点格式。问题是我不知道如何将json格式更改为点格式。下面是一个示例,但实际上树很深并且更复杂。

之前:

{ "nodeid": 0, "depth": 0, "split": "f0", "split_condition": 5.03515, "yes": 1, "no": 2, "missing": 1, "children": [
    { "nodeid": 1, "depth": 1, "split": "f0", "split_condition": 3.0743, "yes": 3, "no": 4, "missing": 3, "children": [
        { "nodeid": 3, "leaf": 0.0856821 },
        { "nodeid": 4, "leaf": 0.158854 }
      ]
    },
    { "nodeid": 2, "depth": 1, "split": "f0", "split_condition": 6.81955, "yes": 5, "no": 6, "missing": 5, "children": [
        { "nodeid": 5, "leaf": 0.240472 },
        { "nodeid": 6, "leaf": 0.371366 }
      ]
    }
  ]
}

之后:

{ "nodeid": 0, "depth": 0, "split": "f0", "split_condition": 5.03515, "yes": 1, "no": 2, "missing": 1, "children": [
    { "nodeid": 1, "depth": 1, "split": "f0", "split_condition": 3.0743, "yes": 3, "no": 4, "missing": 3, "children": [
        { "nodeid": 3, "leaf": 0.5214074298536938 },
        { "nodeid": 4, "leaf": 0.5396301973693495 }
      ]
    },
    { "nodeid": 2, "depth": 1, "split": "f0", "split_condition": 6.81955, "yes": 5, "no": 6, "missing": 5, "children": [
        { "nodeid": 5, "leaf": 0.5598299629669533 },
        { "nodeid": 6, "leaf": 0.5917890109082163 }
      ]
    }
  ]
}

希望的结果:

digraph {
    graph [rankdir=UT]
    0 [label="f0<5.03515"]
    0 -> 1 [label="yes, missing" color="#0000FF"]
    0 -> 2 [label="no" color="#FF0000"]
    1 [label="f0<3.0743"]
    1 -> 3 [label="yes, missing" color="#0000FF"]
    1 -> 4 [label="no" color="#FF0000"]
    3 [label="leaf=0.5214074298536938"]
    4 [label="leaf=0.5396301973693495"]
    2 [label="f0<6.81955"]
    2 -> 5 [label="yes, missing" color="#0000FF"]
    2 -> 6 [label="no" color="#FF0000"]
    5 [label="leaf=0.5598299629669533"]
    6 [label="leaf=0.5917890109082163"]
}

0 个答案:

没有答案