1 데이터와 모형 가져오기 [1]

2 archivist 객체 가져오기

타이타닉 데이터셋은 기계학습 모형 구축에 있어 무척이나 중요하다. archivist를 통해 R 객체와 데이터를 저장하고 필요에 따라 공유하는 것이 가능하다. 먼저 타이타닉 데이터셋과 쟈니(Jonny)가 타이타닉호에 승선했을 때 생존 확률을 알아보자. 참고로 쟈니는 8살이고 Southampton에서 탑승했고, 72 파운드를 지불했으며 부모없이 1등석에 탑승한 정보가 있다. henry는 47로, 1등석에 탑승했으며, 혼자 여행중이였으며 25 파운드를 배삭으로 지불했고, Cherbourg 에서 탑승했다. 2021-05-10 현재 시점 archivist 버그로 인해 데이터와 모형 접근이 되지 않아 나중에 고쳐지면 가져와서 활용할 예정임.

# devtools::install_github("pbiecek/archivist")
library(archivist)

# 타이타닉 생존 데이터셋 -----------------------------
## 기계학습을 위한 훈련 데이터셋 
titanic  <- archivist::aread("pbiecek/models/27e5c")
## 생존 확률 예측을 위한 데이터셋
johnny_d <- archivist::aread("pbiecek/models/e3596")
henry    <- archivist::aread("pbiecek/models/a6538")

# 기계학습 모형 --------------------------------------
titanic_lmr <- archivist::aread("pbiecek/models/58b24")
titanic_rf  <- archivist::aread("pbiecek/models/4e0fc")
titanic_gbm <- archivist::aread("pbiecek/models/b7078")
titanic_svm <- archivist::aread("pbiecek/models/9c27f")

3 타이타닉 데이터셋

stablelearner 팩키지 내부에 titanic 데이터셋이 포함되어 있다. 전처리가 된 깔끔한 데이터는 DALEX 팩키지에 titanic 데이터셋으로 저장되어 있다. 이를 기계학습 예측모형 개발에 사용하자.

library(tidyverse)
library(DALEX)
# library(stablelearner)

titanic <- DALEX::titanic %>% 
  na.omit(.) %>% 
  as_tibble()

titanic %>% 
  select(survived, everything()) %>% 
  reactable::reactable()

쟈니(Jonny)와 헨리(henry)도 기계학습 예측모형을 통해 생존확률을 예측하는데 필요한 탑승자로 분류하여 준비한다.

johnny_d <- 
  data.frame(
          class = factor("1st", levels = c("1st", "2nd", "3rd", 
                     "deck crew", "engineering crew", 
                     "restaurant staff", "victualling crew")),
          gender = factor("male", levels = c("female", "male")),
          age = 8, sibsp = 0, parch = 0, fare = 72,
          embarked = factor("Southampton", levels = c("Belfast",
                      "Cherbourg","Queenstown","Southampton"))) %>% 
  as_tibble()

henry <- 
  data.frame(
       class = factor("1st", levels = c("1st", "2nd", "3rd", 
                  "deck crew", "engineering crew", 
                  "restaurant staff", "victualling crew")),
       gender = factor("male", levels = c("female", "male")),
       age = 47, sibsp = 0, parch = 0, fare = 25,
       embarked = factor("Cherbourg", levels = c("Belfast",
                         "Cherbourg","Queenstown","Southampton"))) %>% 
  as_tibble()

passengers <- bind_rows(johnny_d, henry)

passengers %>% 
  reactable::reactable()

4 타이타닉 생존 모델

서로 다른 네가지 예측모형을 가지고 타이타닉 생존 모형을 구축한다.

# 로지스틱 회귀모형
library("rms")
titanic_lmr <- lrm(survived == "yes" ~ gender + rcs(age) + class + sibsp + parch + fare + embarked, titanic)

# 랜덤 포레스트
library("randomForest")
set.seed(1313)
titanic_rf <- randomForest(survived ~ class + gender + age + sibsp + parch + fare + embarked, data = titanic)

# GBM
library("gbm")
set.seed(1313)
titanic_gbm <- gbm(survived == "yes" ~ class + gender + age + sibsp + parch + fare + embarked, data = titanic, n.trees = 15000, distribution = "bernoulli")

# SVM
library("e1071")
titanic_svm <- svm(survived == "yes" ~ class + gender + age + sibsp + parch + fare + embarked, data = titanic, type = "C-classification", probability = TRUE)

5 모형과 데이터 저장

예측모형을 적합시켜 추후 XAI 목적으로 사용될 수 있도록 로컬 파일에 저장시키고 나중에 불러와서 후속 작업을 수행한다.

titanic_list <- list(
  data = list(training = titanic,
              henry    = henry,
              johnny_d = johnny_d),
  model = list(titanic_lmr = titanic_lmr,
               titanic_rf  = titanic_rf,
               titanic_gbm = titanic_gbm,
               titanic_svm = titanic_svm)
)

titanic_list %>% 
  write_rds("data/titanic_list.rds")
1. Biecek P, Kosinski M. archivist: An R package for managing, recording and restoring data analysis results. Journal of Statistical Software. 2017;82:1–28.
 

데이터 과학자 이광춘 저작

kwangchun.lee.7@gmail.com