开始使用免费开始使用

构建并评估最佳模型

通过交叉验证,您已经找到了在使用 gapminder 中所有特征来预测 life_expectancy 的最佳模型。现在模型已选定,您可以使用之前预留的独立数据集(testing_data)来估计该模型在新数据上的表现。

您将使用全部的 training_data 训练该模型,并使用 testing_data 进行评估。

本练习是课程的一部分

Tidyverse 中的机器学习

查看课程

练习说明

  • 使用 ranger() 在全部训练数据上构建表现最好的模型(mtry = 4),并将其赋值给 best_model
  • testing_data 中提取 life_expectancy 列,赋值给 test_actual
  • 使用 best_modeltesting 数据上预测 life_expectancy,并赋值给 test_predicted
  • 使用 test_actualtest_predicted 向量计算 MAE。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Build the model using all training data and the best performing parameter
best_model <- ranger(formula = ___, data = ___,
                     mtry = ___, num.trees = 100, seed = 42)

# Prepare the test_actual vector
test_actual <- testing_data$___

# Predict life_expectancy for the testing_data
test_predicted <- predict(___, ___)$predictions

# Calculate the test MAE
mae(___, ___)
编辑并运行代码