交叉驗證統計
你已使用 grid search CV 調整隨機森林分類器,現在想檢視交叉驗證結果,以確保你沒有過度擬合。特別是你想要計算每個摺疊的平均測試分數與平均訓練分數之差。資料集已提供為 X_train 與 y_train,管線為 pipe,且多個模組已預先載入,包括以 pd 匯入的 pandas 以及 GridSearchCV()。
本練習屬於課程
在 Python 設計機器學習工作流程
練習說明
- 建立一個具有 3 個交叉驗證摺疊的 grid search 物件,並確保同時回傳訓練與測試統計量。
- 將該 grid search 物件擬合到訓練資料上。
- 將已擬合之 CV 物件的
cv_results_屬性(其中包含交叉驗證結果)存成一個資料框。 - 印出包含平均測試分數的欄位與包含平均訓練分數的欄位之間的差。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Fit your pipeline using GridSearchCV with three folds
grid_search = GridSearchCV(
pipe, params, ____=3, return_train_score=____)
# Fit the grid search
gs = grid_search.____(____, ____)
# Store the results of CV into a pandas dataframe
results = pd.____(gs.____)
# Print the difference between mean test and training scores
print(
results[____]-results['mean_train_score'])