本教程教萌新如何使用lightgbm里面可视化函数
作者:JasonChen
文章内容概览:
- 保留训练结果
- plot_metric()函数的使用(重要)
- plot_importance()函数的使用(重要)
- plot_tree()函数的使用(可选)
- create_tree_digraph()函数的使用(可选)
依赖:
- python3
- lightgbm
- graphviz(可选)
- matplotlib
graphviz安装教程
-
1.下载安装包到本地-----可爱的传送门graphviz的官方安装地址
-
2.下载之后解压并选择解压目录
-
3.配置环境变量:将bin目录添加到Path环境变量中。
-
4.打开cmd(win+R) , 输入dot -version,如果显示如下,则表示安装成功
结果预览
- plot_metric
- plot_importance
- plot_tree()
- create_tree_digraph
代码块
import lightgbm as lgb
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
print('数据...')
x_train = np.random.random((1000,10))
y_train = np.random.rand(1000)>0.5
x_test = np.random.random((100,10))
y_test = np.random.randn(100)>0.5
# 导入到lightgbm矩阵
lgb_train = lgb.Dataset(x_train, y_train)
lgb_test = lgb.Dataset(x_test, y_test, reference=lgb_train)
# 设置参数
params = {
'num_leaves': 5,
'metric': ('auc', 'logloss'),#可以设置多个评价指标
'verbose': 0
}
# if (evals_result and gbm) not in locbals():
# global evals_result,gbm
#如果是局部变量的话,推荐把他们变成全局变量,这样plot的代码位置不受限制
evals_result = {} #记录训练结果所用
print('开始训练...')
# train
gbm = lgb.train(params,
lgb_train,
num_boost_round=100,
valid_sets=[lgb_train, lgb_test],
evals_result=evals_result,#非常重要的参数,一定要明确设置
verbose_eval=10)
print('画出训练结果...')
ax = lgb.plot_metric(evals_result, metric='auc')#metric的值与之前的params里面的值对应
plt.show()
print('画特征重要性排序...')
ax = lgb.plot_importance(gbm, max_num_features=10)#max_features表示最多展示出前10个重要性特征,可以自行设置
plt.show()
print('Plot 3th tree...') # 画出决策树,其中的第三颗
ax = lgb.plot_tree(gbm, tree_index=3, figsize=(20, 8), show_info=['split_gain'])
plt.show()
print('导出决策树的pdf图像到本地')#这里需要安装graphviz应用程序和python安装包
graph = lgb.create_tree_digraph(gbm, tree_index=3, name='Tree3')
graph.render(view=True)
本文完
官网的参考连接如下:
(https://github.com/Microsoft/LightGBM/blob/master/examples/python-guide/plot_example.py)