使用 XGBClassifier 转储具有特征图的 XGBoost 模型

use*_*486 1 python machine-learning xgboost

我想将 XGboost 模型及其特征图转储到文本文件。这可以这样做;

\n\n
# https://xgboost.readthedocs.io/en/latest/python/python_intro.html\nimport xgboost as xgb\nbst = xgb.train(param, dtrain, num_round, evallist)\n# dump model with feature map\nbst.dump_model('dump.raw.txt', 'featmap.txt')\n
Run Code Online (Sandbox Code Playgroud)\n\n

不过,我正在使用 XGBClassifier。该方法dump_model在 XGBClassifier 中不可用。

\n\n
from xgboost import XGBClassifier\nxgboost_model = XGBClassifier()\nxgboost_model.fit(x_train, y_train)\n# line below can't work because dump_model is not available in XGBClassifier\nxgboost_model.dump_model(\xe2\x80\x98dump.raw.txt\xe2\x80\x99, 'featmap.txt\xe2\x80\x99)  \n
Run Code Online (Sandbox Code Playgroud)\n\n

如何使用 XGBClassifier 转储具有特征图的 XGBoost 模型?

\n\n

我正在使用 python 3.7

\n

use*_*486 6

我会回答我自己的问题。

https://xgboost.readthedocs.io/en/latest/python/python_api.htmlget_booster()首先使用该函数获取该模型的底层 xgboost Booster。助推器具有dump_model可用的功能。

bst = xgboost_model.get_booster()
bst.dump_model('dump.raw.txt', 'featmap.txt')
Run Code Online (Sandbox Code Playgroud)