带有CHAID的FileNotFoundError呈现决策树 [英] FileNotFoundError rendering decision tree with CHAID

查看:61
本文介绍了带有CHAID的FileNotFoundError呈现决策树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码获取CHAID的决策树

I used the following code to get the decision tree of CHAID

independent_variable_columns = ['gender', 'grade', 'no_renewals', 'complaint_count']
dep_variable = 'switch'
tree = Tree.from_pandas_df(
    df,
    dict(zip(independent_variable_columns, ['nominal'] * 38)),
    dep_variable,
    max_depth=2
)
tree.to_tree()
tree.render()

但是出现以下错误

  File "C:\Users\data\AppData\Local\Continuum\anaconda3\lib\site-packages\CHAID\graph.py", line 93, in <listcomp>
    [os.remove(file) for file in self.files]
FileNotFoundError: [WinError 3] The system cannot find the path specified: '/tmp/157840175993116164207458496094.png'

推荐答案

在生成文件名和处理临时文件时,该特定库未​​实现最佳实践.这使他们的代码在Windows上以多种方式中断.

That specific library is not implementing best practices when it comes to generating filenames and handling temporary files. That makes their code break, in a variety of ways, on Windows.

我对具有相同根本原因的类似问题发表了评论,提出更改以解决这些问题.

I've left a comment on a similar issue with the same root cause, proposing changes to fix these issues.

您可以通过运行以下代码在本地修复此问题:

You can fix this locally by running this code:

import os
from datetime import datetime
from tempfile import TemporaryDirectory

import plotly.io as pio
import colorlover as cl
from graphviz import Digraph

from CHAID import graph

def Graph_render(self, path, view):
    if path is None:
        path = os.path.join("trees", f"{datetime.now():%Y-%m-%d %H:%M:%S}.gv")
    with TemporaryDirectory() as self.tempdir:
        g = Digraph(
            format="png",
            graph_attr={"splines": "ortho"},
            node_attr={"shape": "plaintext", "labelloc": "b"},
        )
        for node in self.tree:
            image = self.bar_chart(node)
            g.node(str(node.node_id), image=image)
            if node.parent is not None:
                edge_label = f"     ({', '.join(map(str, node.choices))})     \n ")
                g.edge(str(node.parent), str(node.node_id), xlabel=edge_label)

        g.render(path, view=view)

def Graph_bar_chart(self, node):
    fig = {
        "data": [
            {
                "values": list(node.members.values()),
                "labels": list(node.members),
                "showlegend": node.node_id == 0,
                "domain": {"x": [0, 1], "y": [0.4, 1.0]},
                "hole": 0.4,
                "type": "pie",
                "marker_colors": cl.scales["5"]["qual"]["Set1"],
            },
        ],
        "layout": {
            "margin_t": 50,
            "annotations": [{"font_size": 18, "x": 0.5, "y": 0.5}, {"y": [0, 0.2]}],
        },
    }

    if not node.is_terminal:
        p = None if node.p is None else format(node.p, ".5f")
        score = None if node.score is None else format(node.score, ".2f")
        self.append_table(fig, [p, score, node.split.column])

    filename = os.path.join(self.tempdir, f"node-{node.node_id}.png")
    pio.write_image(fig, file=filename, format="png")
    return filename

graph.Graph.render = Graph_render
graph.Graph.bar_chart = Graph_bar_chart

以上是该库中 Graph 类的两种方法的重构,切换为使用 tempdir.TemporaryDirectory()处理临时文件,并清理了如何生成文件名.我还自由地清理了代码中的一些小问题.

The above is a refactor of two methods from the Graph class in that library, switching to using tempdir.TemporaryDirectory() to handle temporary files, and cleaning up how filenames are generated. I also took the liberty to clean up some minor issues in the code.

我已将其转换为拉动请求以更新项目本身.

这篇关于带有CHAID的FileNotFoundError呈现决策树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆