33. R ๋ก์ง์คํฑํ๊ท๋ถ์ ์ฐ์ต๋ฌธ์
01. ํ์ดํ๋(titanic) ๋ฐ์ดํฐ ์
์ ๋์์ผ๋ก 7:3 ๋น์จ๋ก ํ์ต๋ฐ์ดํฐ์ ๊ฒ์ ๋ฐ์ดํฐ๋ก ๊ฐ๊ฐ ์ํ๋งํ ํ ๊ฐ ๋จ๊ณ๋ณ๋ก ๋ถ๋ฅ๋ถ์์ ์ํํ์์ค.
titanic = read.csv("c:/ITWILL/2_Rwork/data/titanic3.csv")
str(titanic)
'data.frame': 1309 obs. of 14 variables:
pclass : 1, 2, 3๋ฑ์ ์ ๋ณด๋ฅผ ๊ฐ๊ฐ 1, 2, 3์ผ๋ก ์ ์ฅ
survived : ์์กด ์ฌ๋ถ. survived(์์กด=1), dead(์ฌ๋ง=0)
name : ์ด๋ฆ(์ ์ธ)
sex : ์ฑ๋ณ. female(์ฌ์ฑ), male(๋จ์ฑ)
age : ๋์ด
sibsp : ํจ๊ป ํ์นํ ํ์ ๋๋ ๋ฐฐ์ฐ์์ ์
parch : ํจ๊ป ํ์นํ ๋ถ๋ชจ ๋๋ ์๋
์ ์
ticket : ํฐ์ผ ๋ฒํธ(์ ์ธ)
fare : ํฐ์ผ ์๊ธ
cabin : ์ ์ค ๋ฒํธ(์ ์ธ)
embarked : ํ์นํ ๊ณณ.(์ ์ธ) C(Cherbourg), Q(Queenstown), S(Southampton)
boat : (์ ์ธ)Factor w/ 28 levels "","1","10","11",..: 13 4 1 1 1 14 3 1 28 1 ...
body : (์ ์ธ)int NA NA NA 135 NA NA NA NA NA 22 ...
home.dest: (์ ์ธ)
์์กด์ฌ๋ถ factorํ ๋ณํ : ๋๋ฏธ๋ณ์ ์์ฑ
titanic$survived <- factor(titanic$survived, levels = c(0, 1))
as.factor vs factor
as.factor : ๋ฌธ์ํ/์ซ์ํ -> ๋๋ฏธ๋ณ์
factor : ๋ฌธ์ํ/์ซ์ํ -> ๋๋ฏธ๋ณ์, levels ๋ณ๊ฒฝ ๊ฐ๋ฅ
์์กด์ฌ๋ถ ๋น๋์
table(titanic$survived)
0 1 -> 0:์ฌ๋ง, 1:์์กด
809 500
์์กด์ฌ๋ถ ๋น์จ
prop.table(table(titanic$survived))
0 1
0.618029 0.381971
๋จ๊ณ1 : ๋ณ์6๊ฐ ์ ์ธ ์๋ธ์
๋ง๋ค๊ธฐ : name, ticket, cabin, embarked, boat, body, home.dest
titanic <- titanic[-c(3, 8, 10:14)]
์นผ๋ผ๋ช
์ผ๋ก ์๋ธ์
์์ฑ ์ : ๋ถํธ(-)์ ์ฝ๋ก (:) ์ฌ์ฉ ๋ถ๊ฐ
str(titanic)
'data.frame': 1309 obs. of 7 variables:
๋จ๊ณ2 : 80% : 20% ๋ฐ์ดํฐ์
๋ถํ ํ๊ธฐ
ํ๋ จ์
: titanic_train, ๊ฒ์ ์
: titanic_test
idx = sample(nrow(titanic), nrow(titanic)*0.8)
titanic_train = titanic[idx, ]
titanic_test = titanic[-idx, ]
dim(titanic_train) #1047 7
dim(titanic_test) #262 7
๋จ๊ณ3 : ๋ถ๋ฅ๋ชจ๋ธ ์์ฑ : ์ข
์๋ณ์=survived, ๋
๋ฆฝ๋ณ์=๋๋จธ์ง ๋ณ์
library(rpart)
model = rpart(survived ~ ., data = titanic_train)
model
๋จ๊ณ4 : ์์ฌ๊ฒฐ์ ํธ๋ฆฌ ์๊ฐํ : ์ค์๋ณ์ 2~3๊ฐ๋ฅผ ๊ธฐ์ค์ผ๋ก ์์กดํ๋ฅ ์ด ๋์ ๊ฒฝ์ฐ ์ค๋ช
ํ๊ธฐ
rpart.plot(model)
[ํด์ค] ์ฑ๋ณ์ด ์ฌ์ฑ์ด๊ณ , ๋์ด๊ฐ 9.5์ธ ๋ฏธ๋ง, ์ ์ค์ 2๋ฑ ์ดํ์ธ ๊ฒฝ์ฐ ์์กดํ๋ฅ ๋๋ค.
๋จ๊ณ5 : ๋ชจ๋ธ ๊ฒ์ /ํ๊ฐ(0:Negative, 1:Positive) : ๋ถ๋ฅ์ ํ๋, ์ ํ๋ฅ , ์ ํ์จ, F1 score
y_pred = predict(model, titanic_test, type = 'class')
y_true = titanic_test$survived
ํผ๋ ํ๋ ฌ
t = table(y_true, y_pred)
t
y_pred
y_true 0(N) 1(P)
0(N) 137(TN) 26(FP) = 163
1(P) 26(FN) 73(TP) = 99
accuracy = (t[1,1] + t[2,2]) / sum(t) # (TN + TP) / ์ ์ฒด
accuracy #0.801526
precision = t[2,2] / sum(t[,2]) # TP / (TP + FP)
precision #0.7373737
recall = t[2,2] / sum(t[2,]) # TP / (TP + FN)
recall
f1_score = 2 * ((precision * recall) / (precision + recall))
f1_score #0.7373737
02. weather ๋ฐ์ดํฐ๋ฅผ ์ด์ฉํ์ฌ ๋ค์๊ณผ ๊ฐ์ ๋จ๊ณ๋ณ๋ก ์์ฌ๊ฒฐ์ ํธ๋ฆฌ ๋ฐฉ์์ผ๋ก ๋ถ๋ฅ๋ถ์์ ์ํํ์์ค.
์กฐ๊ฑด1) rpart() ํจ์ ์ด์ฉ ๋ถ๋ฅ๋ชจ๋ธ ์์ฑ
์กฐ๊ฑด2) y๋ณ์ : RainTomorrow, x๋ณ์ : 1, 6, 8, 14๋ฒ ๋ณ์ ์ ์ธํ ๋๋จธ์ง ๋ณ์๋ก ๋ถ๋ฅ๋ชจ๋ธ ์์ฑ
์กฐ๊ฑด3) ๋ชจ๋ธ์ ์๊ฐํ๋ฅผ ํตํด์ y์ ๊ฐ์ฅ ์ํฅ์ ๋ฏธ์น๋ x๋ณ์ ํ์ธ
์กฐ๊ฑด4) ๋น๊ฐ ์ฌ ํ๋ฅ ์ด 50% ์ด์์ด๋ฉด ‘Yes Rain’, 50% ๋ฏธ๋ง์ด๋ฉด ‘No Rain’์ผ๋ก ๋ฒ์ฃผํ
๋จ๊ณ1 : ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
library(rpart) # model ์์ฑ
library(rpart.plot) # ๋ถ๋ฅํธ๋ฆฌ ์๊ฐํ
setwd("c:/ITWILL/2_Rwork/data")
weather = read.csv("weather.csv", header=TRUE)
str(weather)
'data.frame': 366 obs. of 15 variables:
chrํ ๋ณ์ ์ ์ธ : y = RainTomorrow
weather <- weather[-c(1, 6, 8, 14)]
str(weather)
'data.frame': 366 obs. of 11 variables:
y๋ณ์ -> ๋๋ฏธ๋ณ์ ๋ณํ
weather$RainTomorrow <- as.factor(weather$RainTomorrow)
table(weather$RainTomorrow)
๋จ๊ณ2 : ๋ฐ์ดํฐ ์ํ๋ง : 70% vs 30%
idx <- sample(nrow(weather), nrow(weather)*0.7)
weather_train <- weather[idx, ] # model ํ์ต
weather_test <- weather[-idx, ] # model ํ๊ฐ
๋จ๊ณ3 : ๋ถ๋ฅ๋ชจ๋ธ ์์ฑ
model <- rpart(RainTomorrow ~ ., data = weather_train)
๋จ๊ณ4 : ๋ถ๋ฅ๋ชจ๋ธ ์๊ฐํ - ์ค์๋ณ์ ํ์ธ
rpart.plot(model)
[ํด์ค] ์๊ฐ(Humidity)์ ๋ํ์๋(WinGustSpeed), ๊ธฐ์(Pressure) ๋ฑ์ด ๋น์ ์ํฅ์ ๋ฏธ์น๋ ๋ณ์์ด๋ค.
๋จ๊ณ5 : ์์ธก ํ๋ฅ ๋ฒ์ฃผํ('Yes Rain', 'No Rain')
pred_rate <- predict(model, weather_test) # ๋น์จ ์์ธก
range(pred_rate) # 0.04268293 0.95731707 : ํ๋ฅ ๋ฒ์
dim(pred_rate) # 110 2(No Yes) -> (๋น๊ฐ ์์ฌ ํ๋ฅ ๋น๊ฐ์ฌ ํ๋ฅ )
ํด๋์ค ๋ถ๋ฅ
y_pred <- ifelse(pred_rate[, 2] > 0.5, 'Yes Rain', 'No Rain')
table(y_pred)
No Rain Yes Rain
101 9
๋จ๊ณ6 : ํผ๋ matrix ์์ฑ ๋ฐ ๋ถ๋ฅ ์ ํ๋ ๊ตฌํ๊ธฐ
t = table(y_pred, weather_test$RainTomorrow)
t
# y_pred No Yes : ๊ด์ธก์น
No Rain 88 13
Yes Rain 2 7
acc = (t[1,1] + t[2,2]) / sum(t)
cat('๋ถ๋ฅ์ ํ๋ =', acc) # ๋ถ๋ฅ์ ํ๋ = 0.8636364
03. Boston ๋ฐ์ดํฐ์
์ ๋์์ผ๋ก ๋จ๊ณ๋ณ๋ก ํ๊ทํธ๋ฆฌ ๋ชจ๋ธ์ ์์ฑํ์์ค.
๋จ๊ณ1 : ๋ฐ์ดํฐ์ ๊ฐ์ ธ์ค๊ธฐ
library(MASS)
data("Boston")
str(Boston)
$ crim : 1์ธ๋น ๋ฒ์ฃ์จ num 0.00632 0.02731 0.02729 0.03237 0.06905 ...
$ zn : 25,000 ํ๋ฐฉํผํธ ์ด๊ณผ ๊ฑฐ์ฃผ์ง์ญ ๋น์จ num 18 0 0 0 0 0 12.5 12.5 12.5 12.5 ...
$ indus : ๋น์๋งค์์
์ง์ญ ์ ์ ํ ์ง ๋น์จ num 2.31 7.07 7.07 2.18 2.18 2.18 7.87 7.87 7.87 7.87 ...
$ chas : ์ฐฐ์ค๊ฐ ๋๋ฏธ๋ณ์(1: ๊ฐ์ ๊ฒฝ๊ณ)int 0 0 0 0 0 0 0 0 0 0 ...
$ nox : ์ผ์ฐํ์ง์ num 0.538 0.469 0.469 0.458 0.458 0.458 0.524 0.524 0.524 0.524 ...
$ rm : ํ๊ท ๋ฐฉ์ ๊ฐ์ num 6.58 6.42 7.18 7 7.15 ...
$ age : ๊ณ ํ ๋น์จ num 65.2 78.9 61.1 45.8 54.2 58.7 66.6 96.1 100 85.9 ...
$ dis : ์ง์
์ผํฐ ์ ๊ทผ์ฑ ์ง์ num 4.09 4.97 4.97 6.06 6.06 ...
$ rad : ๋๋ก ์ ๊ทผ์ฑ ์ง์ int 1 2 2 3 3 3 5 5 5 5 ...
$ tax : ์ฌ์ฐ์ธ์จ num 296 242 242 222 222 222 311 311 311 311 ...
$ ptratio: ํ์/๊ต์ฌ ๋น์จ num 15.3 17.8 17.8 18.7 18.7 18.7 15.2 15.2 15.2 15.2 ...
$ black : ํ์ธ ๋น์จ num 397 397 393 395 397 ...
$ lstat : ํ์๊ณ์ธต ๋น์จ num 4.98 9.14 4.03 2.94 5.33 ...
$ medv : ์ฃผํ๊ฐ๊ฒฉ(๋จ์ : 1,000 ๋ฌ๋ฌ) num 24 21.6 34.7 33.4 36.2 28.7 22.9 27.1 16.5 18.9 ...
library(rpart) # model ์์ฑ
library(rpart.plot) # ์์ฌ๊ฒฐ์ ํธ๋ฆฌ ์๊ฐํ
๋จ๊ณ2 : ๋ถ๋ฅ๋ชจ๋ธ ์์ฑ : y๋ณ์ : medv, x๋ณ์ : ๋๋จธ์ง 13๊ฐ ๋ณ์
model <- rpart(medv ~ ., data = Boston)
model
๋จ๊ณ3 : ๋ถ๋ฅ๋ชจ๋ธ ์๊ฐํ - ์ค์๋ณ์ ํ์ธ
rpart.plot(model)
[ํด์ค] ๋ฐฉ์ ๊ฐ์(rm) ๋ง๊ณ , ํ์๊ณ์ธต ๋น์จ(lstat) ๋ฎ์ ์๋ก ์ฃผํ ๊ฐ๊ฒฉ์ด ์ค๋ฅธ๋ค.
๋จ๊ณ4 : 3๊ฒน ๊ต์ฐจ๊ฒ์ ์ ์ํํ์ฌ ๋ชจ๋ธ ํ๊ฐ
library(cvTools)
1) K๊ฒน ๊ต์ฐจ๊ฒ์ ์ ์ํ ์ํ๋ง
cross <- cvFolds(nrow(Boston), K = 3)
cross # Fold Index
cross$subsets[cross$which==1, 1]
cross$subsets[cross$which==2, 1]
cross$subsets[cross$which==3, 1]
2) K๊ฒน ๊ต์ฐจ๊ฒ์
n = 1:3
R2_score <- numeric() # ํ๊ฐ๊ฒฐ๊ณผ
for(k in n){ # 1 2 3
idx <- cross$subsets[cross$which==k, 1]
test <- Boston[idx, ] # ๊ฒ์ ์
(1set)
train <- Boston[-idx, ] # ํ๋ จ์
(2set)
model ์์ฑ
model = rpart(medv ~ ., data = train)
y ์์ธก์น : ๋น์จ์์ธก
y_pred <- predict(model, test)
y_true <- test$medv
model ํ๊ฐ : R2 score
R2_score[k] <- cor(y_true, y_pred)^2}
3) ๊ต์ฐจ๊ฒ์ ํ๊ฐ : R2 score(y ์ค์ผ์ผ๋ง ์๋ ๊ฒฝ์ฐ)
cat('R2 score =', mean(R2_score))
#R2 score = 0.7114659