在Jupyter笔记本中使用d3js更新了可折叠图的答案
笔记本第一个单元格的开始%%html
.node circle {
cursor: pointer;
stroke: #3182bd;
stroke-width: 1.5px;
}
.node text {
font: 10px sans-serif;
pointer-events: none;
text-anchor: middle;
}
line.link {
fill: none;
stroke: #9ecae1;
stroke-width: 1.5px;
}
笔记本第一个单元格结束
笔记本第二个单元格的开始
^{pr2}$
笔记本第二个单元格结束
graph2.json的内容{
"name": "flare",
"children": [
{
"name": "analytics"
},
{
"name": "graph"
}
]
}
图表
单击flare,它是根节点,其他节点将崩溃
参考文献
旧答案
我在Jupyter笔记本中找到了用于交互式可视化决策树的this tutorial here。在
安装graphviz
这有两个步骤:
步骤1:使用pip安装graphviz for pythonpip install graphviz
第二步:然后你必须单独安装graphviz。检查这个link。
然后根据您的系统操作系统,您需要相应地设置路径:
对于windows和Mac OS check this link。
对于Linux/Ubuntucheck this link
安装ipywidgets
使用pippip install ipywidgets
jupyter nbextension enable --py widgetsnbextension
使用condaconda install -c conda-forge ipywidgets
现在来看看代码from IPython.display import SVG
from graphviz import Source
from sklearn.datasets load_iris
from sklearn.tree import DecisionTreeClassifier, export_graphviz
from sklearn import tree
from ipywidgets import interactive
from IPython.display import display
加载数据集,例如本例中的iris datasetdata = load_iris()
#Get the feature matrix
features = data.data
#Get the labels for the sampels
target_label = data.target
#Get feature names
feature_names = data.feature_names
**绘制决策树的函数**def plot_tree(crit, split, depth, min_split, min_leaf=0.17):
classifier = DecisionTreeClassifier(random_state = 123, criterion = crit, splitter = split, max_depth = depth, min_samples_split=min_split, min_samples_leaf=min_leaf)
classifier.fit(features, target_label)
graph = Source(tree.export_graphviz(classifier, out_file=None, feature_names=feature_names, class_names=['0', '1', '2'], filled = True))
display(SVG(graph.pipe(format='svg')))
return classifier
调用函数decision_plot = interactive(plot_tree, crit = ["gini", "entropy"], split = ["best", "random"] , depth=[1, 2, 3, 4, 5, 6, 7], min_split=(0.1,1), min_leaf=(0.1,0.2,0.3,0.5))
display(decision_plot)
你会得到下面的图表
您可以通过更改以下值来交互更改输出单元格中的参数
相同数据但参数不同的另一个决策树
参考文献:
如果觉得《juyter显示决策树图形_在Jupyter Noteb中绘制交互式决策树》对你有帮助,请点赞、收藏,并留下你的观点哦!