[MUSIC PLAYING] [音乐]
Last episode, we used a 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, versicolor, 各种百合 - 细胞，浮雕，
and virginica. Verginica。
Scrolling down, you can see we're 向下滚动，你可以看到
given 50 examples of each type, so 150 examples total. 示例50显示了每种类型的总权限150示例。
Notice there are four features that are 他指出有四个特点
used to describe each example. 用于描述每个示例。
These are the length and width of the sepal and petal. 有cypal和卵石的长度和宽度。
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 现有的数据集，让我们来郁金香scikit学习
and see what it looks like in code. 看看你的代码中是什么样的。
Conveniently, the friendly folks at scikit 适当，友好的人
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, 指的是cypal的长度，其次是宽度，
and so on. 等等。
The target variable contains the labels. 目标变量包含标签。
Likewise, these index to the target names. 另外，这些指的是目标的名称。
Let's print out the first one. 让我们打印第一个。
A label of 0 means it's a setosa. 零称为Setosa。
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 150 entries. 现在数据变量和目标都有150个条目。
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. 我们将调用您留出测试数据的示例
We'll 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 setosa is at index 0, 因此该数据集是零指数下的第一个细胞质，
and the first versicolor is at 50, and so on. 第一次清漆在50，依此类推。
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. 现在让我们看看树的期望。
We'll 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 scikit's 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. 创建一个易于阅读的PDF版本。
I can run our script and open up the PDF, 我可以运行文档并打开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 if the pedal width 例如，询问这个框是否
is less than 0,8 centimeters. 花瓣宽度小于0,8厘米。
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 a setosa, so let's see 我们知道这朵花是Setosa，所以让我们来看看
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 0,8 centimeters. 这是关于花瓣宽度是否小于0,8厘米。
That's the fourth feature. 这是第四个属性。
The answer is true, so we proceed 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, setosa, 所以树给我们的期望，细胞，
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 versicolor. 这朵花是曲张。
Let's see what the tree predicts. 让我们看看树的期望。
Again we read from the top, and this time the pedal width 我们再次从顶部读取，这次是显示花瓣
is greater than 0,8 centimeters. 大于0,8厘米。
The answer to the tree's question is false, 回答错误的树问题，
so we go right. 所以我们会右转
The next question the tree asks is whether the pedal width 树问的第二个问题是它是否是
is less than 1,75. 花瓣宽度小于1,75。
It's trying to narrow it down. 试图缩小它。
That's true, so we go left. 这是真的，所以我们走了。
Now it asks if the pedal length is less than 4,95. 现在询问花瓣长度是否小于4,95。
That's true, so we go left again. 这是真的，所以我们再次左转。
And finally, the tree asks if the pedal width 最后树问是否是
is less than 1,65. 花瓣宽度小于1,65。
That's true, so left it is. 没错，如果我们左转。
And now we have our prediction-- it's a versicolor, 现在我们有期待 - 这是变体，
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 will start looking 下一集将开始看
at what makes a good feature. 什么是一个很好的功能。
Thanks very much for watching, and I'll see you next time. 非常感谢您的收看，下次再见。
[MUSIC PLAYING] [音乐]