R言語を使ってCARTアルゴリズムを使った決定木を作成する手順を自分用のメモとしてまとめました。
手順 1: サンプル表データ Titanic
を読み込みます
R> data("Titanic")
R> Titanic
手順 2: 関数 str()
を使って、表 Titanic
の構造を確認します
R> str(Titanic)
table [1:4, 1:2, 1:2, 1:2] 0 0 35 0 0 0 17 0 118 154 ...
- attr(*, "dimnames")=List of 4
..$ Class : chr [1:4] "1st" "2nd" "3rd" "Crew"
..$ Sex : chr [1:2] "Male" "Female"
..$ Age : chr [1:2] "Child" "Adult"
..$ Survived: chr [1:2] "No" "Yes"
→ table 型のオブジェクトであることを確認します
手順 3: 関数 data.frame()
を使って、データフレーム temporary.data
を作成します
R> temporary.data <- data.frame(Titanic)
R> temporary.data
手順 4: 関数 str()
を使って、データフレーム temporary.data
の構造を確認します
R> str(temporary.data)
'data.frame': 32 obs. of 5 variables:
$ Class : Factor w/ 4 levels "1st","2nd","3rd",..: 1 2 3 4 1 2 3 4 1 2 ...
$ Sex : Factor w/ 2 levels "Male","Female": 1 1 1 1 2 2 2 2 1 1 ...
$ Age : Factor w/ 2 levels "Child","Adult": 1 1 1 1 1 1 1 1 2 2 ...
$ Survived: Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...
$ Freq : num 0 0 35 0 0 0 17 0 118 154 ...
→ 1列目に Class(等級)、2列目に Sex(性別)、3列目に Age(大人か子供か)、4列目に Survived(生還者か死亡者か)、5列目に Freq(その人数)が格納された data.frame 型のオブジェクトであることを確かめます。
手順 5: 関数 rep()
と関数 data.frame()
を使って、1行 = 1人 になるようにデータフレーム titanic.data
に変換します
R> titanic.data <- data.frame(
Class = rep(temporary.data$Class, temporary.data$Freq),
Sex = rep(temporary.data$Sex, temporary.data$Freq),
Age = rep(temporary.data$Age, temporary.data$Freq),
Survived = rep(temporary.data$Survived, temporary.data$Freq)
)
R> titanic.data
手順 6: 関数 str()
を使って作成したデータフレーム titanic.data
のデータ構造を確認します
R> str(titanic.data)
data.frame': 2201 obs. of 4 variables:
$ Class : Factor w/ 4 levels "1st","2nd","3rd",..: 3 3 3 3 3 3 3 3 3 3 ...
$ Sex : Factor w/ 2 levels "Male","Female": 1 1 1 1 1 1 1 1 1 1 ...
$ Age : Factor w/ 2 levels "Child","Adult": 1 1 1 1 1 1 1 1 1 1 ...
$ Survived: Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...
→ 4列構造の data.frame 型のオブジェクトに変換された事を確かめます。
手順 7: ライブラリ mvpart
を読み込みます
R> library("mvpart")
手順 8: 関数 rpart()
を使って、Classification tree(分類木)を作成します
R> titanic.decision.tree <- rpart(formula=Survived~., data=titanic.data, method="class")
R> titanic.decision.tree
n= 2201
node), split, n, loss, yval, (yprob)
* denotes terminal node
1) root 2201 711 No (0.6769650 0.3230350)
2) Sex=Male 1731 367 No (0.7879838 0.2120162)
4) Age=Adult 1667 338 No (0.7972406 0.2027594) *
5) Age=Child 64 29 No (0.5468750 0.4531250)
10) Class=3rd 48 13 No (0.7291667 0.2708333) *
11) Class=1st,2nd 16 0 Yes (0.0000000 1.0000000) *
3) Sex=Female 470 126 Yes (0.2680851 0.7319149)
6) Class=3rd 196 90 No (0.5408163 0.4591837) *
7) Class=1st,2nd,Crew 274 20 Yes (0.0729927 0.9270073) *
手順 9: ライブラリ partykit
を読み込みます
R> library("partykit")
手順 10: 関数 plot()
と 関数 as.party()
を使って図を描写します
R> plot(as.party(titanic.decision.tree))
おまけ: C5.0 アルゴリズムを使って決定木を作成する
手順 1: ライブラリ C50
を読み込みます
R> library("C50")
手順 2: 関数 C5.0()
を使って、Decision tree(決定木)を作成します
R> titanic.c50.decision.tree <- C5.0(x=titanic.data [,-4], y=titanic.data$Survived)
※ x: 目的変数
※ y: 説明変数
手順 3: 関数 summary.C5.0()
を使って、作成した Decision tree(決定木)を確認します
R> summary.C5.0(titanic.c50.decision.tree)
Call:
C5.0.default(x = titanic.data[, -4], y = titanic.data$Survived)
C5.0 [Release 2.07 GPL Edition] Xxx Xxx xx xx:xx:xx xxxx
-------------------------------
Class specified by attribute `outcome'
Read 2201 cases (4 attributes) from undefined.data
Decision tree:
Sex = Male: No (1731/367)
Sex = Female:
:...Class in {1st,2nd,Crew}: Yes (274/20)
Class = 3rd: No (196/90)
Evaluation on training data (2201 cases):
Decision Tree
----------------
Size Errors
3 477(21.7%) <<
(a) (b) <-classified as
---- ----
1470 20 (a): class No
457 254 (b): class Yes
Attribute usage:
100.00% Sex
21.35% Class
Time: 0.0 secs
以上