Rambatino / CHAID

A python implementation of the common CHAID algorithm
Apache License 2.0
149 stars 50 forks source link

Not being able to visualize the tree #110

Closed diogoalvesderesende closed 4 years ago

diogoalvesderesende commented 4 years ago

Hi again,

Unfortunately, I cannot export the tree graph. Here is a reproducible example:

#library
from CHAID import Tree
X = np.array(([1, 2, 3] * 5) + ([2, 2, 3] * 5)).reshape(10, 3)
Y = arr = np.array(([1] * 5) + ([2] * 5))

#Start CHAID
model1 = Tree.from_numpy(X, Y, max_depth=4, min_child_node_size = 30)
model1.to_tree()

#visualize tree
#conda install -c plotly plotly-orca
import os
os.environ["PATH"] += os.pathsep + 'C:/ProgramData/Anaconda3/Library/bin/graphviz/'
model1.render(path=None, view=False)

This is the error I get:

model1.render(path=None, view=False)
Traceback (most recent call last):

  File "<ipython-input-15-795caf0b6056>", line 1, in <module>
    model1.render(path=None, view=False)

  File "C:\Users\diogo\anaconda3\lib\site-packages\CHAID\tree.py", line 291, in render
    Graph(self).render(path, view)

  File "C:\Users\diogo\anaconda3\lib\site-packages\CHAID\graph.py", line 77, in render
    g.render(path, view=view)

  File "C:\Users\diogo\anaconda3\lib\site-packages\graphviz\files.py", line 202, in render
    filepath = self.save(filename, directory)

  File "C:\Users\diogo\anaconda3\lib\site-packages\graphviz\files.py", line 166, in save
    with io.open(filepath, 'w', encoding=self.encoding) as fd:

OSError: [Errno 22] Invalid argument: 'trees\\2020-05-11 16:10:10.gv'

Could you help? thanks!

Rambatino commented 4 years ago

The double \ looks a little suspect.

Rambatino commented 4 years ago

Are you sure os.environ["PATH"] is what you want? I'm on OSX and that prints out all available paths in your shell env, which is not what you want.

Give it a literal path and tell me what it prints.

try:

model1.render(path='C:/ProgramData/Anaconda3/Library/bin/graphviz/file.gv', view=False)
Rambatino commented 4 years ago

Hi @diogoalvesderesende have you had a look at this anymore?

diogoalvesderesende commented 4 years ago

Hi @Rambatino , Thanks for the follow up. I have looked but did not work. I will give a deeper look and include here a reproducible example.

Rambatino commented 4 years ago

Okay thanks @diogoalvesderesende