More Related Content
Similar to Rによる分類木 入門 (20)
Rによる分類木 入門
- 6. Example…
商品を サイズ 色 見た回数
買った M 普通 10
買った L 好き 5
買わない M 嫌い 3
買った M 好き 6
買わない L 好き 3
買わない M 普通 10
買わない M 嫌い 4
買った L 好き 6
目的変数がカテゴリカル(質的)な場合
6
- 10. Example…
商品を サイズ 色 見た回数
買った M 普通 10
買った L 好き 5
買わない M 嫌い 3
買った M 好き 6
買わない L 好き 3
買わない M 普通 10
買わない M 嫌い 4
買った L 好き 6
目的変数がカテゴリカル(質的)な場合
10
- 11. ex.)
M L 好き 普通・嫌い
色
4.5 <4.5
見た回数
ジニ係数(不純度)の一番低い分岐点を採用
データ
買った
買わない
説明変数
サイズ
ジニ係数
0.48 0.375 0.375 0.32 0.00.44
0.465 0.375 0.2
加重平均 加重平均 加重平均
11
- 15. 交差検証法(cross validation)
How to prune?
⑴データをテストデータと
トレーニングデータに分ける
⑵トレーニングデータだけで
モデルをつくる
⑶できたモデルとテストデータを比較し、
答え合わせをして、精度を求める
⑷⑴〜⑶をn回繰り返し、平均する
これを各ノードで行い、
制度の低いところを剪定する
15
- 17. ①
③②
⑥ ⑦
⑫ ⑬
>
library(mvpart)
>
#
決定木のplot
>
iris.rp
<-‐
rpart(Species~.,
data=iris)
>
iris.rp
n=
150
node),
split,
n,
loss,
yval,
(yprob)
*
denotes
terminal
node
1)
root
150
100
setosa
(0.33333333
0.33333333
0.33333333)
2)
Petal.Length<
2.45
50
0
setosa
(1.00000000
0.00000000
0.00000000)
*
3)
Petal.Length>=2.45
100
50
versicolor
(0.00000000
0.50000000
0.50000000)
6)
Petal.Width<
1.75
54
5
versicolor
(0.00000000
0.90740741
0.09259259)
12)
Petal.Length<
4.95
48
1
versicolor
(0.00000000
0.97916667
0.02083333)
*
13)
Petal.Length>=4.95
6
2
virginica
(0.00000000
0.33333333
0.66666667)
*
7)
Petal.Width>=1.75
46
1
virginica
(0.00000000
0.02173913
0.97826087)
*
>
plot(iris.rp,uniform=T,branch=0.6,margin=0)
>
text(iris.rp,uniform=T,use.n=T,all=T)
剪定前
17
- 19. 過学習してないか調べる
>
printcp(iris.rp)
Classification
tree:
rpart(formula
=
Species
~
.,
data
=
iris)
Variables
actually
used
in
tree
construction:
[1]
Petal.Length
Petal.Width
Root
node
error:
100/150
=
0.66667
n=
150
CP
nsplit
rel
error
xerror
xstd
1
0.50
0
1.00
1.21
0.048367
2
0.44
1
0.50
0.74
0.061232
3
0.02
2
0.06
0.11
0.031927
4
0.01
3
0.04
0.11
0.031927
>
plotcp(iris.rp)
19
- 20. 過学習してないか調べる
>
printcp(iris.rp)
Classification
tree:
rpart(formula
=
Species
~
.,
data
=
iris)
Variables
actually
used
in
tree
construction:
[1]
Petal.Length
Petal.Width
Root
node
error:
100/150
=
0.66667
n=
150
CP
nsplit
rel
error
xerror
xstd
1
0.50
0
1.00
1.21
0.048367
2
0.44
1
0.50
0.74
0.061232
3
0.02
2
0.06
0.11
0.031927
4
0.01
3
0.04
0.11
0.031927
>
plotcp(iris.rp)
20
- 21. CP
nsplit
rel
error
xerror
xstd
1
0.50
0
1.00
1.21
0.048367
2
0.44
1
0.50
0.74
0.061232
3
0.02
2
0.06
0.11
0.031927
4
0.01
3
0.04
0.11
0.031927
21
- 22. CP
nsplit
rel
error
xerror
xstd
1
0.50
0
1.00
1.21
0.048367
2
0.44
1
0.50
0.74
0.061232
3
0.02
2
0.06
0.11
0.031927
4
0.01
3
0.04
0.11
0.031927
22
- 23. CP
nsplit
rel
error
xerror
xstd
1
0.50
0
1.00
1.21
0.048367
2
0.44
1
0.50
0.74
0.061232
3
0.02
2
0.06
0.11
0.031927
4
0.01
3
0.04
0.11
0.031927
23
- 24. CP
nsplit
rel
error
xerror
xstd
1
0.50
0
1.00
1.21
0.048367
2
0.44
1
0.50
0.74
0.061232
3
0.02
2
0.06
0.11
0.031927
4
0.01
3
0.04
0.11
0.031927
どちらかの
値で剪定
24
- 25. ①
③②
⑥ ⑦>
iris.rp2_1
<-‐
prune(iris.rp,cp=0.02)
>
iris.rp2_1
n=
150
node),
split,
n,
loss,
yval,
(yprob)
*
denotes
terminal
node
1)
root
150
100
setosa
(0.33333333
0.33333333
0.33333333)
2)
Petal.Length<
2.45
50
0
setosa
(1.00000000
0.00000000
0.00000000)
*
3)
Petal.Length>=2.45
100
50
versicolor
(0.00000000
0.50000000
0.50000000)
6)
Petal.Width<
1.75
54
5
versicolor
(0.00000000
0.90740741
0.09259259)
*
7)
Petal.Width>=1.75
46
1
virginica
(0.00000000
0.02173913
0.97826087)
*
>
plot(iris.rp2_1,uniform=T,branch=0.6,margin=0)
>
text(iris.rp2_1,uniform=T,use.n=T,all=T)
剪定後
25
- 26. ①
③②
⑥ ⑦>
iris.rp2_2
<-‐
prune(iris.rp,cp=0.094)
>
iris.rp2_2
n=
150
node),
split,
n,
loss,
yval,
(yprob)
*
denotes
terminal
node
1)
root
150
100
setosa
(0.33333333
0.33333333
0.33333333)
2)
Petal.Length<
2.45
50
0
setosa
(1.00000000
0.00000000
0.00000000)
*
3)
Petal.Length>=2.45
100
50
versicolor
(0.00000000
0.50000000
0.50000000)
6)
Petal.Width<
1.75
54
5
versicolor
(0.00000000
0.90740741
0.09259259)
*
7)
Petal.Width>=1.75
46
1
virginica
(0.00000000
0.02173913
0.97826087)
*
>
plot(iris.rp2_2,uniform=T,branch=0.6,margin=0)
>
text(iris.rp2_2,uniform=T,use.n=T,all=T)
剪定後
26
- 28. 分類木の精度を測りたいなら・・・
28
最後に
>
iris.tre
<-‐
iris[2*(1:75)-‐1,]
#
奇数行
>
iris.test
<-‐
iris[-‐(2*(1:75)-‐1),]
#
偶数行
#
奇数行だけで分類木の生成
>
iris.tre
<-‐
rpart(Species~.,
data=iris.tre,method="class")
>
pred
<-‐
predict(iris.tre,
iris.test,
type="class")
#
偶数行でテストしてみる
>
table(pred,
iris.test$Species)
pred
setosa
versicolor
virginica
setosa
25
0
0
versicolor
0
24
3
virginica
0
1
22