การใช้งาน GridSearch
ตอนนี้สามารถใช้ฟังก์ชัน GridSearchCV() ของ sklearn เพื่อค้นหาค่าผสมที่ดีที่สุดของ max_depth และ min_samples_leaf ทั้งหมดที่สร้างไว้ในแบบฝึกหัดก่อนหน้า
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
HR Analytics: การพยากรณ์การลาออกของพนักงานด้วย Python
คำแนะนำการฝึกหัด
- นำเข้าฟังก์ชัน
GridSearchCV - นำฟังก์ชัน
GridSearchCV()ไปใช้กับmodelโดยใช้ dictionaryparametersที่กำหนดไว้ก่อนหน้า แล้วบันทึกเป็นparam_search - Fit
param_searchกับชุดข้อมูลสำหรับเทรน - แสดงพารามิเตอร์ที่ดีที่สุดที่พบโดยใช้ attribute
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.____)