開始使用免費開始

整合所有步驟

你剛加入一家心律不整偵測的新創公司,想用心律不整資料集 arrh 來訓練模型。你注意到隨機森林在不少 Kaggle 比賽中表現不錯,所以想用網格搜尋來嘗試最大深度為 2、5、或 10。你也發現資料集的維度相當高,因此想評估特徵選擇方法的效果。

為了避免不小心過度擬合,你已經先把資料分割好。你會用 X_trainy_train 做網格搜尋,並用 X_testy_test 來判斷特徵選擇是否有幫助。這四個資料分割折已預先載入到你的環境中。你也可以使用 GridSearchCV()train_test_split()SelectKBest()chi2(),以及作為 rfc 提供的 RandomForestClassifier

本練習屬於課程

在 Python 設計機器學習工作流程

檢視課程

練習說明

  • 使用網格搜尋,對 RandomForestClassifier 嘗試最大深度為 2、5、與 10,並儲存表現最佳的參數設定。
  • 接著,依據上一步找出的最佳參數,重新擬合估計器。
  • 套用使用 chi2 評分函式的 SelectKBest 特徵選擇器,並重新擬合分類器。

動手互動練習

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

# Find the best value for max_depth among values 2, 5 and 10
grid_search = GridSearchCV(
  ____(random_state=1), param_grid=____)
best_value = grid_search.____(
  ____, ____).best_params_['max_depth']

# Using the best value from above, fit a random forest
clf = rfc(
  random_state=1, ____=best_value).____(X_train, y_train)

# Apply SelectKBest with chi2 and pick top 100 features
vt = SelectKBest(____, k=____).____(X_train, y_train)

# Create a new dataset only containing the selected features
X_train_reduced = ____.transform(____)
編輯並執行程式碼