การผสมโมเดล (Model Blending)
เริ่มต้นสร้าง ensemble โมเดลด้วยเทคนิค blending
เป้าหมายคือการฝึกโมเดล 2 แบบที่แตกต่างกันบนข้อมูลการแข่งขัน New York City Taxi จากนั้นพยากรณ์ผลบนข้อมูลทดสอบ แล้วผสมผลลัพธ์โดยใช้ค่าเฉลี่ยเลขคณิตอย่างง่าย
train และ test DataFrames พร้อมใช้งานแล้วใน workspace ของคุณ features คือรายการคอลัมน์ที่ใช้สำหรับการฝึกโมเดล ซึ่งก็พร้อมใช้งานใน workspace เช่นกัน ตัวแปรเป้าหมายมีชื่อว่า "fare_amount"
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การแข่งขัน Kaggle ด้วย Python
คำแนะนำการฝึกหัด
- ฝึก Gradient Boosting model บนข้อมูล train โดยใช้รายการ
featuresและคอลัมน์ "fare_amount" เป็นตัวแปรเป้าหมาย - ฝึก Random Forest model ในลักษณะเดียวกัน
- พยากรณ์ผลบนข้อมูลทดสอบโดยใช้ทั้ง Gradient Boosting และ Random Forest model
- คำนวณค่าเฉลี่ยของผลพยากรณ์จากทั้งสองโมเดล
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
from sklearn.ensemble import GradientBoostingRegressor, RandomForestRegressor
# Train a Gradient Boosting model
gb = GradientBoostingRegressor().____(____[features], ____.fare_amount)
# Train a Random Forest model
rf = RandomForestRegressor().____(____[features], ____.fare_amount)
# Make predictions on the test data
test['gb_pred'] = ____.____(test[features])
test['rf_pred'] = ____.____(test[features])
# Find mean of model predictions
test['blend'] = (____[____] + ____[____]) / 2
print(test[['gb_pred', 'rf_pred', 'blend']].head(3))