開始使用免費開始

建置並評估最佳模型

透過交叉驗證,你已經找出使用 gapminder 中所有特徵來預測 life_expectancy 的最佳模型。現在既然已經選定模型,你可以使用先前保留做獨立評估的資料集(testing_data),來估計此模型在新資料上的表現。

你將使用全部的 training_data 來建置這個模型,並用 testing_data 進行評估。

本練習屬於課程

Tidyverse 的 Machine Learning

檢視課程

練習說明

  • 使用 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(___, ___)
編輯並執行程式碼