Filenotfounderror Rendering Decision Tree With 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 = Tre
Solution 1:
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
defGraph_render(self, path, view):
if path isNone:
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 isnotNone:
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)
defGraph_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]}],
},
}
ifnot node.is_terminal:
p = Noneif node.p isNoneelseformat(node.p, ".5f")
score = Noneif node.score isNoneelseformat(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
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.
I've turned this into a pull request to update the project itself.
Post a Comment for "Filenotfounderror Rendering Decision Tree With Chaid"