字幕表 動画を再生する
[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
実は解釈しやすい 少数モデルの1つで
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.
データセットには3種の異なる 花が含まれています
The data set includes three different types of flowers.
それらはアイリスの全種で setona と versicolor と virginica です
They're all species of iris-- setosa, versicolor,
下にスクロールすると
and virginica.
各種50例 つまり全部で 150 例が示されています
Scrolling down, you can see we're
各例を表すのに 4つの特徴量が使われています
given 50 examples of each type, so 150 examples total.
これらは萼と花弁の長さと幅です
Notice there are four features that are
りんごとオレンジの問題と同じに
used to describe each example.
最初の4列は特徴量で
These are the length and width of the sepal and 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
ではアイリスを scikit-learn に インポートして
of flower we have if we're given a new flower that we've never
コードでどう見えるか見てみましょう
seen before.
タイミングよく scikit の 好意的な人々が
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 scikit
アイリスをこのようにコードに インポートできます
provided a bunch of sample data sets,
データセットは Wiki からの表と
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.
ですから 最初の値は萼の長さ 2番目は萼の幅を表す等です
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,
0 のラベルは setona を意味します
and so on.
Wiki の表を見れば
The target variable contains the labels.
今第1行を印刷したと気づきます
Likewise, these index to the target names.
データとターゲット変数両方に 150 のエントリーがあります
Let's print out the first one.
お望みならそれらに繰り返して
A label of 0 means it's a 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.
しかし その前にまず データを分ける必要があります
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
ただ今回の試みでは 各種の花の1つの例を除外します
on data it's never seen before.
たまたま データセットは 順序がついているので
Testing is actually a really important part
最初の setosa はインデックス 0 に
of doing machine learning well in practice,
最初の versicolor は 50 に等々
and we'll cover it in more detail in a future episode.
構文は少し複雑そうですが やっているのはただ
Just for this exercise, I'll remove one example
データとターゲット変数から 3つのエントリーを除外してるだけです
of each type of flower.
次に 新しい変数セットを2つ作ります
And as it happens, the data set is
1つは学習用 1つはテスト用です
ordered so the first setosa is at index 0,
学習にデータの大部分があり
and the first versicolor 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,
私達は各種の花が1つあると知っていて
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.
その為に scikit の チュートリアルの中から
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.
これら2例からのコードを結合させて
To do that, I'm going to copy-paste
読みやすい PDF を作ったら
some code in from scikit's tutorials,
スクリプトを実行して その PDF を開けます
and because this code is for visualization
そして木が見られます
and not machine-learning concepts,
データ分類にそれを使う為に 上から読んで始めます
I won't cover the details here.
各ノードは特徴量の1つについて 「はい」か「いいえ」を尋ねます
Note that I'm combining the code from these two examples
例えば このノードは花弁の幅が
to create an easy-to-read PDF.
0.8 cm 以下か尋ねます
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 if the pedal width
メタデータを使うことで 特徴量の名前を見つけられますね
is less than 0.8 centimeters.
私達はこの花が setosa だと知っています
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.
花弁の幅が 0.8 cm 以下かです
Here are the features and label for our first testing flower.
それは4番目の特徴量です
Remember, you can find the feature names
答えは真なので 左へ進みます
by looking at the metadata.
この時点でもう葉ノードにいます
We know this flower is a setosa, so let's see
他に尋ねる質問はありません
what the tree predicts.
そこで木は setosa という 予測を出します
I'll resize the windows to make this easier to see.
それは正しいです
And the first question the tree asks
ラベルは 0 ですね これは その種類の花のインデックスです
is whether the petal width is less than 0.8 centimeters.
では2つ目のテスト例をやってみましょう
That's the fourth feature.
これは versicolor なんですが
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,
0.8 cm より大きいです
so the tree gives us a prediction, setosa,
木の質問に対する答えは偽です
and it's right.
そこで右へ行きます
Notice the label is 0, which indexes to that type of flower.
木の次の質問は 花弁の幅が 1.75 以下かです
Now let's try our second testing example.
それを絞り込もうとしています
This one is a versicolor.
それは真なので 左へ行きます
Let's see what the tree predicts.
今度は花弁の長さが 4.95 以下かと 訊いています
Again we read from the top, and this time the pedal width
それは真なので また左へ行きます
is greater than 0.8 centimeters.
最後に 木は花弁の幅が 1.65 以下かと訊きます
The answer to the tree's question is false,
それは真なので 左です
so we go right.
こうして予測が出て versicolor です
The next question the tree asks is whether the pedal 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 pedal length is less than 4.95.
決定木を手早く可視化して 読む方法を説明しました
That's true, so we go left again.
ここで学ぶことはもっと沢山あります
And finally, the tree asks if the pedal width
特に 例からどのようにして それらが自動で作られるのかですね
is less than 1.65.
以降の回でそれを 説明していきますが
That's true, so left it is.
今は不可欠な点で 終わりにしましょう
And now we have our prediction-- it's a versicolor,
木が尋ねるどの質問も
and that's right again.
皆さんの特徴量の1つについてで なければいけません
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]