開始使用免費開始

實作 GridSearch

你現在可以使用 sklearnGridSearchCV() 函式,從前一個練習產生的所有 max_depthmin_samples_leaf 組合中,找出最佳的參數組合。

本練習屬於課程

HR 分析:用 Python 預測員工流失

檢視課程

練習說明

  • 匯入 GridSearchCV 函式。
  • 對你的 model 套用 GridSearchCV(),並使用先前定義的 parameters 字典。將結果儲存為 param_search
  • param_search 擬合到訓練資料集。
  • 使用 best_params_ 屬性列印出找到的最佳參數。

動手互動練習

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

# import the GridSearchCV function
from sklearn.model_selection import ____

# set up parameters: done
parameters = dict(max_depth=depth, min_samples_leaf=samples)

# initialize the param_search function using the GridSearchCV function, initial model and parameters above
param_search = ____(model, parameters, cv=3)

# fit the param_search to the training dataset
____.fit(features_train, target_train)

# print the best parameters found
print(param_search.____)
編輯並執行程式碼