转:lightGBM的黑科技--plot函数
生活随笔
收集整理的這篇文章主要介紹了
转:lightGBM的黑科技--plot函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本來想研究一下lightGBM的plotting相關的接口,發現網上已經有人做了,而且還挺不錯的(lightGBM的黑科技--plot函數),就直接給轉過來了
# -*- coding: utf-8 -*-
# @Time : 2018/6/11
# @Author : Reynold chen
import lightgbm as lgb
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, # 非常重要的參數,一定要明確設置,輸出的結果是上面一個參數valid_sets配置的值
verbose_eval=10)
print(evals_result)
print('畫出訓練結果...')
ax = lgb.plot_metric(evals_result, metric='auc') # metric的值與之前的params里面的值對應
plt.show()
print('畫特征重要性排序...')
lgb.plot_importance(gbm, max_num_features=10) # max_features表示最多展示出前10個重要性特征,可以自行設置
plt.show()
print('Plot 3th tree...') # 畫出決策樹,其中的第三顆
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://blog.csdn.net/jason160918/article/details/80973448
總結
以上是生活随笔為你收集整理的转:lightGBM的黑科技--plot函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: calayer动画总结(一)
- 下一篇: Selector 概念