R에서 하이퍼파라미터 튜닝
Dr. Shirin Elsinghorst
Senior Data Scientist
library(h2o)
h2o.init()
H2O가 아직 실행 중이 아닙니다. 지금 시작합니다...
java version "1.8.0_351"
Java(TM) SE Runtime Environment (build 1.8.0_351-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.351-b10, mixed mode)
H2O JVM 시작 및 연결: ... 연결 성공!
R이 H2O 클러스터에 연결되었습니다:
H2O 클러스터 가동 시간: 1초 620밀리초
H2O 클러스터 표준시: UTC
H2O 데이터 파싱 표준시: UTC
H2O 클러스터 버전: 3.38.0.1
H2O 클러스터 버전 경과: 2개월 25일
H2O 클러스터 이름: H2O_started_from_R_repl_chk886
H2O 클러스터 노드 수: 1
H2O 클러스터 총 메모리: 0.98 GB
H2O 클러스터 총 코어: 2
H2O 클러스터 허용 코어: 2
H2O 클러스터 상태: TRUE
H2O 연결 IP: localhost
H2O 연결 포트: 54321
H2O 연결 프록시: NA
H2O 내부 보안: FALSE
R 버전: R version 4.2.1 (2022-06-23)
glimpse(seeds_data)
관측치: 150
변수: 8
$ area <dbl> 15.26, 14.88, 14.29, 13.84 ...
$ perimeter <dbl> 14.84, 14.57, 14.09, 13.94 ...
$ compactness <dbl> 0.8710, 0.8811, 0.9050 ...
$ kernel_length <dbl> 5.763, 5.554, 5.291, 5.324 ...
$ kernel_width <dbl> 3.312, 3.333, 3.337, 3.379 ...
$ asymmetry <dbl> 2.2210, 1.0180, 2.6990 ...
$ kernel_groove <dbl> 5.220, 4.956, 4.825, 4.805 ...
$ seed_type <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
seeds_data %>%
count(seed_type)
# A tibble: 3 x 2
seed_type n
<int> <int>
1 1 50
2 2 50
3 3 50
데이터는 H2O 프레임으로
seeds_data_hf <- as.h2o(seeds_data)
피처와 타깃 변수 정의
y <- "seed_type"
x <- setdiff(colnames(seeds_data_hf), y)
분류 문제에서는 타깃을 factor로 변환
seeds_data_hf[, y] <- as.factor(seeds_data_hf[, y])
sframe <- h2o.splitFrame(data = seeds_data_hf,
ratios = c(0.7, 0.15),
seed = 42)
train <- sframe[[1]]
valid <- sframe[[2]]
test <- sframe[[3]]
summary(train$seed_type, exact_quantiles = TRUE)
seed_type
1:36
2:36
3:35
summary(test$seed_type, exact_quantiles = TRUE)
seed_type
1:8
2:8
3:5
h2o.gbm(), h2o.xgboost()로 그레이디언트 부스팅h2o.glm()로 일반화 선형 모델h2o.randomForest()로 랜덤 포레스트h2o.deeplearning()로 신경망gbm_model <- h2o.gbm(x = x, y = y,
training_frame = train,
validation_frame = valid)
모델 세부정보:
==============
H2OMultinomialModel: gbm
Model ID: GBM_model_R_1540736041817_1
모델 요약:
number_of_trees number_of_internal_trees model_size_in_bytes min_depth
50 150 24877 2
max_depth mean_depth min_leaves max_leaves mean_leaves
5 4.72000 3 10 8.26667
perf <- h2o.performance(gbm_model, test)h2o.confusionMatrix(perf)
혼동 행렬: 행=실제 클래스; 열=예측 클래스
1 2 3 Error Rate
1 7 0 1 0.1250 = 1 / 8
2 0 8 0 0.0000 = 0 / 8
3 0 0 5 0.0000 = 0 / 5
Totals 7 8 6 0.0476 = 1 / 21
h2o.logloss(perf)
0.2351779
h2o.predict(gbm_model, test)
R에서 하이퍼파라미터 튜닝