栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

更改使用导出graphviz创建的决策树图的颜色

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

更改使用导出graphviz创建的决策树图的颜色

  • 您可以通过获取所有边缘的列表
    graph.get_edge_list()
  • 每个源节点应有两个目标节点,索引较低的一个被评估为True,索引较高的一个被评估为False。
  • 可以通过以下方式分配颜色

    set_fillcolor()

    import pydotplus
    from sklearn.datasets import load_iris
    from sklearn import tree
    import collections

    clf = tree.DecisionTreeClassifier(random_state=42)
    iris = load_iris()

    clf = clf.fit(iris.data, iris.target)

    dot_data = tree.export_graphviz(clf,
    feature_names=iris.feature_names,
    out_file=None,
    filled=True,
    rounded=True)
    graph = pydotplus.graph_from_dot_data(dot_data)

    colors = (‘brown’, ‘forestgreen’)
    edges = collections.defaultdict(list)

    for edge in graph.get_edge_list():
    edges[edge.get_source()].append(int(edge.get_destination()))

    for edge in edges:
    edges[edge].sort()
    for i in range(2):
    dest = graph.get_node(str(edges[edge][i]))[0]
    dest.set_fillcolor(colors[i])

    graph.write_png(‘tree.png’)


另外,我看过一些树,其中连接节点的线的长度
与拆分所解释的%方差成正比。
如果可能的话,我也希望能够做到这一点!

你可以玩set_weight()和set_len()但这是一个有点
棘手,需要一些摆弄得到它的权利,但这里是一些代码,让
你开始。

for edge in edges:    edges[edge].sort()    src = graph.get_node(edge)[0]    total_weight = int(src.get_attributes()['label'].split('samples = ')[1].split('<br/>')[0])    for i in range(2):        dest = graph.get_node(str(edges[edge][i]))[0]        weight = int(dest.get_attributes()['label'].split('samples = ')[1].split('<br/>')[0])        graph.get_edge(edge, str(edges[edge][0]))[0].set_weight((1 - weight / total_weight) * 100)        graph.get_edge(edge, str(edges[edge][0]))[0].set_len(weight / total_weight)        graph.get_edge(edge, str(edges[edge][0]))[0].set_minlen(weight / total_weight)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/645759.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号