09_tabular. Using dtreeviz

Hi

Can anybody offer any suggestions on using dtreeviz from within a kaggle notebook?

This

!pip install dtreeviz
from dtreeviz.trees import dtreeviz

results in an ImportError

cannot import name ‘dtreeviz’ from ‘dtreeviz.trees’ (/opt/conda/lib/python3.7/site-packages/dtreeviz/trees.py)

Googling suggests this is a not uncommon problem with kaggle notebooks and I can’t find a solution that works for me

Many thanks

All the best

Julian

I am not familiar with dtreeviz, but there is apparently no module dtreeviz in the module dtreeviz.trees. That would also by a weird naming.

Either import everything by using *.

from dtreeviz.trees import *

or specify an existing module

from dtreeviz.trees import ShadowDecTree

Hello Tobias

Many thanks for your reply - much appreciated.

It’s very strange

If I try to import everything, it seems nothing gets imported

Many of the examples I’ve googled suggest there should be a module called dtreeviz

Please could you let me know how I can check what modules are available in dtreeviz.trees

All the best

Julian

I guess an older version of dtreeviz.trees had dtreeviz. Try to install an older version of dtreeviz or modernize that old import. I think the latter would be better.

@dzy82f I haven’t found a proper documentation yet, but some notebooks are available at GitHub - parrt/dtreeviz: A python library for decision tree visualization and model interpretation.. Maybe that helps. Good luck

Hello Tobias

Sadly, it works in Colab but not in kaggle

Many thanks for your help

All the best

Julian

I still can’t get this working in kaggle but it does work in Colab

if 'google.colab' in sys.modules:
  !pip install -q dtreeviz

At the same time the API has changed so

samp_idx = np.random.permutation(len(y))[:500]
dtreeviz(m, xs.iloc[samp_idx], y.iloc[samp_idx], xs.columns, dep_var,
        fontname='DejaVu Sans', scale=1.6, label_fontsize=10,
        orientation='LR')

needs to be changed to

samp_idx = np.random.permutation(len(y))[:500]

viz_model=dtreeviz.model(m,
                         X_train=xs.iloc[samp_idx],
                         y_train=y.iloc[samp_idx],
                         feature_names=xs.columns,
                         target_name=dep_var)

viz_model.view(fontname='DejaVu Sans', scale=1.6, label_fontsize=10,
               orientation='LR')

to avoid “UserWarning: X does not have valid feature names”

change

m = DecisionTreeRegressor(max_leaf_nodes=4)
m.fit(xs, y);

to

m = DecisionTreeRegressor(max_leaf_nodes=4)
m.fit(xs.values, y.values);
4 Likes

This works in kaggle. I am using dtreeviz 2.2.1
import dtreeviz
samp_idx = np.random.permutation(len(y))[:500]
viz_model=dtreeviz.model(m,
X_train=xs.iloc[samp_idx],
y_train=y.iloc[samp_idx],
feature_names=xs.columns,
target_name=dep_var)

viz_model.view(fontname=‘DejaVu Sans’, scale=1.6, label_fontsize=10,
orientation=‘LR’)

2 Likes

Hi Pawan
Many thanks
All the best
Julian

1 Like

well it worked !!! the first time i used .model it gave me an error . but your code did work

Dtreeviz is a module in this case. if you want to use dtreeviz method, you have to call dtreeviz.dtreeviz(…). However, this method is deprecated. Instead, you can use dtreeviz.model(…) method.
Resources for this method:Visualizing TensorFlow Decision Forest Trees with dtreeviz  |  TensorFlow Decision Forests

Hello Julian, many thanks for share your code