使用 H2O 的机器学习

R 中的超参数调优

Dr. Shirin Elsinghorst

Senior Data Scientist

什么是 H2O?

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 集群健康:          TRUE 
    H2O 连接 IP:           localhost 
    H2O 连接端口:          54321 
    H2O 连接代理:          NA 
    H2O 内部安全:          FALSE 
    R 版本:                 R version 4.2.1 (2022-06-23)
R 中的超参数调优

新数据集:种子数据

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
R 中的超参数调优

用 H2O 准备建模数据

  • 数据为H2O 帧

    seeds_data_hf <- as.h2o(seeds_data)
    
  • 定义特征和目标变量

    y <- "seed_type"
    x <- setdiff(colnames(seeds_data_hf), y)
    
  • 分类时目标应为因子

    seeds_data_hf[, y] <- as.factor(seeds_data_hf[, y])
    
R 中的超参数调优

训练集、验证集与测试集

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
R 中的超参数调优

使用 H2O 训练模型

  • 使用 h2o.gbm() 和 h2o.xgboost() 的梯度提升模型
  • 使用 h2o.glm() 的广义线性模型
  • 使用 h2o.randomForest() 的随机森林模型
  • 使用 h2o.deeplearning() 的神经网络
R 中的超参数调优

使用 H2O 训练模型

gbm_model <- h2o.gbm(x = x, y = y, 
                     training_frame = train, 
                     validation_frame = valid)
模型详情:
==============

H2OMultinomialModel: gbm
模型 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
R 中的超参数调优
  • 模型性能
perf <- h2o.performance(gbm_model, test)

h2o.confusionMatrix(perf)
混淆矩阵:行标签:实际类别;列标签:预测类别
       1 2 3  错误     比率
1      7 0 1 0.1250 =  1 / 8
2      0 8 0 0.0000 =  0 / 8
3      0 0 5 0.0000 =  0 / 5
合计   7 8 6 0.0476 = 1 / 21
h2o.logloss(perf)
0.2351779
  • 预测新数据
h2o.predict(gbm_model, test)
R 中的超参数调优

开始练习!

R 中的超参数调优

Preparing Video For Download...