Skip to content

plot tree with high cardinality featureΒ #5687

Closed

Description

@moziada

When I try to plot a tree that splits on a feature with high cardinality the overall diagram gets missed up no matter how I tried to adjust the width and height
out
There must be a way to ignore writing all the values that are used on the split within the nodes

Activity

  1. jmoralez commented on Feb 12, 2023

    @jmoralez
    Collaborator

    Hi @moziada, thanks for using LightGBM. I think a possible solution would be to check the number of categories in the split and if there are too many we should collapse them and show them in the tooltip instead, which would look like this:
    Screenshot from 2023-02-12 16-30-37
    The problem is that at least for me (firefox + jupyterlab) the graph isn't automatically shown as SVG, so I had to save it first and then load it and show as HTML which is cumbersome. We should find a way to display it as SVG by default, I can look into that later, or if you're interested you can investigate and contribute the change yourself. The logic for showing it in the tooltip is very simple, here's a sample diff:

    diff --git a/python-package/lightgbm/plotting.py b/python-package/lightgbm/plotting.py
    index d2a57209..87c287c0 100644
    --- a/python-package/lightgbm/plotting.py
    +++ b/python-package/lightgbm/plotting.py
    @@ -493,7 +493,13 @@ def _to_graphviz(
                         direction = _determine_direction_for_numeric_split(
                             example_case[split_feature], root['threshold'], root['missing_type'], root['default_left']
                         )
    -            label += f"<B>{_float2str(root['threshold'], precision)}</B>"
    +            if is_categorical_split and len(root['threshold'].split('||')) > 5:
    +                threshold = '...'
    +                tooltip = root['threshold']
    +            else:
    +                threshold = root['threshold']
    +                tooltip = None
    +            label += f"<B>{_float2str(threshold, precision)}</B>"
                 for info in ['split_gain', 'internal_value', 'internal_weight', "internal_count", "data_percentage"]:
                     if info in show_info:
                         output = info.split('_')[-1]
    @@ -525,7 +531,8 @@ def _to_graphviz(
                 if "data_percentage" in show_info:
                     label += f"<br/>{_float2str(root['leaf_count'] / total_count * 100, 2)}% of data"
                 label = f"<{label}>"
    -        graph.node(name, label=label, shape=shape, style=style, fillcolor=fillcolor, color=color, penwidth=penwidth)
    +            tooltip = None
    +        graph.node(name, label=label, shape=shape, style=style, fillcolor=fillcolor, color=color, penwidth=penwidth, tooltip=tooltip)
             if parent is not None:
                 graph.edge(parent, name, decision, color=color, penwidth=penwidth)
  2. github-actions commented on Mar 15, 2023

    @github-actions
    Contributor

    This issue has been automatically closed because it has been awaiting a response for too long. When you have time to to work with the maintainers to resolve this issue, please post a new comment and it will be re-opened. If the issue has been locked for editing by the time you return to it, please open a new issue and reference this one. Thank you for taking the time to improve LightGBM!

  3. jmoralez commented on Mar 15, 2023

    @jmoralez
    Collaborator

    I shouldn't have set the awaiting response for this, it's a valid issue and I think should be fixed. I'm reopening.

  4. github-actions commented on Mar 16, 2023

    @github-actions
    Contributor

    This issue has been automatically closed because it has been awaiting a response for too long. When you have time to to work with the maintainers to resolve this issue, please post a new comment and it will be re-opened. If the issue has been locked for editing by the time you return to it, please open a new issue and reference this one. Thank you for taking the time to improve LightGBM!

  5. moziada commented on Mar 16, 2023

    @moziada
    ContributorAuthor

    Hey @jameslamb sorry I did not receive your response earlier, I will look into it and try to make a contribution.
    I will keep you with any updates

  6. moziada commented on Mar 26, 2023

    @moziada
    ContributorAuthor

    After searching I have found that this is a common issue with JupyterLab and people have opened issues on it (like this issuse).
    The workaround to display svg images on JupyterLab is to wrap the svg image content with IPython.display.HTML then the tooltip works properly
    JupyterLab

    Also I have tested the tooltip on Jupyter Notebook it works fine no workarounds are needed
    Jupyter Notebook
    Both screenshots are taken on Firefox browser

  7. moziada commented on Mar 28, 2023

    @moziada
    ContributorAuthor

    @jmoralez sorry I forgot to mention you with my results

  8. jmoralez commented on Mar 30, 2023

    @jmoralez
    Collaborator

    I think this is a good feature. We can maybe add an argument with the maximum number of thresholds to show and in the description of that argument specify that the user may need to use the IPython.display.HTML in jupyterlab. Do you want to submit a PR with that?

  9. moziada commented on Mar 31, 2023

    @moziada
    ContributorAuthor

    Sure I am working on a PR

  10. moziada commented on Apr 1, 2023

    @moziada
    ContributorAuthor

    I am having difficulties on testing the changes I have made, is there any systematic way to make changes and install the new version to the current python environment?

  11. jameslamb commented on Apr 2, 2023

    @jameslamb
    Member

    Since you're only touching Python code, compile the library just one time by running the following from the root of the repo.

    rm -r ./build
    mkdir ./build
    cd ./build
    cmake ..
    make -j2

    Then every time you change the Python package, run this from the root of the repo.

    cd ./python-package
    python setup.py install --precompile

    Then every time you want to run the tests, point pytest at any of the directories and files in the tests directory. For example, by runming the following from the root of the repo.

    pytest tests/python_package_test/test_plotting.py
  12. moziada commented on Apr 3, 2023

    @moziada
    ContributorAuthor

    I have made the changes but I noticed that all .rst doc pages under Plotting section are not available on github

  13. jameslamb commented on Apr 3, 2023

    @jameslamb
    Member

    That page is automatically generared from the docstrings in python-package/lightgbm/plotting.py.

  14. jmoralez commented on Apr 4, 2023

    @jmoralez
    Collaborator

    You can also render the docs locally following this.

  15. moziada commented on Apr 4, 2023

    @moziada
    ContributorAuthor

    Thanks I have managed to render it correctly and it seems fine to me, I have opened pull request here #5818

  16. added a commit that references this issue on Jun 10, 2023
    15e3aec
  17. github-actions commented on Sep 13, 2023

    @github-actions
    Contributor

    This issue has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.

  18. locked as resolved and limited conversation to collaborators on Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions