## Visualizing a Decision Tree – Machine Learning Recipes #2

Thanks! Share it with your friends!

Last episode, we treated our Decision Tree as a blackbox. In this episode, we’ll build one on a real dataset, add code to visualize it, and practice reading it – so you can see how it works under the hood. And hey — I may have gone a little fast through some parts. Just let me know, I’ll slow down. Also: we’ll do a Q&A episode down the road, so if anything is unclear, just ask!

Subscribe to the Google Developers: http://goo.gl/mQyv5L –
Subscribe to the brand new Firebase Channel: https://goo.gl/9giPHG
And here’s our playlist: https://goo.gl/KewA03

Shivji Tiwari says:

On which screen are you working?

A R says:

Wonderful Series of ML! Someone recommends me another one? please

Harel Brandman says:

I keep getting errors when trying the pdf thing

Matt 92 says:

If there is something wrong about the decision tree classifier. I already made decision tree classifier with iris datasets.
I used python 3.7, I think someone was struggling about this and they can't open the decision tree.

Remember to download the packages and modules first
# Import packagaes and modules that required
import pandas as pd

import numpy as np

from sklearn import tree

from sklearn import datasets

# Importing datasets

# Buildng the deciosn tree classifier and Train data

iris_classifier = tree.DecisionTreeClassifier()

iris_classifier = iris_classifier.fit(iris.data, iris.target)

print ("Trained iris_classifier")

print(iris_classifier)

# test data

print (iris.data[1],iris.target[1])

# Data = [sepal length, sepal width, petal lenght, petal width]

# Target = ['setosa' 'versicolor' 'virginica']

print (iris.feature_names,iris.target_names)

# 0 = setosa, 1 = versicolor, 2 = virginica

#Visualizing the decision tree

from sklearn.tree import export_graphviz

export_graphviz(iris_classifier, out_file='Decision tree of iris datasets classifier.dot',

feature_names = iris.feature_names,

class_names = iris.target_names,

rounded = True,filled = True, impurity=False)

# You can add more the classes

# Sorry if I got something wrong

The decision tree will be looks a likely same in the video, but I think there will something different.
You can use other classifiers like randomforestclassifier, etc for visualizing the decision tree.

Igor Carvalho de Paula says:

the same cod: "'list' object has no attribute 'write'"

Kryptöñ says:

Unable to create the pdf, despite of all dependencies installed
Traceback (most recent call last):
File "/home/tbd/PycharmProjects/tensorEnv/MLiris2.py", line 42, in <module>
graph.write_pdf("iris.pdf")
AttributeError: 'list' object has no attribute 'write_pdf'

Jay Jhaveri says:

Working code for pdf on python 3, jupyter/anaconda

# Exporting the decision tree

from sklearn.externals.six import StringIO

import pydot

from sklearn import tree

import graphviz as gp

dot_data = StringIO()

tree.export_graphviz(clf,

out_file=dot_data,

feature_names=iris.feature_names,

class_names=iris.target_names,

filled=True, rounded=True,

impurity=False)

# I used this module (graphviz) to generate the graph

graph = gp.Source(dot_data.getvalue())

graph.render("iris", view = True)

/*
Note :- use
pip uninstall graphviz / conda remove graphviz
conda install python-graphviz

also make sure you have
pydot, sklearn already installed.
*/

paritosh batish says:

so decision tree is like nested if else

Jeran rai says:

my brain hurts, where did this feature_name and target_name come from, and if this came from the sklearn.dataset than how can a rookie like me know if you just keep teaching like for a experienced viewers.

Bingchao Wang says:

for python3 user
tree.plot_tree(clf, filled=True)

plt.show()

raw n says:

python 2????? seriously?

Quinton Carroll says:

How is he getting the blue screen?

TAMMA Aziz says:

Thanks for this amazing content and clear explanation

The Horror says:

Doing this tutorial properly took about a hour on me side, me dum self was struggling to just read and paste properly

Karthik Logan says:

sir what is mean by axis=0

7620313 says:

Use the following code if you are using Python 3

# viz
import graphviz as gp

graph = gp.Source(dot_data.getvalue())
graph.render("iris", view = True)

If you have problem with PATH, add this line after "import graphviz as gp"
import os

os.environ["PATH"] += os.pathsep + 'paste the bin folder path of Graphviz package here (usually found in anaconda folder under pkgs)'

Vidit Khanna says:

So ML can be described as an auto-generated nested if else.