LightGBM模型可视化
参考文档 https://lightgbm.readthedocs.io/en/latest/Python-API.html#plotting
lgb中,对应的可视化函数是lightgbm.create_tree_digraph。以iris数据为例,训练一个lgb分类模型并可视化
#在环境变量中加入安装的Graphviz路径
import os
os.environ["PATH"] += os.pathsep + 'E:/Program Files (x86)/Graphviz2.38/bin'
from sklearn.datasets import load_iris
import lightgbm as lgb
iris = load_iris()
lgb_clf = lgb.LGBMClassifier()
lgb_clf.fit(iris.data, iris.target)
lgb.create_tree_digraph(lgb_clf, tree_index=1)
lgb中提供了另一个api plot_tree,使用matplotlib可视化树模型。效果上没有graphviz清楚。
import matplotlib.pyplot as plt
fig2 = plt.figure(figsize=(20, 20))
ax = fig2.subplots()
lgb.plot_tree(lgb_clf, tree_index=1, ax=ax)
plt.show()
print(‘在训练过程中绘图…’)
ax = lgb.plot_metric(evals_result, metric=‘l1’)
plt.show()
print(‘画出特征重要度…’)
ax = lgb.plot_importance(gbm, max_num_features=10)
plt.show()
print(‘画出第84颗树…’)
ax = lgb.plot_tree(gbm, tree_index=83, figsize=(20, 8), show_info=[‘split_gain’])
plt.show()
#print(‘用graphviz画出第84颗树…’)
#graph = lgb.create_tree_digraph(gbm, tree_index=83, name=‘Tree84’)
#graph.render(view=True)