栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

决策树可视化 xgb、lgb可视化

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

决策树可视化 xgb、lgb可视化

目录

导包

决策树可视化方法

 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)

 

 xgb、lgb建模
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)

 xgb可视化

        其中num_trees为子树的索引

xgb.to_graphviz(xgb_model, num_trees=0, rankdir='UT')

 lgb可视化
lgb.create_tree_digraph(model, tree_index=0,encoding='UTF-8')

 

 

关注公众号Python风控模型与数据分析,更多知识、代码分享

往期精彩文章

Catboost原理详解

Catboost参数详解及实战

模型可解释性-shap value原理及实战

pyecarts动态交互图表-可视化大屏

pyecharts动态图表嵌入ppt

xgboost原理(无推导就轻易理解)

lgb实战风控算法赛

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1033510.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号