網站首頁 編程語言 正文
前言:
決策樹是梯度提升機和隨機森林的基本構建塊,在學習這些模型的工作原理和模型可解釋性時,可視化決策樹是一個非常有幫助。不過,當前的可視化包還很初級,對新手沒有多少幫助。
最近逛 Github 時,發現一款非常棒的 dtreeviz 工具庫:它用于決策樹可視化和模型解釋。使用 dtreeviz 可以可視化特征空間如何在決策節點上分割,訓練樣本如何分布在葉節點中,樹如何對特定觀察進行預測等等。這些操作對于理解分類或回歸決策樹的工作方式至關重要。
一、安裝
pip install dtreeviz ? ? ? ? ? ? # install dtreeviz for sklearn pip install dtreeviz[xgboost] ? ?# install XGBoost related dependency pip install dtreeviz[pyspark] ? ?# install pyspark related dependency pip install dtreeviz[lightgbm] ? # install LightGBM related dependency
二、用法
dtree:創建決策樹可視化的主要功能。給定決策樹回歸器或分類器,使用 graphviz 創建并返回樹可視化。
1.所需的庫
導入所需要的基本庫
from sklearn.datasets import * from sklearn import tree from dtreeviz.trees import *
2.回歸決策樹
樹的默認方向是自上而下,但您可以使用orientation=“LR”
將其更改為從左到右。view() 給出一個帶有渲染的 graphviz
對象的彈出窗口。
regr = tree.DecisionTreeRegressor(max_depth=2) boston = load_boston() regr.fit(boston.data, boston.target) viz = dtreeviz(regr, ? ? ? ? ? ? ? ?boston.data, ? ? ? ? ? ? ? ?boston.target, ? ? ? ? ? ? ? ?target_name='price', ? ? ? ? ? ? ? ?feature_names=boston.feature_names) ? ? ? ? ? ? ?? viz.view() ? ?
3.分類決策樹
分類樹需要class_names
的附加參數,給出類值與類名的映射。
classifier = tree.DecisionTreeClassifier(max_depth=2) ?# limit depth of tree iris = load_iris() classifier.fit(iris.data, iris.target) viz = dtreeviz(classifier,? ? ? ? ? ? ? ? ?iris.data,? ? ? ? ? ? ? ? ?iris.target, ? ? ? ? ? ? ? ?target_name='variety', ? ? ? ? ? ? ? ?feature_names=iris.feature_names,? ? ? ? ? ? ? ? ?class_names=["setosa", "versicolor", "virginica"] ?# need class_names for classifier ? ? ? ? ? ? ? ) ? ? ? ? ? ? ? ?? viz.view()?
4.預測路徑
突出顯示參數 X 中傳遞的單個觀察的特征值所在的決策節點。給出觀察的特征值并突出樹用于遍歷路徑的特征。
regr = tree.DecisionTreeRegressor(max_depth=2) ?# limit depth of tree diabetes = load_diabetes() regr.fit(diabetes.data, diabetes.target) X = diabetes.data[np.random.randint(0, len(diabetes.data)),:] ?# random sample from training viz = dtreeviz(regr, ? ? ? ? ? ? ? ?diabetes.data,? ? ? ? ? ? ? ? ?diabetes.target,? ? ? ? ? ? ? ? ?target_name='value',? ? ? ? ? ? ? ? ?orientation ='LR', ?# left-right orientation ? ? ? ? ? ? ? ?feature_names=diabetes.feature_names, ? ? ? ? ? ? ? ?X=X) ?# need to give single observation for prediction ? ? ? ? ? ? ?? viz.view() ?
如果只想可視化預測路徑,則需要設置參數show_just_path=True
dtreeviz(regr, ? ? ? ? diabetes.data,? ? ? ? ? diabetes.target,? ? ? ? ? target_name='value',? ? ? ? ? orientation ='TD', ?# top-down orientation ? ? ? ? feature_names=diabetes.feature_names, ? ? ? ? X=X, # need to give single observation for prediction ? ? ? ? show_just_path=True ? ?? ? ? ? ? )
5.解釋預測路徑
這些可視化對于向沒有機器學習技能的人解釋為什么您的模型做出特定預測很有用。在explain_type=plain_english
的情況下,它在預測路徑中搜索并找到特征值范圍。
X = dataset[features].iloc[10] print(X) Pclass ? ? ? ? ? ? ?3.0 Age ? ? ? ? ? ? ? ? 4.0 Fare ? ? ? ? ? ? ? 16.7 Sex_label ? ? ? ? ? 0.0 Cabin_label ? ? ? 145.0 Embarked_label ? ? ?2.0 print(explain_prediction_path(tree_classifier, X, feature_names=features, explanation_type="plain_english")) 2.5 <= Pclass? Age < 36.5 Fare < 23.35 Sex_label < 0.5
在explain_type=sklearn_default
(僅適用于scikit-learn)的情況下,我們可以僅可視化預測路徑中涉及的特征的重要性。 特征的重要性是基于雜質的平均減少來計算的。
explain_prediction_path(tree_classifier, X, feature_names=features, explanation_type="sklearn_default")
此外我們還可以自定義顏色,比如:
dtreeviz.trees.dtreeviz(regr, ? ? ? ? ? ? ? ? ? ? ? ? boston.data, ? ? ? ? ? ? ? ? ? ? ? ? boston.target, ? ? ? ? ? ? ? ? ? ? ? ? target_name='price', ? ? ? ? ? ? ? ? ? ? ? ? feature_names=boston.feature_names, ? ? ? ? ? ? ? ? ? ? ? ? colors={'scatter_marker': '#00ff00'})
原文鏈接:https://blog.csdn.net/weixin_38037405/article/details/121755696
相關推薦
- 2023-03-16 python使用redis實現消息隊列(異步)的實現完整例程_python
- 2022-09-28 python?Scala函數與訪問修辭符實例詳解_python
- 2023-07-09 SQL Server中的NULL值處理:判斷與解決方案
- 2022-04-14 Python中字典的相關操作介紹_python
- 2023-10-25 對于Echarts實例化與銷毀的一些運用
- 2022-01-18 微信小程序以post方式提交
- 2023-02-10 rust引用和借用的使用小結_Rust語言
- 2022-11-16 通用?HTTP?簽名組件的另類實現方式_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支