前言:
决策树是梯度提升机和随机森林的基本构建块,在学习这些模型的工作原理和模型可解释性时,可视化决策树是一个非常有帮助。不过,当前的可视化包还很初级,对新手没有多少帮助。
最近逛 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'})
到此这篇关于python工具dtreeviz决策树可视化和模型可解释性的文章就介绍到这了,更多相关python工具dtreeviz内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
查看更多关于python工具dtreeviz决策树可视化和模型可解释性的详细内容...