開始使用免費開始

模型混合(Blending)

你將以「blending」技術開始建立模型集成(ensemble)。

你的目標是針對 New York City Taxi 競賽的資料訓練 2 個不同的模型。對測試資料做預測,接著用簡單的算術平均將兩者混合。

traintest 這兩個 DataFrame 已在你的工作區中可用。features 是用於訓練的欄位名稱清單,也已在你的工作區中可用。目標變數名稱為「fare_amount」。

本練習屬於課程

用 Python 拿下 Kaggle 競賽

檢視課程

練習說明

  • 使用 features 清單在訓練資料上訓練一個 Gradient Boosting 模型,並以「fare_amount」欄作為目標變數。
  • 以相同方式訓練一個 Random Forest 模型。
  • 使用兩個模型(Gradient Boosting 與 Random Forest)對測試資料進行預測。
  • 計算兩個模型預測值的平均。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

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))
編輯並執行程式碼