日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

python工具dtreeviz決策樹可視化和模型可解釋性_python

作者:Python學習與數據挖掘 ? 更新時間: 2022-05-04 編程語言

前言:

決策樹是梯度提升機和隨機森林的基本構建塊,在學習這些模型的工作原理和模型可解釋性時,可視化決策樹是一個非常有幫助。不過,當前的可視化包還很初級,對新手沒有多少幫助。

最近逛 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

欄目分類
最近更新