偏差-方差权衡

R 中的树模型机器学习

Sandro Raabe

Data Scientist

超参数

  • 由建模者选择
  • tree_depth
  • 查阅文档!
?decision_tree

多个超参数

R 中的树模型机器学习

简单模型

simple_spec <- decision_tree(tree_depth = 2) %>% 
    set_mode("regression")

simple_spec %>% fit(final_grade ~ .,
                    data = training_data)

复杂模型

complex_spec <- decision_tree(tree_depth = 15) %>% 
    set_mode("regression")

complex_spec %>% fit(final_grade ~ .,
                     data = training_data)

深度为2的树

深度为30的树

R 中的树模型机器学习

复杂模型——过拟合——高方差

训练集预测:表现很好!

训练集误差小

mae(train_results, 
    estimate = .pred,
    truth = final_grade)
# A tibble: 1 x 3
  .metric  .estimate
1 mae          0.204

测试集预测:差得多!

测试集误差大

mae(test_results, 
    estimate = .pred,
    truth = final_grade)
# A tibble: 1 x 3
  .metric  .estimate
1 mae          0.947
R 中的树模型机器学习

简单模型——欠拟合——高偏差

训练集和测试集上均为误差:

bind_rows(training = mae(train_results, estimate = .pred, truth = final_grade),
          test     = mae(test_results,  estimate = .pred, truth = final_grade),
          .id = "dataset")
# A tibble: 2 x 4
  dataset    .metric  .estimate
  <chr>      <chr>        <dbl>
1 training   mae          0.754
2 test       mae          0.844
R 中的树模型机器学习

偏差-方差权衡

偏差-方差权衡

 

  • 简单模型 -> 高偏差
  • 复杂模型 -> 高方差
  • 偏差与方差的权衡
  • 将模型建在"最佳点"附近
R 中的树模型机器学习

检测过拟合

样本外/交叉验证:

collect_metrics(cv_fits)


# A tibble: 1 x 3
  .metric    mean     n 
1 mae       2.432     5
  • CV误差高
  • 过拟合 / 高方差
  • 降低复杂度!

样本内:

mae(training_pred, 
    estimate = .pred, 
    truth = final_grade)
# A tibble: 1 x 2
  .metric  .estimate
1 mae          0.228
  • 训练误差小
R 中的树模型机器学习

检测欠拟合

样本内:

mae(training_pred, estimate = .pred, truth = final_grade)
# A tibble: 1 x 2
  .metric .estimate
  <chr>       <dbl>
1 mae         2.432
  • 样本内/训练误差大
  • 欠拟合 / 高偏差
  • 提高复杂度!
R 中的树模型机器学习

让我们权衡一下!

R 中的树模型机器学习

Preparing Video For Download...