dtreeviz icon indicating copy to clipboard operation
dtreeviz copied to clipboard

dtreeviz fails with IndexError for n_classes >= 11

Open CermakM opened this issue 6 years ago • 16 comments

Apparently dtreeviz in dtreeviz.trees.py fails on the following block of code:

> /home/macermak/.local/lib/python3.6/site-packages/dtreeviz/trees.py(320)dtreeviz()
    318 
    319     n_classes = shadow_tree.nclasses()
--> 320     color_values = color_blind_friendly_colors[n_classes]
    321 
    322     # Fix the mapping from target value to color for entire tree

resulting in

IndexError: list index out of range

My model has 15 classes and I believe the length of color_blind_friendly_colors (which is 11) is causing the issue.

CermakM avatar Dec 03 '18 20:12 CermakM

ah. yeah, sorry. I should put a check in there but only 10 classes can be handled right now. we had to handpick the colors. sorry about that.

parrt avatar Dec 03 '18 20:12 parrt

Wow, quick response!

So there is nothing I can do at the moment?

CermakM avatar Dec 03 '18 20:12 CermakM

Nope :( sorry.

parrt avatar Dec 03 '18 20:12 parrt

Okay, let's hope for a quick solution in the bright future. Keep up the good work and thanks for the quick response.

CermakM avatar Dec 03 '18 20:12 CermakM

thanks :) I'd be happy to accept a pull request that increased the number of color palettes ;)

parrt avatar Dec 03 '18 20:12 parrt

It's a bit more tricky than just having more colors.

get_num_bins gets called with n_classes as well and it references NUM_BINS which also only has 10 values. What would be a good default value for NUM_BINS for n_classes > 10?

Any good ideas about having more colors? Just repeating the colors and giving a warning, or making up new colors?

Ashafix avatar Jun 21 '19 19:06 Ashafix

Yeah, That's the problem. I started running out of ideas for colors and default values above 10. May be a sequential color map is required.

parrt avatar Jun 21 '19 21:06 parrt

Currently running into the same issue for an idea I am testing. But totally understandable. Maybe a semi-simple and quick workaround is just to declare a simple target to color hex dict or just allowing us to passing in a list of colors.

As in the user declares the targets to color dict on their end.

{0: "#fcba03", 1: "#a303a9"}

EricCacciavillani avatar Mar 11 '20 18:03 EricCacciavillani

Any solution? I even considered not using colors, but when I add None to the color_blind_friendly_colors array (colors.py), I get the following error: color_map = {v: color_values[i] for i, v in enumerate(class_values)}

TypeError: 'NoneType' object is not subscriptable

chenhajaj avatar May 24 '20 08:05 chenhajaj

In principle, just add another list of colors so this works:

color_values = color_blind_friendly_colors[n_classes]

parrt avatar May 24 '20 16:05 parrt

In principle, just add another list of colors so this works:

color_values = color_blind_friendly_colors[n_classes]

I have 30 classes. I don't really care about colors, they can all be represented by the same color. Can I make it work?

chenhajaj avatar May 24 '20 16:05 chenhajaj

See if you can set dtreeviz.color_blind_friendly_colors[30+1] = [c]*30 for some color c.

parrt avatar May 24 '20 16:05 parrt

See if you can set dtreeviz.color_blind_friendly_colors[30+1] = [c]*30 for some color c.

Worked, thanks :) Had to change the NUM_BINS array as well

chenhajaj avatar May 24 '20 19:05 chenhajaj

Maybe allow to represent only certainty of the written class via the hue. Then you can either have hue="class_certainty" or hue="certainty", and it switches to hue="certainty" above 10 classes and probably drops a warning that it did.

benelot avatar Jul 17 '21 09:07 benelot

Hi folks, if you still need a solution for this issue, you can follow this PR https://github.com/parrt/dtreeviz/pull/185

tlapusan avatar Jun 13 '22 07:06 tlapusan

@tlapusan tla, @parrt

Hi, I really appreciate the work making support on more classes.

I now have a 86 classes decision tree model. Is it any possible way to call decision_boundaries without color issue?( "Index Error")

Or It's a bad idea having 86 classes , should I make my classes smaller?

0ptimista avatar Mar 24 '23 02:03 0ptimista