背景
训练好的xgboost模型,以树图的方式将子树可视化。
准备
1、python安装pydotplus包,直接pip install pydotplus或者whl文件安装;
2、python安装graphviz包,直接pip install graphviz或者whl文件安装;
3、下载安装graphviz,下载地http://www.graphviz.org/download/;安装完成后应将graphviz的安装路径添加到环境变量Path中:
示例
import xgboost as xgb
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.datasets import load_breast_cancer
dataset = load_breast_cancer()
X = pd.DataFrame(dataset.data)
#生成特征名map文件;注意:变量名中不能带有空格;i代表indicator数据类型,q代表quantity数据类型
X.columns = pd.Series(dataset.feature_names).str.replace(' ','_')
def create_feature_map(features):
outfile = open('clf.fmap','w')
for i,f in enumerate(features):
outfile.write('{0}\t{1}\tq\n'.format(i,f))
outfile.close()
create_feature_map(X.columns)
#训练模型
model = xgb.XGBClassifier(n_estimators=5)
model.fit(X, dataset.target)
#模型树可视化
xgb.plot_tree(model, num_trees=0, fmap='clf.fmap')
fig = plt.gcf()
fig.set_size_inches(120,120)
fig.savefig('tree.png')