在Jupyter Noteb中绘制交互式决策树

2024-10-01 15:32:51 发布

您现在位置:Python中文网/ 问答频道 /正文

在一个笔记本上,有一个可以交互地探索它的节点的方法吗?我在想这样的事情。这是KNIME的一个例子。在

我已经找到了https://planspace.org/20151129-see_sklearn_trees_with_d3/和{a3},我知道你可以在Jupyter中运行d3,但是我没有找到任何可以这样做的包。在


Tags: 方法httpsorg节点with笔记本sklearn事情
3条回答

在Jupyter笔记本中使用d3js更新了可折叠图的答案

笔记本第一个单元格的开始

%%html
<div id="d3-example"></div>
<style>

.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;
}
</style>

笔记本第一个单元格结束

笔记本第二个单元格的开始

^{pr2}$

笔记本第二个单元格结束

graph2.json的内容

   {
 "name": "flare",
 "children": [
  {
   "name": "analytics"
    },
    {
   "name": "graph"
    }
   ]
}

图表enter image description here

单击flare,它是根节点,其他节点将崩溃

enter image description here

此处使用的笔记本的Github存储库Collapsible tree in ipython notebook

参考文献

旧答案

我在Jupyter笔记本中找到了用于交互式可视化决策树的this tutorial here。在

安装graphviz

这有两个步骤: 步骤1:使用pip安装graphviz for python

pip install graphviz

第二步:然后你必须单独安装graphviz。检查这个link。 然后根据您的系统操作系统,您需要相应地设置路径:

对于windows和Mac OS check this link。 对于Linux/Ubuntucheck this link

安装ipywidgets

使用pip

pip install ipywidgets
jupyter nbextension enable --py widgetsnbextension

使用conda

conda 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 dataset

data = 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)

你会得到下面的图表 enter image description here

您可以通过更改以下值来交互更改输出单元格中的参数

enter image description here

相同数据但参数不同的另一个决策树enter image description here

参考文献:

1.如果您只想在Jupyter中使用D3,这里有一个教程:https://medium.com/@stallonejacob/d3-in-juypter-notebook-685d6dca75c8

enter image description here

enter image description here

2.为了构建一个交互式决策树,这里有另一个有趣的GUI工具箱TMVAGui。在

在这里,代码只是一行代码: factory.DrawDecisionTree(dataset, "BDT")

{a4}

有一个叫做pydot的模块。您可以创建图形并添加边来生成决策树。在

import pydot # 

graph = pydot.Dot(graph_type='graph')
edge1 = pydot.Edge('1', '2', label = 'edge1')
edge2 = pydot.Edge('1', '3', label = 'edge2')
graph.add_edge(edge1)
graph.add_edge(edge2)

graph.write_png('my_graph.png')

这是一个将输出决策树的png文件的示例。希望这有帮助!在

相关问题 更多 >

    热门问题