สร้างและประเมินโมเดลที่ดีที่สุด
จากการใช้ cross-validation คุณสามารถระบุโมเดลที่ดีที่สุดสำหรับการทำนาย life_expectancy โดยใช้ฟีเจอร์ทั้งหมดใน gapminder ได้แล้ว เมื่อเลือกโมเดลได้แล้ว ให้นำชุดข้อมูลอิสระ (testing_data) ที่สำรองไว้มาใช้ประเมินประสิทธิภาพของโมเดลกับข้อมูลใหม่
คุณจะสร้างโมเดลนี้โดยใช้ข้อมูลทั้งหมดใน training_data และประเมินผลด้วย testing_data
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Machine Learning ใน Tidyverse
คำแนะนำการฝึกหัด
- ใช้
ranger()เพื่อสร้างโมเดลที่มีประสิทธิภาพสูงสุด (mtry = 4) โดยใช้ข้อมูล training ทั้งหมด แล้วกำหนดให้กับbest_model - ดึงคอลัมน์
life_expectancyจากtesting_dataแล้วกำหนดให้กับtest_actual - ทำนาย
life_expectancyโดยใช้best_modelกับข้อมูลtestingแล้วกำหนดให้กับtest_predicted - คำนวณค่า MAE โดยใช้เวกเตอร์
test_actualและtest_predicted
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# 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(___, ___)