Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+9 votes

Is anyone having trouble using the "plot_tree" function with scikit learn?

Here is the line of code that is producing the error below.

tree.plot_tree(clf,feature_names=X_train.columns,class_names=['Deceased','Not deceased'],label='root')

error:

module 'sklearn.tree' has no attribute 'plot_tree'

I looked at Dr. Stonedahl's example code and he uses the same code so I am wondering if anyone else is getting this error?

asked in CSC320 by (1 point)

2 Answers

+3 votes
 
Best answer

It turns out the plot_tree function was added in scikit_learn version 0.21. The current version up on Colab is the latest (0.22), but if you're getting that error on your local computer, you probably an older version.

I'm assuming you have the Anaconda python distribution.

Options include:
a) trying to convince Anaconda to update the scikit learn package.
b) throwing up your hands, and reinstalling the whole Anaconda (which I'm pretty sure will get you a new enough version of scikit-learn... unless you have to add the conda-forge channel... not sure.)

answered by (5.7k points)
selected by
0

P.S. If you (or someone else) fixes your local version, post to let others know how you did it!

+1

I've decided to move my code to Colab, don't really want to deal with installing new updates. Best of luck to whoever tries!

0

Ehren apparently updated scikit-learn pretty easily using the Anaconda package management tool -- I encouraged him to post his info to this question, if he has time.

+2 votes

I am currently using my Anaconda Python and I was able to fix that issue. I had gone into the Anaconda navigator and went under the environments tab where you can search for packages and you want to search for scikit-learn (this is where sklearn is) and update it (this should result in version 0.22.1 for scikit-learn which allows the plot_tree to work).

I had tried running the plot_tree in VS Code and it does not throw the error anymore, but it does not display the decision tree still. This is probably due to VS Code not being able to plot math related things. I do not know if it is just VS code, but when I tried running it in spyder it worked.

answered by (1 point)
...