實作 GridSearch
你現在可以使用 sklearn 的 GridSearchCV() 函式,從前一個練習產生的所有 max_depth 和 min_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.____)