字幕表 動画を再生する
[MUSIC PLAYING]
{ 機械学習 }
Welcome back.
{ 機械学習 } レシピ
We've covered a lot of ground already,
ようこそ
so today I want to review and reinforce concepts.
もう多くの領域を実施しました
To do that, we'll explore two things.
今日は復習して 概念を強化していきます
First, we'll code up a basic pipeline
その為に2つのことを追求します
for supervised learning.
第1に教師付き学習の為の 基本的なパイプラインをコード化します
I'll show you how multiple classifiers
複数の分類器が同じ問題を 解ける様子を見せましょう
can solve the same problem.
次にアルゴリズムがデータから ものを学ぶとはどういうことか
Next, we'll build up a little more intuition
もう少し直感を開発します
for what it means for an algorithm to learn something
というのはそれは魔法みたいですが 実は違いますから
from data, because that sounds kind of magical, but it's not.
始めに 皆さんがやりたいような 一般的な実験を見てみましょう
To kick things off, let's look at a common experiment
スパム分類器を作るとします
you might want to do.
それは単に受信メールに スパムかスパムでないかと
Imagine you're building a spam classifier.
ラベル付けする関数です
That's just a function that labels an incoming email
では もうデータセットを集めて
as spam or not spam.
モデルを学習させる準備ができたとします
Now, say you've already collected a data set
しかし実際に運用を始める前に
and you're ready to train a model.
まず答える必要がある質問があります
But before you put it into production,
学習データに無い E メールを 分類するのにそれを使った時
there's a question you need to answer first--
精度はどの位になるか?
how accurate will it be when you use it to classify emails that
私達はモデルを導入する前に うまく機能するか
weren't in your training data?
できるだけ検証したいです
As best we can, we want to verify our models work well
それを把握できる実験を することができます
before we deploy them.
1つの方法はデータセットを2つに 分割することです
And we can do an experiment to help us figure that out.
これらを「学習」と「テスト」と 呼びます
One approach is to partition our data set into two parts.
「学習」はモデルを学習させる為に使い
We'll call these Train and Test.
「テスト」は新データに対しての 精度を見る為に使います
We'll use Train to train our model
これは一般的なパターンです ではコードではどうか見てみましょう
and Test to see how accurate it is on new data.
まず始めに Scikit に データセットをインポートしましょう
That's a common pattern, so let's see how it looks in code.
またアイリスを使います 便利に含まれていますからね
To kick things off, let's import a data set into [? SyKit. ?]
さて アイリスは第2回で もう見ましたが
We'll use Iris again, because it's handily included.
前に見ていないものは
Now, we already saw Iris in episode two.
特徴量 x とラベル y を 呼び出すことです
But what we haven't seen before is
なぜでしょう
that I'm calling the features x and the labels y.
それは分類器を関数と 考える1つの方法だからです
Why is that?
高レベルでは x を入力と
Well, that's because one way to think of a classifier
y を出力と考えられます
is as a function.
それに関してこの回の後半で もっとお話しします
At a high level, you can think of x as the input
データセットをインストールした後 最初にやることは
and y as the output.
それを学習とテストに 分割することです
I'll talk more about that in the second half of this episode.
その為に便利なユーティリティを インポートできて
After we import the data set, the first thing we want to do
それで構文を明確にできます
is partition it into Train and Test.
x と y をとります
And to do that, we can import a handy utility,
特徴量とラベルです そしてそれらを分割して
and it makes the syntax clear.
2つのセットにします
We're taking our x's and our y's,
X_train と y_train は 学習セットの特徴量とラベルです
or our features and labels, and partitioning them
X_test と y_test はテストセットの 特徴量とラベルです
into two sets.
ここで言ってるのはただ データの半分はテストに使いたい
X_train and y_train are the features and labels
ということです
for the training set.
アイリスに 150 例があるので 75 が学習に
And X_test and y_test are the features and labels
75 がテストに入ります
for the testing set.
では分類器を作ります
Here, I'm just saying that I want half the data to be
ここで2つの異なる種類を使い
used for testing.
それらがどう同じ作業をするか 見せましょう
So if we have 150 examples in Iris, 75 will be in Train
もう見たことのある決定木から 始めましょう
and 75 will be in Test.
分類器特定の コードは2行しかありませんね
Now we'll create our classifier.
では学習データを使って 分類器を学習させましょう
I'll use two different types here
この時点でデータ分類にもう使えます
to show you how they accomplish the same task.
次に 予測メソッドを呼び出し
Let's start with the decision tree we've already seen.
テストデータを分類する為に 使います
Note there's only two lines of code
予測を打ち出せば
that are classifier-specific.
数字のリストが見えます
Now let's train the classifier using our training data.
これらは分類器がテストデータの 各行に対して予測する
At this point, it's ready to be used to classify data.
アイリスの種類に相当します
And next, we'll call the predict method
ではテストセットに対する 分類器の精度を見てみましょう
and use it to classify our testing data.
上に テストデータに対する 本当のラベルがありましたよね
If you print out the predictions,
精度を計算する為に
you'll see there are a list of numbers.
予測ラベルと本当のラベルを 比較して
These correspond to the type of Iris
スコアを計算できます
the classifier predicts for each row in the testing data.
Scikit に便利なメソッドがあり
Now let's see how accurate our classifier
それをする為にインポートできます
was on the testing set.
ここに注目 精度は 90% 以上です
Recall that up top, we have the true labels for the testing
皆さんが自分でこれを試すと 少し違うかもしれません
data.
「学習 / テスト」データの分割方法で ランダム性があるからです
To calculate our accuracy, we can
さて ここに面白いものがあります
compare the predicted labels to the true labels,
これらの2行を置き換えて
and tally up the score.
同じ作業をする別の分類器を作れます
There's a convenience method in [? Sykit ?]
決定木を使う代わりに
we can import to do that.
K 近傍 という分類器を使います
Notice here, our accuracy was over 90%.
実験を実行するとコードは 全く同じに動作すると分かります
If you try this on your own, it might be a little bit different
実行したら精度は違うかもしれません
because of some randomness in how the Train/Test
この分類器は少し異なって 動作するからで
data is partitioned.
また「学習 / テスト」分割に ランダム性があるからです
Now, here's something interesting.
同様に もっと高度な 分類器を使いたい場合
By replacing these two lines, we can use a different classifier
それをインポートして この2行を変えればいいだけです
to accomplish the same task.
あとはコードは同じです
Instead of using a decision tree,
ここでのポイントは 多種の分類器がある一方で
we'll use one called [? KNearestNeighbors. ?]
高レベルでは それらの インターフェースは似ていることです
If we run our experiment, we'll see that the code
では データから学ぶとは何かについて もう少し話しましょう
works in exactly the same way.
前に 特徴量 x とラベル y を 呼び出すと言いました
The accuracy may be different when you run it,
それらは関数の入力と出力だからと
because this classifier works a little bit differently
無論 関数はプログラミングから もう知っていますね
and because of the randomness in the Train/Test split.
def classify と関数があります
Likewise, if we wanted to use a more sophisticated classifier,
教師付き学習で既に知ってるように
we could just import it and change these two lines.
これを自分では書きません
Otherwise, our code is the same.
私達は学習データからそれを学ぶ アルゴリズムが欲しいのです
The takeaway here is that while there are many different types
では関数を学ぶとは どういう意味でしょう
of classifiers, at a high level, they have a similar interface.
関数は入力値から出力値への 対応付けにすぎません
Now let's talk a little bit more about what
ここに前に見たような関数があります
it means to learn from data.
y=mx+b
Earlier, I said we called the features x and the labels y,
これは直線の方程式で パラメーターは2つで
because they were the input and output of a function.
傾きを表す m と
Now, of course, a function is something we already
y 切片を表す b です
know from programming.
勿論これらのパラメーターがあれば
def classify-- there's our function.
x の異なる値に対する 関数を図示できます
As we already know in supervised learning,
教師付き学習で classfy 関数にも
we don't want to write this ourselves.
幾つかパラメーターがあるでしょう
We want an algorithm to learn it from training data.
でも 入力 x は分類したい 例に対して特徴量で
So what does it mean to learn a function?
出力 y は ラベルです
Well, a function is just a mapping from input
スパムかスパムでないか 花の種類のようなものです
to output values.
では関数の本体は 一体どう見えるのでしょう
Here's a function you might have seen before-- y
それがアルゴリズム的に 書きたい
equals mx plus b.
または学びたい部分です
That's the equation for a line, and there
ここで理解する大事なことは
are two parameters-- m, which gives the slope;
私達は一から始めているのではなく
and b, which gives the y-intercept.
どこからともなく関数の本体を 引っ張ってきてるのです
Given these parameters, of course,
私達はモデルから始めているのです
we can plot the function for different values of x.
モデルを試作品とか 関数の本体を定義する規則と
Now, in supervised learning, our classified function
考えて結構です
might have some parameters as well,
通常 モデルには
but the input x are the features for an example we
学習データと合わせられる パラメーターがあります
want to classify, and the output y
ここにこのプロセスの働き方の 高度な例があります
is a label, like Spam or Not Spam, or a type of flower.
トイデータセットを見て どの種のモデルを
So what could the body of the function look like?
分類器として使えるか 考えてみましょう
Well, that's the part we want to write algorithmically
赤い点と緑の点を区分けしたいとします
or in other words, learn.
その幾つかをここのグラフに 描いてあります
The important thing to understand here
その為に使う特徴量は2つだけで
is we're not starting from scratch
点の X と Y 座標です
and pulling the body of the function out of thin air.
ではこのデータをどう分類するか 考えてみましょう
Instead, we start with a model.
前に見たことのない 新しい点を考えて
And you can think of a model as the prototype for
その点を赤か緑に分類する 関数が要ります
or the rules that define the body of our function.
実は分類したいデータが 沢山あるかもしれません
Typically, a model has parameters
ここに薄緑と薄赤で テスト例を描きました
that we can adjust with our training data.
これらは学習データに入って いなかった点です
And here's a high-level example of how this process works.
分類器はそれらを前に 見たことがありません
Let's look at a toy data set and think about what kind of model
ではどうやって正しいラベルを 予測できるのでしょう
we could use as a classifier.
例えばどうにかしてデータに こんな風に線を引けるとして
Pretend we're interested in distinguishing
線の左側の点は緑で
between red dots and green dots, some of which
線の右側の点は赤だと言えるでしょう
I've drawn here on a graph.
この線は分類器として 役立てられます
To do that, we'll use just two features--
ではこの線をどう学べるのでしょう
the x- and y-coordinates of a dot.
1つの方法は学習データを使って
Now let's think about how we could classify this data.
モデルのパラメーターを調整します
We want a function that considers
使うモデルが前に見たような 単純な直線だとしましょう
a new dot it's never seen before,
すると調整するパラメーターは 2つで m と b です
and classifies it as red or green.
それらを変えることで 線の出る場所を変えられます
In fact, there might be a lot of data we want to classify.
ではどうやって正しいパラメーターを 覚えられるでしょう
Here, I've drawn our testing examples
1つの考え方は 学習データを使って
in light green and light red.
繰り返し調整できるということです
These are dots that weren't in our training data.
例えば 任意の線から始めて
The classifier has never seen them before, so how can
最初の学習データを 分類するのに使います
it predict the right label?
正しくできれば線を変える 必要はないので
Well, imagine if we could somehow draw a line
次に移ります
across the data like this.
しかし他方答えが間違っていたら
Then we could say the dots to the left
モデルのパラメーターを調整して
of the line are green and dots to the right of the line are
もっと正確になるようにします
red.
ここでのポイントはこうです
And this line can serve as our classifier.
学習のことを考える1つの方法は 学習データを使って
So how can we learn this line?
モデルのパラメーターを 調整することです
Well, one way is to use the training data to adjust
ここに実に特別なものがあります
the parameters of a model.
tensorflow/playground と 呼ばれているものです
And let's say the model we use is a simple straight line
これはニューラルネットワークの 素晴らしい例です
like we saw before.
じかにブラウザーで 実行し実験できます
That means we have two parameters to adjust-- m and b.
まさにこれ自体に関する回が あっていいと思いますが
And by changing them, we can change where the line appears.
今は早速いじってみます
So how could we learn the right parameters?
すごいですよ
Well, one idea is that we can iteratively adjust
playground には試せる 異なるデータセットが備わっています
them using our training data.
とてもシンプルなのもあります
For example, we might start with a random line
例えば これを分類する為に 私達の線を使えます
and use it to classify the first training example.
もっとずっと複雑な データセットもあります
If it gets it right, we don't need to change our line,
このデータセットは特に難しいです
so we move on to the next one.
それを分類する為に ネットワークを作れるか見てください
But on the other hand, if it gets it wrong,
ニューラルネットワークを もっと高度な種類の分類器と
we could slightly adjust the parameters of our model
考えていいでしょう
to make it more accurate.
決定木やシンプルな線のようにです
The takeaway here is this.
しかし 原則として 考え方は似ています
One way to think of learning is using training data
役に立てたでしょうか
to adjust the parameters of a model.
Twitter を作ったので
Now, here's something really special.
新しい回の通知がご覧になれます
It's called tensorflow/playground.
次回は Google I/O の仕事の都合上
This is a beautiful example of a neural network
数週間後になる予定です
you can run and experiment with right in your browser.
ご視聴ありがとう では次回にお会いしましょう
Now, this deserves its own episode for sure,
but for now, go ahead and play with it.
It's awesome.
The playground comes with different data
sets you can try out.
Some are very simple.
For example, we could use our line to classify this one.
Some data sets are much more complex.
This data set is especially hard.
And see if you can build a network to classify it.
Now, you can think of a neural network
as a more sophisticated type of classifier,
like a decision tree or a simple line.
But in principle, the idea is similar.
OK.
Hope that was helpful.
I just created a Twitter that you can follow
to be notified of new episodes.
And the next one should be out in a couple of weeks,
depending on how much work I'm doing for Google I/O. Thanks,
as always, for watching, and I'll see you next time.