最良モデルの構築と評価
交差検証により、gapminder のすべての特徴量を使って life_expectancy を予測する最良のモデルを特定できました。モデルを選んだら、保持しておいた独立データ集合(testing_data)を使って、新しいデータに対するこのモデルの性能を見積もることができます。
ここでは、すべての training_data を使ってこのモデルを構築し、testing_data を使って評価します。
この演習はコースの一部です
Tidyverse で学ぶ Machine Learning
演習の手順
- すべての訓練データを使って、
ranger()で最良のモデル(mtry = 4)を構築し、best_modelに代入します。 testing_dataからlife_expectancy列を抽出して、test_actualに代入します。best_modelを使ってtestingデータでlife_expectancyを予測し、test_predictedに代入します。test_actualとtest_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(___, ___)