last episode we use the decision tree as
our classifier today we'll add code to
visualize it so we can see how it works
under the hood there are many types of
classifiers you may have heard of before
things like neural nets or support
vector machines so why did we use a
decision tree to start well they have a
very unique property they're easy to
read and understand in fact they're one
of the few models that are interpretable
where you can understand exactly why the
classifier makes a decision that's
amazingly useful in practice to get
started I'll introduce you to a real
data set we'll work with today
it's called iris iris is a classic
machine learning problem in it you want
to identify what type of flower you have
based on different measurements like the
length and width of the petal the data
set includes three different types of
flowers they're all species of iris
setosa versi color and virginica
scrolling down you can see we're given
50 examples of each type so 150 examples
total notice there are four features
that are used to describe each example
these are the length and width of the
sepal in petal and just like in our
apples and oranges problem the first
four columns give the features and the
last column gives the labels which is
the type of flower in each row our goal
is to use this data set to train a
classifier then we can use that
classifier to predict what species of
flower we have if we're given a new
flower that we've never seen before
knowing how to work with an existing
data set is a good skill so let's import
iris into scikit-learn and see what it
looks like in code conveniently the
friendly folks at psych had provided a
bunch of sample data sets including iris
as well as utilities to make them easy
to import we can import iris into our
code like this the data set includes
both the table from Wikipedia as well as
some metadata the metadata tells you the
names of the features and the names of
different types of flowers the features
and examples themselves are contained in
the data variable for example if I print
out the first entry you can see the
measurements for this flower these index
to the feature names so the first value
refers to the sepal length and the
second to sepal width and so on the
target variable contains the labels
likewise these index to the target
let's print out the first one a label of
zero means it's atossa
if you look at the table from Wikipedia
you'll notice that we just printed out
the first row now both the data and
target variables have a hundred 50
entries if you want you can iterate over
them to print out the entire data set
like this now that we know how to work
with the data set we're ready to train a
classifier but before we do that first
we need to split up the data I'm going
to remove several of the examples and
put them aside for later
we'll call the examples I'm putting
aside our testing data will keep these
separate from our training data and
later on we'll use our testing examples
to test how accurate the classifier is
on data it's never seen before testing
is actually a really important part of
doing machine learning well in practice
and we'll cover it in more detail in a
future episode just for this exercise
I'll remove one example of each type of
flower and as it happens the data set is
ordered so the first Atossa is that
index zero and the first versi color is
at 50 and so on the syntax looks a
little bit complicated but all I'm doing
is removing three entries from the data
and target variables then I'll create
two new sets of variables one for
training and one for testing training
will have the majority of our data and
testing will have just the examples I
removed now just as before we can create
a decision tree classifier and train it
on our training data before we visualize
it let's use the tree to classify our
testing data we know we have one flower
of each type and we can print out the
labels we expect now let's see what the
tree predicts will give it the features
for our testing data and we'll get back
labels you can see the predicted labels
match our testing data that means it got
them all right now keep in mind this was
a very simple test and we'll go into
more detail down the road now let's
visualize the tree so we can see how the
classifier works to do that I'm going to
copy/paste some code in from side kits
tutorials and because this code is for
visualization and not machine learning
concepts I won't cover the details here
note that I'm combining the code from
these two examples to create an
easy-to-read PDF
I can run our script and open up the PDF
and we can see the tree to use it to
classify data you start by reading from
the top
each node asks a yes-or-no question
about one of the features for example
this node asks the pedal width is less
than point 8 centimeters if it's true
for the example you're classifying go
left otherwise go right now let's use
this tree to classify an example from
our testing data here are the features
and label for our first testing flower
remember you can find the feature names
by looking at the metadata we know this
flower is as Tosa
so let's see what the tree predicts I'll
resize the windows to make this easier
to see and the first question the tree
asks is whether the petal width is less
than point 8 centimeters that's the
fourth feature the answer is true so we
perceive left at this point we're
already at a leaf node there are no
other questions to ask so the tree gives
us a prediction so Tosa and it's right
notice the label is 0 which indexes to
that type of flower now let's try our
second testing example this one is a
versi color let's see what the tree
predicts again we read from the top and
this time the petal width is greater
than point 8 centimeters the answer to
the trees question is false so we go
right the next question the tree asks is
whether the petal width is less than
1.75 it's trying to narrow it down
that's true so we go left now it asks if
the petal length is less than 4 point 9
5 that's true so we go left again and
finally the tree asks if the petal width
is less than 1 point 6 5 that's true so
left it is and now we have our
prediction it's a versi color and that's
right again you can try the last one on
your own as an exercise and remember the
way we're using the tree is the same way
it works in code so that's how you
quickly visualize and read a decision
tree there's a lot more to learn here
especially how they're built
automatically from examples we'll get to
that in a future episode but for now
let's close with an essential point
every question the tree asks must be
about one of your features that means
the better your features are the better
a tree you can build and the next
episode we'll start looking at what
makes a good feature thanks very much
for watching and I'll see you next time