xgboost 模型展示 特征重要性和决策树形状

本文介绍了在使用xgboost时遇到的问题及其解决方案,包括未安装graphviz的解决方法——通过Anaconda Prompt安装,以及如何调整决策树图像的大小,以清晰查看特征重要性和树结构。
# *********get xgb model's feature importance************
b2_model = xgb.Booster(model_file=model_path + '/b2_model_12.model')
importance = b2_model.get_fscore()
importance = sorted(importance.items(), key=operator.itemgetter(1), reverse=False)
df = pd.DataFrame(importance, columns=['feature', 'fscore'])
feat_num = df.shape[0]
for k in range(feat_num):
    index = df.at[k, 'feature']
    index = index.replace('f', '')
    index = int(index)
    name = feat_name[index]
    df.at[k, 'feature'] = name
    stop = 0

df['fscore'] = df['fscore'] / df['fscore'].sum()
df.to_csv(model_path + "xgb_feat_importance_12.csv", index=False)

# Plot the feature importances
plt.figure()
df.plot(kind='barh', x='feature', y='fscore', legend=False, figsize=(6, 10), fontsize=14)
plt.title('XGBoost Feature Importance')
plt.xlabel('relative importance')
plt.show()

#plot the tree
plt.figure()
xgb.plot_tree(b2_model, num_trees=0)
# xgb.plot_tree(b2_model, num_trees=0, rank
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值