dzy82f
(Julian Macnamara)
February 24, 2023, 1:45pm
1
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
twhy55
(Tobias)
February 24, 2023, 2:39pm
2
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
dzy82f
(Julian Macnamara)
February 24, 2023, 3:57pm
3
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.
twhy55
(Tobias)
February 24, 2023, 5:02pm
5
@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
dzy82f
(Julian Macnamara)
February 25, 2023, 12:45pm
6
Hello Tobias
Sadly, it works in Colab but not in kaggle
Many thanks for your help
All the best
Julian
dzy82f
(Julian Macnamara)
February 26, 2023, 8:55am
7
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);
3 Likes
pawan18
(Pawan Kumar)
May 6, 2023, 4:19am
8
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’)
1 Like
dzy82f
(Julian Macnamara)
May 14, 2023, 8:51am
9
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