模型堆疊 II
好,回顧一下你在堆疊實作中已經完成的步驟:
- 將訓練資料分成兩部分
- 在第 1 部分上訓練多個模型
- 對第 2 部分進行預測
- 對測試資料進行預測
現在,你的目標是使用步驟 3 和 4 的預測作為特徵,建立第二層模型。也就是說,這個模型會在第 2 部分的資料上訓練,然後你就可以在測試資料上進行堆疊預測。
part_2 和 test 這兩個 DataFrame 已經在你的工作環境中可用。Gradient Boosting 與 Random Forest 的預測已分別以「gbpred」與「rfpred」欄位名稱儲存在這些 DataFrame 中。
本練習屬於課程
用 Python 拿下 Kaggle 競賽
練習說明
- 使用 Gradient Boosting 與 Random Forest 模型的預測作為特徵,在第 2 部分資料上訓練一個 Linear Regression 模型。
- 使用 Gradient Boosting 與 Random Forest 模型的預測作為特徵,對測試資料進行預測。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
from sklearn.linear_model import LinearRegression
# Create linear regression model without the intercept
lr = LinearRegression(fit_intercept=False)
# Train 2nd level model on the Part 2 data
lr.____(part_2[['gb_pred', '____']], part_2.fare_amount)
# Make stacking predictions on the test data
test['stacking'] = lr.____(test[['gb_pred', '____']])
# Look at the model coefficients
print(lr.coef_)