Ivan Anderies
Ivan Anderies

Reputation: 41

ImportError: cannot import name 'dtreeviz' from 'dtreeviz.trees' (/usr/local/lib/python3.8/dist-packages/dtreeviz/trees.py)

When I try to run this vizulization on google colab I am getting this error,

ImportError: cannot import name 'dtreeviz' from 'dtreeviz.trees' (/usr/local/lib/python3.8/dist-packages/dtreeviz/trees.py)

from sklearn.datasets import load_wine
from sklearn.ensemble import RandomForestClassifier
from dtreeviz.trees import dtreeviz


rf = RandomForestClassifier(n_estimators=100,
                            max_depth=3,
                            max_features='auto',
                            min_samples_leaf=4,
                            bootstrap=True,
                            n_jobs=-1,
                            random_state=0)
rf.fit(X, y)

viz = dtreeviz(rf.estimators_[99], X, y,
               target_name="SizeClass",
               feature_names=X_train.columns,
               class_names=list(y_train.feature_names),
               title="100th decision tree")

viz.save("decision_tree.svg")

from google.colab import files
files.download("decision_treef.svg")

I tried pip installing but it says that the requirments are already met

Upvotes: 2

Views: 5331

Answers (5)

mastervit
mastervit

Reputation: 11

Apparently the dtreeviz library API has changed since the version you are using. So you need either to roll back the dtreeviz version, as suggested by Edgar Rios Linares or update you code to follow the new API. As an example how to do that I will be using the code from the book "Machine Learning For Dummies" which probably has wider audience (your code can be fixed in a similar manner). For generality I will not use Colab or Anaconda platform - just plain Python and its libraries.

The ML4Dummies book published the following code that uses the older API:

from dtreeviz.trees import dtreeviz
viz = dtreeviz(dt, X, y,
      target_name='play_tennis',
      feature_names=X.columns,
      class_names=["No", "Yes"],
      scale=2.0)
viz

If you install the latest version of the library (as of April 2023) with

pip install dtreeviz 

then your version will be dtreeviz-2.2.1 and the code above does not work and running it results in the error described in the Stack Overflow question:

ImportError: cannot import name 'dtreeviz' from 'dtreeviz.trees'

Fixing that error results in other errors since that code is not compatible with the new API.

You need to follow the new API which is described in README.MD of the dtreeviz github repo (See installation instructions). Adopting the new API for the book example, you get:

import dtreeviz
viz = dtreeviz.model(dt, X, y,
           target_name='play_tennis',
           feature_names=X.columns, 
           class_names=["No", "Yes"])  

# In a notebook, you can render inline without calling show()
viz.view(scale=1.4) 
# Uncomment to save with scale 1
#vizRender = viz.view(scale=1)
#vizRender.save("play_tennis_decision_tree.svg")

play tennis decision tree

Upvotes: 1

ShannonatODU
ShannonatODU

Reputation: 1

I had the same problem. After trying both solutions, I can confirm that installing the older version dtreeviz==1.4.0 fixed the problem.

Upvotes: 0

Edgar Rios Linares
Edgar Rios Linares

Reputation: 1

Try installing and old version

pip install dtreeviz==1.4.0

Upvotes: 0

WhiteLedAnn
WhiteLedAnn

Reputation: 41

If you use import dtreeviz

you should use also

vis = dtreeviz.model

Here is documentation

Upvotes: 0

Jordy
Jordy

Reputation: 1960

According to Decision Tree Visualization's Github. When using google colab, there is no need to import dtreeviz from the directory dtreeviz.trees. At the Colab Example, it is seen that enough just like this :

import dtreeviz

instead:

from dtreeviz.trees import dtreeviz

Upvotes: 0

Related Questions