目录
导包
决策树可视化方法
xgb、lgb建模
xgb可视化
lgb可视化
from sklearn.datasets import load_iris
from sklearn.tree import plot_tree
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
1、不需要安装graphviz软件包
鉴于实际建模应用往往是xgb、lgb等,决策树更多用于分析,所以此处将决策树建模与可视化封装在一起,方便实用
def DecisionTree_plot(x,y,feature_names=None,target_names=None,max_depth=3,min_samples_leaf=10):
clf = DecisionTreeClassifier(max_depth=max_depth,min_samples_leaf=min_samples_leaf).fit(x,y)
plt.figure(dpi=100,figsize=(8,8))
plot_tree(clf, filled=True,rounded=True,feature_names=feature_names,
class_names=target_names)
plt.show()
iris = load_iris()
DecisionTree_plot(iris.data,iris.target,iris.feature_names,iris.target_names)
可以看到,使用原生方法可视化的结果还是比较简陋的
2、 决策树可视化方法2,需要安装graphviz软件包
import graphviz
def DecisionTree_plot2(x,y,feature_names=None,target_names=None,max_depth=3,min_samples_leaf=10):
clf = DecisionTreeClassifier(max_depth=max_depth,min_samples_leaf=min_samples_leaf).fit(x,y)
dot_data = tree.export_graphviz(clf,
feature_names=iris.feature_names,
class_names=iris.target_names,
filled=True, rounded=True,
special_characters=True)
graph = graphviz.Source(dot_data)
return graph
DecisionTree_plot2(iris.data,iris.target,iris.feature_names,iris.target_names)
import xgboost as xgb
import pandas as pd
import numpy as np
df=pd.read_excel('data.xlsx')
xgb_df = xgb.DMatrix(df.drop('y',axis=1), label =df.y)
lgb_df =lgb.Dataset(df.drop('y',axis=1), label =df.y)
param = {'max_depth':3, 'eta':0.2, 'min_child_weight':50}
xgb_model = xgb.train(param, dtrain)
lgb_model = lgb.train(param, dtrain)
其中num_trees为子树的索引
xgb.to_graphviz(xgb_model, num_trees=0, rankdir='UT')
lgb.create_tree_digraph(model, tree_index=0,encoding='UTF-8')
往期精彩文章
Catboost原理详解
Catboost参数详解及实战
模型可解释性-shap value原理及实战
pyecarts动态交互图表-可视化大屏
pyecharts动态图表嵌入ppt
xgboost原理(无推导就轻易理解)
lgb实战风控算法赛