You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, there are two options to get the decision tree representations: export_graphviz and export_text.
I would like to add export_dict, which will output the decision as a nested dictionary.
While you can convert the graphviz representation using cli tools, it's a bit unruly and is a weird workflow. When converting back, the nesting is also kind of weird, with /n chars floating around.
The main utility for json/dict representations are
That they're far easier to work with for any front-end work.
Working with a dict allows you to use the representation easily for any downstream tasks using the decision tree (that aren't raw inference).
Reacted by Luiz Eduardo Amaral, Pakshal Shah and Lucas Morin
@jwilber Can you describe your desired data structure of a tree with multiple branches please?
(I'm the developer of the sklearn-porter which solves similar issues.)
I was opening an issue here to see if there’s any interest - if so I’d open a PR. If you think your lib is a good fit lmk and I can either open a pr or send you the code!
A core developer can answer your question whether it's possible to add a export_dict method to export model data or not I guess. A purpose is to use the data and to visualize it individually.
The porter can already exports the model data of a decicion tree and tanspiles it to another programming language.
Finally I can give you some feedback to your visualization. Great work, but what are the advantages in comparison to the output of the export_graphviz method? For me it's really difficult to compare the values visually. The branches have all the same width and color. The font size is very small. The visualization isn't compact, so how would it looks like if the tree is very large with many features and classes? I think the main advantage of your work is that we can make the visualization interactive and more useable for analysis. What do you think?
Sorry, this whole interaction is very confusing to me. I’m just trying to see if sklearn devs desire a PR as described or not. I’m not sure where your library fits here?
I appreciate the feedback! But the visual I linked is just a 10 minute chunk of d3 code to verify that the function’s output will work for some arbitrary tree generated from sklearn. Not a project itself, and outside the scope of this issue.
Reasons for not wanting export_graphviz detailed in comment one.
I see export_dict as another way to serialize and inspect the tree without having to go through Python object. Going through the Python object can be a bit involved as demonstrated by our example. Other libraries has similar functions to serialized trees into something human readable such as: LightGBM's model_to_string or XGBoost's Model IO.
I am overall +0.5 on adding this to sklearn. It has secondary benefit of making it a little easier to export the trees into another format. I suggest we wait to see other's opinions before working on this.
Reacted by Darius Morawiec, Jared Wilber and Lucas Morin
The main reason this feature would be useful for me is that I can easily port decision trees to other places. I already put some trees in web applications and microcontrollers. I would love to help if this would benefit the community as well.
For an industry use-case, this would it easier to export trees to text for transmission over protocols such as MQTT to edge devices, which may need to perform inference on the tree but not have scikit-learn. While export_text (or other methods) can produce outputs that are relatively simple to parse, a dict that could be JSON-serialized would be extremely simple and portable.
I've recently found myself in need of this and wrote some custom function to do it.... I've even pushed it to write sql inference code. Some standard json extraction would be a good start.
I've figured I could do the PR for this since it would be quite close to export text, with an option to dump it into a json... Maybe some of the code (tests) could be refactored so that its doesn't feel like a copy paste.
Activity
ayush714 commentedon May 6, 2021
Can I work on this feature?
nok commentedon May 16, 2021
@jwilber Can you describe your desired data structure of a tree with multiple branches please?
(I'm the developer of the sklearn-porter which solves similar issues.)
jwilber commentedon May 16, 2021
I’ve written a method that yields the same result as
export_graphviz, but in a nested dict.See output example here (though the data here is slightly massaged to work better with d3): https://observablehq.com/@jwilber/visualize-decision-trees-from-sklearn
I was opening an issue here to see if there’s any interest - if so I’d open a PR. If you think your lib is a good fit lmk and I can either open a pr or send you the code!
nok commentedon May 16, 2021
A core developer can answer your question whether it's possible to add a
export_dictmethod to export model data or not I guess. A purpose is to use the data and to visualize it individually.The
portercan already exports the model data of a decicion tree and tanspiles it to another programming language.Finally I can give you some feedback to your visualization. Great work, but what are the advantages in comparison to the output of the
export_graphvizmethod? For me it's really difficult to compare the values visually. The branches have all the same width and color. The font size is very small. The visualization isn't compact, so how would it looks like if the tree is very large with many features and classes? I think the main advantage of your work is that we can make the visualization interactive and more useable for analysis. What do you think?jwilber commentedon May 16, 2021
Sorry, this whole interaction is very confusing to me. I’m just trying to see if sklearn devs desire a PR as described or not. I’m not sure where your library fits here?
I appreciate the feedback! But the visual I linked is just a 10 minute chunk of d3 code to verify that the function’s output will work for some arbitrary tree generated from sklearn. Not a project itself, and outside the scope of this issue.
Reasons for not wanting
export_graphvizdetailed in comment one.thomasjpfan commentedon May 17, 2021
I see
export_dictas another way to serialize and inspect the tree without having to go through Python object. Going through the Python object can be a bit involved as demonstrated by our example. Other libraries has similar functions to serialized trees into something human readable such as: LightGBM's model_to_string or XGBoost's Model IO.I am overall +0.5 on adding this to sklearn. It has secondary benefit of making it a little easier to export the trees into another format. I suggest we wait to see other's opinions before working on this.
luxedo commentedon Jul 1, 2021
Hi! I just like to add the description of the issue I just posted.
The main reason this feature would be useful for me is that I can easily port decision trees to other places. I already put some trees in web applications and microcontrollers. I would love to help if this would benefit the community as well.
ogrisel commentedon Jun 29, 2023
I would rather be in favor of returning a dict of numpy arrays instead of a list/array of dicts.
Either outputs can then be turned into a pandas dataframe easily.
JustinDula commentedon May 6, 2024
This would be a useful feature.
For an industry use-case, this would it easier to export trees to text for transmission over protocols such as MQTT to edge devices, which may need to perform inference on the tree but not have scikit-learn. While export_text (or other methods) can produce outputs that are relatively simple to parse, a dict that could be JSON-serialized would be extremely simple and portable.
lcrmorin commentedon Mar 1, 2026
I've recently found myself in need of this and wrote some custom function to do it.... I've even pushed it to write sql inference code. Some standard json extraction would be a good start.
lcrmorin commentedon Aug 24, 2026
I've figured I could do the PR for this since it would be quite close to export text, with an option to dump it into a json... Maybe some of the code (tests) could be refactored so that its doesn't feel like a copy paste.